有没有播放媒体与默认媒体播放器的方法吗? 我可以用下面的代码做到这一点:
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开始在后台三星的音乐播放器。 你可以指定艺术家,专辑或标题。 这种方法也适用于谷歌播放音乐可惜即使是普通的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一个解决方案,但这种解决方案适用于我的应用程序。