如何暂停并播放音乐播放器

时间:2021-08-24 18:56:24

I want to write a program which can pause and play every media players in android. How can i set priority of this program higher than the others? also i wrote the following code

我想写一个程序,可以暂停并播放android中所有的媒体播放器。我怎样才能把这个程序的优先级设置得比其他程序高?我还编写了以下代码

public static final String SERVICECMD = "com.android.music.musicservicecommand";
    public static final String CMDNAME = "command";
    public static final String CMDPAUSE = "pause";
    public static final String CMDPLAY = "play";
    public static final String CMDTOGGLEPAUSE = "togglepause";

  AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (mAudioManager.isMusicActive()) {

            Intent i = new Intent(SERVICECMD);
            i.putExtra(CMDNAME, CMDPAUSE);
            MainActivity.this.sendBroadcast(i);
            Toast.makeText(this, "media player is pause!", Toast.LENGTH_LONG).show();

        }
        if (!mAudioManager.isMusicActive()) {
            Intent i = new Intent(SERVICECMD);


            i.putExtra(CMDNAME, CMDPLAY);
            MainActivity.this.sendBroadcast(i);

            Toast.makeText(this, "media player is play!", Toast.LENGTH_LONG).show();
        }

and it can pause every players but i dont know how to play them again. I can play some players but not all of them i dont know why ???

它可以让每个球员暂停,但我不知道如何再和他们比赛。我可以打一些球员,但不是所有的我不知道为什么?

1 个解决方案

#1


1  

Have an onClick for a play and pause button

有一个onClick播放和暂停按钮

You'll have to find a way to retrieve all of the MediaPlayer Objects.

您必须找到一种方法来检索所有MediaPlayer对象。

Pausing:

暂停:

mMediaPlayer.pause(); // Pause the MediaPlayer

Playing:

玩:

mMediaPlayer.prepare(); // Prepares the MediaPlayer to play a song

** Please have an onPrepare method as well

**也请有一个onPrepare方法

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
              mediaPlayer.start(); // play!
}

Also, here's a reference for you: https://github.com/naman14/Timber

另外,这里还有一个引用:https://github.com/naman14/Timber

This open source music app should have most of the relevant resources you need to creating a music player controller or etc. You'll be able to understand how it works.

这个开源的音乐应用程序应该拥有创建音乐播放器控制器等所需的大部分相关资源。您将能够理解它是如何工作的。

Also, the code you've presented won't allow me to help you with identifying any issues unless you have not implemented the MediaPlayer Object.

另外,您提供的代码不会允许我帮助您识别任何问题,除非您没有实现MediaPlayer对象。

Here's why: Difference between Audiomanager and MediaPlayer

原因如下:音频管理器和MediaPlayer之间的差异

-- EDIT --

——编辑

Here's how you can stop other MediaPlayer instances via the MediaPlayerRegistry.

以下是通过MediaPlayerRegistry阻止其他MediaPlayer实例的方法。

Android: How to stop other active media players?

Android:如何阻止其他活跃的媒体玩家?

#1


1  

Have an onClick for a play and pause button

有一个onClick播放和暂停按钮

You'll have to find a way to retrieve all of the MediaPlayer Objects.

您必须找到一种方法来检索所有MediaPlayer对象。

Pausing:

暂停:

mMediaPlayer.pause(); // Pause the MediaPlayer

Playing:

玩:

mMediaPlayer.prepare(); // Prepares the MediaPlayer to play a song

** Please have an onPrepare method as well

**也请有一个onPrepare方法

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
              mediaPlayer.start(); // play!
}

Also, here's a reference for you: https://github.com/naman14/Timber

另外,这里还有一个引用:https://github.com/naman14/Timber

This open source music app should have most of the relevant resources you need to creating a music player controller or etc. You'll be able to understand how it works.

这个开源的音乐应用程序应该拥有创建音乐播放器控制器等所需的大部分相关资源。您将能够理解它是如何工作的。

Also, the code you've presented won't allow me to help you with identifying any issues unless you have not implemented the MediaPlayer Object.

另外,您提供的代码不会允许我帮助您识别任何问题,除非您没有实现MediaPlayer对象。

Here's why: Difference between Audiomanager and MediaPlayer

原因如下:音频管理器和MediaPlayer之间的差异

-- EDIT --

——编辑

Here's how you can stop other MediaPlayer instances via the MediaPlayerRegistry.

以下是通过MediaPlayerRegistry阻止其他MediaPlayer实例的方法。

Android: How to stop other active media players?

Android:如何阻止其他活跃的媒体玩家?