gpt4 book ai didi

android - Mediaplayer setOnCompletionListener 重复调用

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:10 26 4
gpt4 key购买 nike

在我的应用程序中,我在 ListView 中显示我的音频文件,当单击列表项时,必须使用搜索栏在 ListView 单元格内播放特定文件。但现在我想在播放结束时更新用户界面。我用过 onCompletionListener,但它在播放开始时重复调用。 这是我的代码

private void startPlayBack(final MessagesAdapter.ViewHolder viewHolder){

try {

if(mMediaPlayer!=null){
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
}
mMediaPlayer.reset();
mMediaPlayer.setDataSource(viewHolder.mChatMessage.mMessages);
mMediaPlayer.prepareAsync();

} catch (IOException e) {
Log.i("audioplay", "IOException *************",e);
}catch (IllegalStateException e) {
Log.i("audioplay", "IllegalStateException *************",e);
}

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {

viewHolder.mAudioPlayBtn.setVisibility(View.VISIBLE);
viewHolder.mAudioPlayBtn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.audio_close));
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
}
});

mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.i("Completion Listener", "audio Completed");
}
});

viewHolder.mAudioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mMediaPlayer != null && fromUser) {
mMediaPlayer.seekTo(progress * 1000);
mMediaPlayer.start();
}
}
});

seekUpdation(viewHolder);
}

这是得到的日志信息,

08-20 07:28:24.099  14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.099 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.099 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.103 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.107 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.107 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:25.739 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.415 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.415 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.419 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.419 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:27.323 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:27.959 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed

最佳答案

ViewPager 中使用 MediaPlayers 时,我遇到了类似的问题。结果是 MediaPlayers 遇到错误,导致调用 OnCompletionListener。来自OnErrorListener documentation :

Returns

True if the method handled the error, false if it didn't. Returning false, or not having an OnErrorListener at all, will cause the OnCompletionListener to be called.

为您的 MediaPlayers 实现一个 OnErrorListener 以防止对 OnCompletionListener 的无关调用。

关于android - Mediaplayer setOnCompletionListener 重复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112261/

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