如何停止Mediaplayer



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

我的要求是,当我点击-(第一次-音调1应该开始播放(。(第二次-音调1应停止播放,音调2应开始播放((第三次-音调2应停止播放,音调1应重新开始播放(等等…

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

我对您的逻辑做了一个小修改,并添加了mediaplayer.reset((

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

当我第二次单击同一图像时,视图tone2开始播放,但tone1没有停止。

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

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

相关内容

  • 没有找到相关文章

最新更新