gpt4 book ai didi

java - mediaplayer.isPlaying 始终为 false

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:23 26 4
gpt4 key购买 nike

我目前正在开发聊天应用程序。在此具有发送语音注释的功能。语音笔记的发送和播放工作正常。但问题是,当我播放语音笔记时,之前播放的语音笔记不会停止..两者一起播放。

这是我播放语音笔记的代码,

 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 总是为 false。

请帮助我!

最佳答案

来自documentation :

When the call to pause() returns, the MediaPlayer object enters the Paused state. Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content.

这里可能就是这种情况。声音没有停止的原因和 isPlaying() 返回 true 的原因可能不同。例如,多个 MediaPlayer 实例可能并行存在,将 mPlayer 设置为静态将排除此类选项。

关于java - mediaplayer.isPlaying 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46907866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com