2015-06-19 193 views
5

我的应用程序显示来自SD卡的歌曲列表。我希望能够在默认播放器上播放歌曲。我有关于这首歌曲的所有数据:编号,标题,专辑,艺术家,路径...在默认音乐播放器上播放歌曲 - android

有没有办法启动默认播放器来播放歌曲?

我已经试过:

  1. 使用Intent.CATEGORY_APP_MUSIC。我可以启动默认播放器,但无法设置歌曲。 Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC)打开默认的音乐应用程序。 但是intent.setData(Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id))引发意图未找到的异常。使用已过时的MediaStore.INTENT_ACTION_MUSIC_PLAYER。未找到活动。 Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER).setDataAndType(Uri.fromFile(songFile), "audio/*")

  2. 使用INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH。启动搜索,但不是该歌曲,表示无法在Play音乐上准备混音。不过,它现在可以从谷歌,所以它可能是关键。 Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).putExtra(SearchManager.QUERY, name)
  3. 使用ACTION_VIEW。它的工作原理,但它推出了一个紧凑版本的播放器,我想要完整的播放器。

compat view

注:我想推出外部播放器,而不是在我的应用程序。

更新:看起来像Google Now硬编码Play音乐和Youtube支持。

+0

的[推出的Android使用意向的音乐播放器]可能重复(H ttp://stackoverflow.com/questions/3114471/android-launching-music-player-using-intent) –

+0

其实不是,我想推出特定的歌曲,而不仅仅是音乐播放器。 –

+0

您是否找到答案> –

回答

0

尝试用这样简单的东西:

MediaPlayer mp = new MediaPlayer();  
mp.setLooping(true); 
try { 
    mp.setDataSource(mFile); //mFile is the path to your mp3 file 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
try { 
    mp.prepare(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
mp.start(); 

还有一例子与MadiaPlayer和文件保存在res /生/目录:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); 
mediaPlayer.start(); // no need to call prepare(); create() does that for you 

更多:http://developer.android.com/guide/topics/media/mediaplayer.html

+0

**注意**:我想启动**外部播放器**,不在我的应用程序中。 –

+0

嗯,所以你尝试用这个选定的最佳答案在这里http://stackoverflow.com/questions/3114471/android-launching-music-player-using-intent? –

+0

是的,它启动了一个只播放而不是整个客户端的紧凑版本。 –

0
Intent musicIntent = new Intent(); 
//use Action VIEW to launch app 
musicIntent.setAction(Intent.ACTION_VIEW);  

String mimeType = getMimeTypeFromFile(); // in your case it is audio 
Uri uri = " yourfile uri"; 
musicIntent.setDataAndType(uri,mimeType); 
startActivity(intent); 

如果更改了mime类型,默认视图应用程序将被打开,用于视频mime-type vid eo播放器将被打开,用于音频音乐播放器和图像库。

如果有多个玩家然后Resolver活动将被打开。

不要忘记赶上ActivityNotFoundException

在音乐播放器ACTION_VIEW AudioPreview activty被称为优先。还有其他的活动可以处理这个动作,只需使用动作“com.android.music.PLAYBACK_VIEWER”而不是ACTION_VIEW或使用它来检查一次。

+0

在我的问题中,这与解决方案4有何不同? –

+0

检查AndroidManifest.xl音乐应用程序,您可以从AndroidXRef获取代码 –

+0

android.content.ActivityNotFoundException:未找到处理Intent的活动{act = com.android.music.PLAYBACK_VIEWER dat = content:// media/external/audio/media/2797 typ = audio/*} –

3

如果你想启动设备上的默认的音乐播放器应用程序,应该试试这个:

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
File file = new File(YOUR_SONG_URI); 
intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
startActivity(intent); 
+0

它推出了精简版的默认音乐播放器,在更新后的问题中看到图片 –

+0

啊,对不起,我很想你! – Karoly

+0

没关系,这是最接近的解决方案。 –

0

它将推出一个紧凑型播放作为应用程序已经实现这种方式, 可以发现许多第三方应用程序打开的全部应用程序与

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
File file = new File(YOUR_SONG_URI); 
intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
startActivity(intent); 

,你已经尝试过这种

相关问题