Launching the default music player

2019-04-14 11:02发布

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);

4条回答
太酷不给撩
2楼-- · 2019-04-14 11:18

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.

查看更多
贪生不怕死
3楼-- · 2019-04-14 11:21
        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);
查看更多
对你真心纯属浪费
4楼-- · 2019-04-14 11:25

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...?

查看更多
Animai°情兽
5楼-- · 2019-04-14 11:36

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);
}
查看更多
登录 后发表回答