我嘗試使用MediaPlayer(Android ICS)循環播放2個視頻並每次交換它們。Android - MediaPlayer freeze/surfaceview
它的偉大工程,但隨機時間後申請凍結和我得到的錯誤:
[SurfaceView] connect : already connected (cur=3, req=3)
setVideoSurfaceTexture failed : -22
這裏是我的代碼:
public void onCreate(Bundle savedInstanceState) {
...
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(currentPath);
mMediaPlayer.setDisplay(mPreview.getHolder());
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.prepare();
} catch (Exception e) {
}
public void onPrepared(MediaPlayer mp) {
mp.start();
}
public void onCompletion(MediaPlayer mp) {
try {
mMediaPlayer.reset();
if (mCurrentPath == mPath1)
mCurrentPath = mPath2;
else
mCurrentPath = mPath1;
mMediaPlayer.setDataSource(mCurrentPath);
mMediaPlayer.prepare();
} catch (Exception e) {
}
}
任何想法?
什麼是'mCurrentPath','mPath1'和'mPath2'?這可能與你的問題無關,但如果它們是'String',那麼你不應該在'onCompletion(...)'中使用'=='來比較它們。改用'if(mCurrentPath.equals(mPath1))'。 – Squonk
確切:) mPath1&2是字符串。 - 實際上凍結髮生時也沒有交換視頻和循環播放相同的視頻 – iam
好吧,對不起,我看不出其他代碼有什麼根本性錯誤,但只是你知道,用'=='比較'String's在Java中會導致問題(通常它只是不起作用)。 – Squonk