有没有办法用默认媒体播放器播放媒体?我可以使用以下代码执行此操作:
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);
但这会启动一个控制较少的玩家,不能被推到后台。我可以使用默认媒体播放器启动播放器吗?
试试以下代码:::
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);
我最近几天一直在研究这个,因为我没有音乐播放器。看起来很悲惨,不能轻易做到。在浏览了各种音乐应用程序的AndroidManifest.xml以寻找线索之后,我偶然发现了MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH。
使用下面的方法,只要歌曲在Android MediaStore中,我就可以在后台启动三星音乐播放器。您可以指定艺术家,专辑或标题。此方法也适用于Google Play音乐,但不幸的是,即使最新版本的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的解决方案,但此解决方案适用于我的应用程序。