Android媒体播放器不会停止.当我点击播放按钮时,它会播放多次



当我一次又一次点击播放按钮时,它会同时播放多次。我想停止多重播放。这是代码:

媒体播放器和按钮的对象


MediaPlayer mPlayer;
Button playbtn;
Button stopbtn;

按钮点击事件监听器播放音频


playbtn = (Button) findViewById(R.id.play);
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();


按钮点击事件监听器以停止音频


stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});

如有任何帮助,我们将不胜感激。谢谢

它每次点击播放都会创建一个新玩家,但只保留对最后一个玩家的引用,因此解决方案是将玩家保留在列表中并停止所有玩家。

private List<MediaPlayer> players = new ArrayList<>()

stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for(player in players){
if(player!=null && player.isPlaying()){
mPlayer.stop();
}
}
players.clear();
}
});

另一个解决方案是,只使用播放器的一个实例。将播放器初始化移动到外部点击为:

playbtn = (Button) findViewById(R.id.play);
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}

try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!mPlayer.isPlaying())
mPlayer.start();

最新更新