Start default music player with music playing by d

2019-06-01 03:19发布

I am developing an application where I need to start the default music app and play all the songs. I have tried a number of approaches, but nothing seems to work.

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(
                          "com.android.music.MediaPlaybackActivityStarter");
startActivity( LaunchIntent );

and

Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.music",
    "com.android.music.MediaPlaybackActivity");
intent.setComponent(comp);
intent.setAction(Intent.ACTION_RUN);
startActivity(intent);

Just starts the music player

Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  "1");
startActivity(i);

Plays just the first song.

4条回答
小情绪 Triste *
2楼-- · 2019-06-01 03:41

Use this for api below 15

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

and later use android.intent.category.APP_MUSIC.

查看更多
Evening l夕情丶
3楼-- · 2019-06-01 03:45

To simply launch the music player do:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

please note, that this works only in SDK API level 8 up

查看更多
我命由我不由天
4楼-- · 2019-06-01 03:50

I think you should implement your playlist which you can pass your list to mediaplayer and set your next song in mediaplayer.onCompletion ().

player.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d("ON COMPLETE", "true");
                    player.isCompleteTrack = true;
                    if (mPlaylist.isLastTrack()) {

                        Log.i(PLAYER_ENGINE_TAG, " end playlist");
                        stop();
                    } else {

                        Log.i(PLAYER_ENGINE_TAG, " next song");
                        next();
                    }
                }
            });

Next()

@Override
    public void next() {

        if (!mPlaylist.isEmpty()) {

            mPlaylist.setSelectedMedia(mPlaylist.getSelectedMedia() + 1);
        }

        play();
    }

Or you can try open m3u (playlist) file.

查看更多
欢心
5楼-- · 2019-06-01 04:04

I recently also started getting this issue after some security updates. To solve this you need to disable all notifications from the default music player. Do this by going to Settings/Apps & Notifications/Apps/Music Then make sure all notifications are set to off and also block the app from accessing your storage.

查看更多
登录 后发表回答