启动默认音乐播放器

时间:2013-02-09 21:53:30

标签: android android-intent android-music-player

我正在尝试推出音乐播放器,以便它能够启动并立即开始播放第一首歌曲。即时通讯使用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 个答案:

答案 0 :(得分:2)

为什么不使用android.intent.action.MUSIC_PLAYER

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

请注意,这已从API 15弃用,FROM API向上可以使用android.intent.category.APP_MUSIC

答案 1 :(得分: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);

答案 2 :(得分:2)

要启动默认音乐播放器,请尝试使用以下代码。

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

答案 3 :(得分:1)

好的,所以我发现这个代码有效

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

但问题是让用户点击后退按钮,然后按下音乐播放器的按钮,它重新启动播放器并再次开始播放第一首歌曲... 那么我怎么才能在没有其他任何东西的情况下推出音乐播放器......?

相关问题