如何停止媒体播放器

how to stop Mediaplayer

我有 3 个音调。我正在调用 playmusic() 函数以在 imageview 切换时播放这些音调中的 2 个。当我第一次点击 imageview 时,tone1 开始播放。当我在同一个图像视图上第二次单击时,tone2 开始播放,但 tone1 没有停止。

我的要求是,当我点击 imageview 时 - (第一次 - tone1 应该开始播放)。 (第二次 - tone1 应该停止播放,tone2 应该开始播放) (第三次 - tone2 应该停止播放而 tone1 应该再次开始播放) 等等....

 String file=null;
 int x=0;
    public void playMusic(int i) {
        x=i;
        if(i==1){
            file = R.raw.tone1;
        }else if(i==2){
            file = R.raw.tone2;
        }else{
            file = R.raw.tone3;
        }
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        AssetFileDescriptor assetFileDescriptor = 
        Main2Activity.this.getResources().openRawResourceFd(file);
        if(assetFileDescriptor == null) return;
        try {
            mediaPlayer.setDataSource(
                    assetFileDescriptor.getFileDescriptor(),
                    assetFileDescriptor.getStartOffset(),
                    assetFileDescriptor.getLength()
            );
            assetFileDescriptor.close();

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    if(mediaPlayer != null){
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }
                }
            });

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(final MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.prepareAsync();

        } catch (IllegalArgumentException e) {
//                            HelpFunctions.showLog("ERROR = " + e);
        } catch (IllegalStateException e) {
//                            HelpFunctions.showLog("ERROR = " + e);
        } catch (IOException e) {
//                            HelpFunctions.showLog("ERROR = " + e);
        }
    }

when i click 2nd time on the same imageview tone2 starts playing, but tone1 is not stopped.

如果这是唯一的问题,请尝试在调用 mediaPlayer = new MediaPlayer(); 之前添加以下行:

if (mediaPlayer != null && mediaPlayer.isPlaying()) mediaPlayer.stop();

我对你的逻辑做了一点修改并添加了 mediaplayer.reset()。这是解决方案 link

public class MainActivity extends AppCompatActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final ImageView player = findViewById(R.id.play);
      player.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
             playMusic(1);
         }
       });

      mediaPlayer = new MediaPlayer();
    }

    int file = 0;
    MediaPlayer mediaPlayer;

    int x = 0;

    public void playMusic(int i) {
     x = x + i;
     if (x == 1) {
     file = R.raw.tone1;
   } 
    else if (x == 2) {
    file = R.raw.tone2;
   } else {
    x = 0;
    file = R.raw.tone3;
    }
mediaPlayer.reset();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
AssetFileDescriptor assetFileDescriptor =
    MainActivity.this.getResources().openRawResourceFd(file);
if (assetFileDescriptor == null) return;
try {
  mediaPlayer.setDataSource(
      assetFileDescriptor.getFileDescriptor(),
      assetFileDescriptor.getStartOffset(),
      assetFileDescriptor.getLength()
  );
  assetFileDescriptor.close();

  mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
      if (mediaPlayer != null) {
        mediaPlayer.release();
      }
    }
  });

  mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()      {
    @Override
    public void onPrepared(final MediaPlayer mediaPlayer) {
      mediaPlayer.start();
    }
  });
  mediaPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
  //                            HelpFunctions.showLog("ERROR = " + e);
   } catch (IllegalStateException e) {
  //                            HelpFunctions.showLog("ERROR = " + e);
} catch (IOException e) {
  //                            HelpFunctions.showLog("ERROR = " + e);
    }
  }
}