在默认音乐播放器上播放歌曲 - 安卓

Yoa*_*erg 8 android mediastore android-intent android-music-player

我的应用程序显示sd卡的歌曲列表.我希望能够在默认播放器上播放歌曲.我已经获得了关于这首歌的所有数据:id,title,album,artist,path ...

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

我尝试过的:

  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))抛出异常没有找到意图.

  2. 使用已弃用MediaStore.INTENT_ACTION_MUSIC_PLAYER.找不到活动. Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER).setDataAndType(Uri.fromFile(songFile), "audio/*")

  3. INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH.启动搜索,但不是歌曲,说无法准备混合播放音乐.但是,它现在可以在Google上运行,因此它可能是关键. Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).putExtra(SearchManager.QUERY, name)
  4. ACTION_VIEW.它有效,但它推出了一个紧凑版本的播放器,我想要完整的播放器.

compat视图

注意:我想启动外部播放器,而不是在我的应用中.

更新:看起来像Google Now硬编码的Play Music和Youtube支持.

Gab*_*ova 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();
Run Code Online (Sandbox Code Playgroud)

还有一个使用 MadiaPlayer 并将文件保存在 res/raw/ 目录中的示例:

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

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