How to start the default music player from my app?
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 mu开发者_开发百科sic player app of the user. How can I do this?
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.
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) {
}
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);
精彩评论