Launching the default music player

允我心安 提交于 2019-12-21 21:44:05

问题


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



来源:https://stackoverflow.com/questions/14792018/launching-the-default-music-player

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