问题
My app show list of song from sd card. I want to be able to play the song on the default player. I've got all the data about the song: id, title, album, artist, path...
Is there a way to launch the default player to play the song?
What I've tried:
Using
Intent.CATEGORY_APP_MUSIC
. I can launch the default player, but can't set the song.Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC)
open the default music app. Howeverintent.setData(Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id))
throws exception that intent not found.Using deprecated
MediaStore.INTENT_ACTION_MUSIC_PLAYER
. Activity not found.Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER).setDataAndType(Uri.fromFile(songFile), "audio/*")
- Using
INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
. Launch the search, but not the song, says couldn't prepare mix on Play music. However, it works from Google now, so it may be the key.Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).putExtra(SearchManager.QUERY, name)
- Using
ACTION_VIEW
. it works but it launch a compact version of the player, and I want the full player.

Note: I want to launch External player, not in my app.
Update: Looks like Google Now hardcoded Play Music and Youtube support.
回答1:
If you would like to start the default music player app on the device, should try this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
回答2:
it is launching a compact version for playing as that app has implemented it that way, you can find many 3rd party app that open the full app with
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
as you already tried this
回答3:
Try with something simple like this:
MediaPlayer mp = new MediaPlayer();
mp.setLooping(true);
try {
mp.setDataSource(mFile); //mFile is the path to your mp3 file
} catch (Exception e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mp.start();
And one more example with MadiaPlayer and file saved in res/raw/ directory:
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
More about: http://developer.android.com/guide/topics/media/mediaplayer.html
回答4:
Intent musicIntent = new Intent();
//use Action VIEW to launch app
musicIntent.setAction(Intent.ACTION_VIEW);
String mimeType = getMimeTypeFromFile(); // in your case it is audio
Uri uri = " yourfile uri";
musicIntent.setDataAndType(uri,mimeType);
startActivity(intent);
The default view application will be opened if the mime type is changed, for video mime-type video player will be opened, for audio music player and for image gallery.
If there are multiple player then Resolver
activity will be opened.
Don’t forget to catch ActivityNotFoundException
In Music player for ACTION_VIEW AudioPreview activty is called with priority. there are other activities which handle this action , just check once with action "com.android.music.PLAYBACK_VIEWER
" instead of ACTION_VIEW or with it.
来源:https://stackoverflow.com/questions/30937370/play-song-on-default-music-player-android