MediaPlayer的onCompletionListener叫暂停时 [英] MediaPlayer onCompletionListener called while paused
问题描述
,而它被暂停我的媒体播放器由于某种原因完成。现在,我已经看到在另一个帖子里说,大约是被异步和300ms的差异,我才明白。但是我的主要问题是,我有留在视频20秒,暂停时它完成20秒跑完了之后。
这里要说的是我可能会搞砸对相关code:
My media player for some reason completes while it is paused. Now i have seen another post that said about it being async and 300ms difference and that I understand. But my main problem is that i have 20 seconds left on the video and while paused it finishes after the 20 seconds run out. here is the relevant code that I might have messed up on:
private void initPlayer()
{
try {
if(videoFile != null)
{
afd = getAssets().openFd(videoFile);
instructionVideoPlayer = new MediaPlayer();
instructionVideoPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
instructionVideoPlayer.setDisplay(holder);
instructionVideoPlayer.prepare();
Log.i("Instruction Video", "videoPrepared");
instructionVideoPlayer.setOnCompletionListener(instructionVideoComplete);
instructionVideoPlayer.setOnPreparedListener(this);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
Log.i(this.toString(), "IOEXception");
e.printStackTrace();
//initPlayer();
} catch (Exception e)
{
Log.i("InitPlayer", e.getClass().toString());
e.printStackTrace();
}
restoreOverlayState();
}
监听器:
MediaPlayer.OnCompletionListener instructionVideoComplete = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
Log.i("onCompletion", "called");
if(!videoFile.contentEquals(LangSelect.INSTRUCTION_05))
{
Intent returnIntent = new Intent();
returnIntent.putExtra(LangSelect.ACTION, LangSelect.GO_VIDEO_NEXT);
setResult(RESULT_OK, returnIntent);
continuePlaying = true;
toFinish();
}
else
toFinish();
}
};
public void onPrepared(MediaPlayer mediaPlayer)
{
playVideo();
}
在播放和暂停功能
the play and pause functions
private void playVideo()
{
instructionVideoPlayer.seekTo(pausedAt);
instructionVideoPlayer.start();
restoreOverlayState();
}
private void pauseVideo()
{
pausedAt = instructionVideoPlayer.getCurrentPosition();
instructionVideoPlayer.pause();
}
private void restoreOverlayState()
{
prefs = getSharedPreferences(SAVE_PREFS, Context.MODE_PRIVATE);
if(prefs.getBoolean(IS_PAUSED, false))
{
pauseOverlay.setVisibility(View.VISIBLE);
pauseVideo();
}
else
{
pauseOverlay.setVisibility(View.GONE);
}
}
编辑:这只是发生在歌Nexus S(2.3.3)我测试的手机之一。其他的Galaxy S(升级Froyo)没有此错误。
this only happens on the Nexus S (2.3.3) one of the phones I am testing on. The other Galaxy S(Froyo) does not have this error.
推荐答案
我想通了,我在做什么错。该OnCompletionListener正在调用。问题是我跑2媒体播放器和同样onCompletionlistener意外设置它们为我打电话initPlayer两次。一旦在surfaceCreated并再次在onResume的末端()。无论是Galaxy S的被搞砸或已奇怪的错误检查becasue这个问题没有显示在那里。
I figured out what I was doing Wrong. the OnCompletionListener was being called. The problem was I was running 2 media players and setting the same onCompletionlistener to them accidentally as I was calling initPlayer twice. Once in surfaceCreated and again at the end of onResume(). Either the Galaxy S is messed up or it has weird error checking becasue this problem didnt show up there.
这篇关于MediaPlayer的onCompletionListener叫暂停时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!