播放和停止按钮不能一起使用
Play and stop button doesn't work together
我的布局中有 2 个按钮。
1. Play/Pause
2.停止
播放和暂停按钮效果很好,但当停止按钮不起作用时。我实际上不知道问题从哪里开始。
这是我的代码
case R.id.playBtn:
if (mediaPlaying){
mediaPlaying = false;
mPlayer.pause();
playedLength = mPlayer.getCurrentPosition();
playButton.setBackgroundResource(R.drawable.play);
}else{
mediaPlaying = true;
if(mPlayer == null){
// create the media player
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlaying = false;
playedLength = 0;
playButton.setBackgroundResource(R.drawable.play);
}
});
try {
afd = getAssets().openFd(""+AUDIO+"");
mPlayer.setDataSource(afd.getFileDescriptor());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} else
// resume playing
mPlayer.seekTo(playedLength);
mPlayer.start();
playButton.setBackgroundResource(R.drawable.pause);
}
break;
case R.id.stopBtn:
if (mediaPlaying){
mediaPlaying = false;
if (mPlayer != null){
mPlayer.stop();
mPlayer = null;
playButton.setBackgroundResource(R.drawable.play);
}
}else{
mediaPlaying = true;
Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
if (mPlayer != null){
mPlayer.pause();
mPlayer.seekTo(0);
}
}
break;
我想做这个
当点击停止按钮时,完全停止播放器,无论是正在播放还是暂停。在播放时,如果我单击停止,播放器会重新启动,但当它暂停并单击停止时,它不会重新启动。
感谢任何帮助。
更新
case R.id.playBtn:
if (state == PlayerState.PLAYING){
mPlayer.pause();
playedLength = mPlayer.getCurrentPosition();
playButton.setBackgroundResource(R.drawable.play);
}else if (state == PlayerState.PLAYING){
if(mPlayer == null){
// create the media player
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlaying = false;
playedLength = 0;
playButton.setBackgroundResource(R.drawable.play);
}
});
try {
afd = getAssets().openFd(""+AUDIO+"");
mPlayer.setDataSource(afd.getFileDescriptor());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} else
// resume playing
mPlayer.seekTo(playedLength);
mPlayer.start();
playButton.setBackgroundResource(R.drawable.pause);
}
break;
case R.id.stopBtn:
if (state == PlayerState.STOPPED){
if (mPlayer != null){
mPlayer.stop();
mPlayer = null;
playButton.setBackgroundResource(R.drawable.play);
}
}else if (state == PlayerState.PAUSED){
Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
if (mPlayer != null){
mPlayer.pause();
mPlayer.seekTo(0);
}
}
break;
您遇到不同行为的原因是布尔值
mediaPlaying
将在单击停止按钮时具有不同的值。它只会在值为 true 时停止播放器,如果您在播放器暂停时点击停止,它将是 false
,因此您的代码不会停止播放器。
我建议您添加一个 PlayerState 枚举,例如(PLAYING, STOPPED, PAUSED) 而不是使用布尔值。这样你就更容易知道玩家处于什么状态,应该采取什么行动。
在代码中
public enum PlayerState {
PLAYING,
STOPPED,
PAUSED
}
然后您创建变量并设置初始状态(与您对 mediaPlaying 所做的相同(并且在您的应用程序中的相同位置))
private PlayerState state = PlayerState.STOPPED;
然后在您的代码中替换您操作 mediaPlaying 的地方,并将 if(mediaPlaying) 替换为 if 语句,例如
if (state == PlayerState.PLAYING) { ...
一旦完成,您就不再需要布尔媒体播放了。而且您对应用程序中的媒体播放器状态有更详细的了解。
某种状态代码,很难,因为我没有所有代码。在每种情况下检查状态转换,然后设置状态(您的代码中缺少)
case R.id.playBtn:
if (state == PlayerState.PLAYING){
// logic here for going from playing to paused
state = PlayerState.PAUSED;
} else if (state != PlayerState.PLAYING){
// logic from non playing to playing
state = PlayerState.PLAYING;
}
break;
case R.id.stopBtn:
if (state == PlayerState.PLAYING){
// logic if playing
} else if (state == PlayerState.PAUSED){
// logic if paused
}
state = PlayerState.STOPPED;
break;
我的布局中有 2 个按钮。 1. Play/Pause 2.停止 播放和暂停按钮效果很好,但当停止按钮不起作用时。我实际上不知道问题从哪里开始。
这是我的代码
case R.id.playBtn:
if (mediaPlaying){
mediaPlaying = false;
mPlayer.pause();
playedLength = mPlayer.getCurrentPosition();
playButton.setBackgroundResource(R.drawable.play);
}else{
mediaPlaying = true;
if(mPlayer == null){
// create the media player
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlaying = false;
playedLength = 0;
playButton.setBackgroundResource(R.drawable.play);
}
});
try {
afd = getAssets().openFd(""+AUDIO+"");
mPlayer.setDataSource(afd.getFileDescriptor());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} else
// resume playing
mPlayer.seekTo(playedLength);
mPlayer.start();
playButton.setBackgroundResource(R.drawable.pause);
}
break;
case R.id.stopBtn:
if (mediaPlaying){
mediaPlaying = false;
if (mPlayer != null){
mPlayer.stop();
mPlayer = null;
playButton.setBackgroundResource(R.drawable.play);
}
}else{
mediaPlaying = true;
Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
if (mPlayer != null){
mPlayer.pause();
mPlayer.seekTo(0);
}
}
break;
我想做这个 当点击停止按钮时,完全停止播放器,无论是正在播放还是暂停。在播放时,如果我单击停止,播放器会重新启动,但当它暂停并单击停止时,它不会重新启动。 感谢任何帮助。
更新
case R.id.playBtn:
if (state == PlayerState.PLAYING){
mPlayer.pause();
playedLength = mPlayer.getCurrentPosition();
playButton.setBackgroundResource(R.drawable.play);
}else if (state == PlayerState.PLAYING){
if(mPlayer == null){
// create the media player
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlaying = false;
playedLength = 0;
playButton.setBackgroundResource(R.drawable.play);
}
});
try {
afd = getAssets().openFd(""+AUDIO+"");
mPlayer.setDataSource(afd.getFileDescriptor());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} else
// resume playing
mPlayer.seekTo(playedLength);
mPlayer.start();
playButton.setBackgroundResource(R.drawable.pause);
}
break;
case R.id.stopBtn:
if (state == PlayerState.STOPPED){
if (mPlayer != null){
mPlayer.stop();
mPlayer = null;
playButton.setBackgroundResource(R.drawable.play);
}
}else if (state == PlayerState.PAUSED){
Toast.makeText(getApplicationContext(), "Added to favorites.", Toast.LENGTH_SHORT).show();
if (mPlayer != null){
mPlayer.pause();
mPlayer.seekTo(0);
}
}
break;
您遇到不同行为的原因是布尔值
mediaPlaying
将在单击停止按钮时具有不同的值。它只会在值为 true 时停止播放器,如果您在播放器暂停时点击停止,它将是 false
,因此您的代码不会停止播放器。
我建议您添加一个 PlayerState 枚举,例如(PLAYING, STOPPED, PAUSED) 而不是使用布尔值。这样你就更容易知道玩家处于什么状态,应该采取什么行动。
在代码中
public enum PlayerState {
PLAYING,
STOPPED,
PAUSED
}
然后您创建变量并设置初始状态(与您对 mediaPlaying 所做的相同(并且在您的应用程序中的相同位置))
private PlayerState state = PlayerState.STOPPED;
然后在您的代码中替换您操作 mediaPlaying 的地方,并将 if(mediaPlaying) 替换为 if 语句,例如
if (state == PlayerState.PLAYING) { ...
一旦完成,您就不再需要布尔媒体播放了。而且您对应用程序中的媒体播放器状态有更详细的了解。
某种状态代码,很难,因为我没有所有代码。在每种情况下检查状态转换,然后设置状态(您的代码中缺少)
case R.id.playBtn:
if (state == PlayerState.PLAYING){
// logic here for going from playing to paused
state = PlayerState.PAUSED;
} else if (state != PlayerState.PLAYING){
// logic from non playing to playing
state = PlayerState.PLAYING;
}
break;
case R.id.stopBtn:
if (state == PlayerState.PLAYING){
// logic if playing
} else if (state == PlayerState.PAUSED){
// logic if paused
}
state = PlayerState.STOPPED;
break;