How to start the default music player from my app?

强颜欢笑 提交于 2019-12-13 13:55:18

问题


I make a app with a ListView. When I tap on a ListView item, a .ogg soundfile should start playing. Not in my app, but in the default music player app of the user. How can I do this?


回答1:


Try this

Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);

Or

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
Intent it = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(it);  

Extracted from http://snipt.net/Martin/android-intent-usage/

I haven't tested myself.




回答2:


dunno if this directly applies to the question, but here is a way to open the Play Music app from your own app:

Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.music");
if (i == null)
     throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}



回答3:


Try this:

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


来源:https://stackoverflow.com/questions/6265893/how-to-start-the-default-music-player-from-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!