Android:使用默认音乐播放器播放歌曲文件

问题描述:

有没有办法使用默认媒体播放器播放媒体。我可以用下面的代码做Android:使用默认音乐播放器播放歌曲文件

Intent intent = new Intent(Intent.ACTION_VIEW); 
MimeTypeMap mime = MimeTypeMap.getSingleton(); 
String type = mime.getMimeTypeFromExtension("mp3"); 
intent.setDataAndType(Uri.fromFile(new File(songPath.toString())), type); 
startActivity(intent); 

但是这会启动一个控制器较少的播放器,不能将其推到背景。 我可以用默认媒体播放器启动播放器吗?

Thx! 拉胡尔。

试试下面的代码:::

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); 
    File file = new File(songPath.toString()); 
    intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
    startActivity(intent); 

更新::试试这也

Intent intent = new Intent(); 
    ComponentName comp = new ComponentName("com.android.music", "com.android.music.MediaPlaybackActivity"); 
    intent.setComponent(comp); 
    intent.setAction(android.content.Intent.ACTION_VIEW); 
    File file = new File(songPath.toString()); 
    intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
    startActivity(intent); 
+0

thx!但是它的功能和我的代码一样。我想启动默认媒体播放器。 – rahul

+0

它启动默认播放器为我的想法..也更新我的帖子检查它 –

+0

第二个选项工作。我们需要在意图中设置组件对象。谢谢! – rahul

我一直在研究这个在过去的几天,因为我没有股票音乐播放器。看起来很悲惨,不能轻易完成。在浏览各种音乐应用程序的AndroidManifest.xml以查找线索后,我偶然发现了MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH。

使用下面的方法,只要歌曲位于Android MediaStore中,我就可以在后台启动Samsung音乐播放器。您可以指定艺术家,专辑或标题。这种方法也适用于谷歌播放音乐,但遗憾的是,即使普通的Android播放器的最新版本没有此意图:

https://github.com/android/platform_packages_apps_music/blob/master/AndroidManifest.xml

private boolean playSong(String search){ 
    try { 
     Intent intent = new Intent(); 
     intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra(SearchManager.QUERY, search); 
     startActivity(intent); 
     return true; 
    } catch (Exception ex){ 
     ex.printStackTrace(); 
     // Try other methods here 
     return false; 
    } 
} 

这将是很好地发现,使用内容URI的解决方案或URL,但该解决方案适用于我的应用程序。

+0

这实际上适用于note2。凉。谢谢 – zproxy