我目前正在处理聊天应用程序。在此,有发送语音笔记的功能。语音笔记的发送和播放工作正常。但问题是,当我播放语音音符时,之前的语音音符不会停止播放......两者都在一起播放。
这是我播放语音留言的代码,
public static class MessageViewHolder extends RecyclerView.ViewHolder {
private TextView messageTextView;
private ImageView messageImageView;
private TextView timeTextView;
private LinearLayout textMessageLayout;
private TextView messengerNameTextView;
private CircleImageView messengerPhotoImageView;
ImageView leftQuackImage, rightQuackImage;
private LinearLayout voiceNotesLayout;
private ImageView playImageView, stopImageView;
private SeekBar progressSeekbar;
private MediaPlayer mPlayer;
double timeElapsed = 0, finalTime = 0;
Handler durationHandler = new Handler();
Runnable updateSeekBarTime;
boolean isPlaying;
public MessageViewHolder(View itemView) {
super(itemView);
messageTextView = (TextView) itemView.findViewById(R.id.messageTextView);
messageImageView = (ImageView) itemView.findViewById(R.id.messageImageView);
timeTextView = (TextView) itemView.findViewById(R.id.timeTextView);
textMessageLayout = (LinearLayout) itemView.findViewById(R.id.textMessageLayout);
messengerNameTextView = (TextView) itemView.findViewById(R.id.messengerTextView);
messengerPhotoImageView = (CircleImageView) itemView.findViewById(R.id.messengerImageView);
leftQuackImage = (ImageView) itemView.findViewById(R.id.leftQuackImage);
rightQuackImage = (ImageView) itemView.findViewById(R.id.rightQuackImage);
voiceNotesLayout = (LinearLayout) itemView.findViewById(R.id.voiceNotesLayout);
playImageView = (ImageView) itemView.findViewById(R.id.playImageView);
stopImageView = (ImageView) itemView.findViewById(R.id.stopImageView);
progressSeekbar = (SeekBar) itemView.findViewById(R.id.progressSeekbar);
isPlaying = false;
mPlayer = new MediaPlayer();
}
public void setupAudioPlay(final long duration, final String url){
progressSeekbar.setMax((int) duration);
progressSeekbar.setClickable(false);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
mPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}catch (IllegalStateException e){
e.printStackTrace();
}
playImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Media player","isPlaying"+mPlayer.isPlaying()+isPlaying);
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
if (!isPlaying) {
isPlaying = true;
mPlayer.start();
playImageView.setVisibility(View.GONE);
stopImageView.setVisibility(View.VISIBLE);
timeElapsed = mPlayer.getCurrentPosition();
progressSeekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
} else {
isPlaying = false;
mPlayer.pause();
}
}
});
stopImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlaying) {
if (stopPlaying()) {
playImageView.setVisibility(View.VISIBLE);
stopImageView.setVisibility(View.GONE);
}
}
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mp != null) {
mPlayer.seekTo(0);
isPlaying = false;
progressSeekbar.setProgress(0);
}
playImageView.setVisibility(View.VISIBLE);
stopImageView.setVisibility(View.GONE);
}
});
updateSeekBarTime = new Runnable() {
public void run() {
if (mPlayer != null) {
if (mPlayer.isPlaying()) {
timeElapsed = mPlayer.getCurrentPosition();
progressSeekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(this, 100);
} else {
mPlayer.pause();
isPlaying = false;
progressSeekbar.setProgress(0);
playImageView.setVisibility(View.VISIBLE);
stopImageView.setVisibility(View.GONE);
}
}
}
};
}
private boolean stopPlaying() {
mPlayer.pause();
mPlayer.seekTo(0);
if (!mPlayer.isPlaying()) {
return true;
}
return false;
}
}
这里mPlayer.isPlaying总是变错。
请帮助我!
答案 0 :(得分:2)
当对pause()的调用返回时,MediaPlayer对象进入Paused状态。请注意,从“已启动”状态到“暂停”状态的转换(反之亦然)在播放器引擎中异步发生。在调用isPlaying()时更新状态可能需要一些时间,对于流内容,它可能需要几秒钟。
这可能就是这种情况。声音未停止的原因以及为什么isPlaying()返回true可能会有所不同。例如,MediaPlayer的多个实例可能并行存在,这使得mPlayer static会排除这样的选项。
答案 1 :(得分:1)
您可以在必要时检查系统音频状态。在您的情况下,无论何时单击播放按钮,都应首先检查下面的条件。
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// to do
}
在要执行部分中,您应该实施以下主要操作:
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE);
audioManager.dispatchMediaKeyEvent(event);
您可以找到整个文档here