Launching the default music player

2019-04-14 11:16发布

问题:

I'm trying to launch the music player so it will launch and immediately start playing the first song. im using an Intent but it just doesn't work...it says "no activity found to handle intent".

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
       //"songsList" is an array with paths of all the songs in the sdcard
    Uri uri = Uri.parse(songsList.get(0));
    String type = "audio/mp3";
    intent.setDataAndType(uri, type);
    startActivity(intent);

回答1:

Why not use android.intent.action.MUSIC_PLAYER?

Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);

Note that this is deprecated from API 15, FROM API upwards you can use android.intent.category.APP_MUSIC.



回答2:

        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(AUDIO_PATH);
        intent.setPackage("com.google.android.music");          
        intent.setDataAndType(Uri.fromFile(file), "audio/*");
        mContext.startActivity(intent);


回答3:

To launch default music player, please try with below code.

try {
    Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC);
    getContext().startActivity(intent);
} catch (Exception e) {
    Log.d(TAG, "Exception for launching music player "+e);
}


回答4:

ok so i find this code that works

                Intent intent = new Intent();  
                intent.setAction(android.content.Intent.ACTION_VIEW);  
                File file = new File(songsList.get(0));  
                intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                startActivity(intent);

but the thing is that lets say that the user hits the back button and then hits the button of the music player, it restarts the player and starts playing the first song again... so how do i just launch the music player without anything else...?