Android:MediaPlayer在3秒后崩溃

时间:2012-02-10 19:53:08

标签: android android-mediaplayer

我正在尝试使用MediaPlayer和MediaController播放视频。视频开始,三秒后,它崩溃了。我相信这是因为视频控制在视频启动后隐藏了三秒钟。例外情况说“抽象方法没有实现View.onControllerHide()等。我已经实现了我应该的所有类,我找不到任何对onControllerHide()方法的引用。任何事情都会有所帮助。下面的代码(我省略了不相关的代码,以便于阅读)。谢谢!

public class LessonView extends Activity implements OnTouchListener, OnPreparedListener,        SurfaceHolder.Callback, 
MediaController.MediaPlayerControl, OnVideoSizeChangedListener, OnCompletionListener, OnBufferingUpdateListener  {

private static final String TAG = "LessonView";
private MediaPlayer mMediaPlayer;
private MediaController mMediaController;
private Handler handler;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String uri;
private int mVideoWidth = 0;
private int mVideoHeight = 0;
private boolean mIsVideoReadyToBePlayed, mIsVideoSizeKnown; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // remove status bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.lessonview);

    ImageView backTabImg = (ImageView) findViewById(R.id.backTabImageView);
    backTabImg.setOnTouchListener(this);

    mPreview.setOnTouchListener(this);

    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    uri = Globals.currentLesson.getDownloadURL();
    handler = new Handler();
    changeBackground();
}

private void playVideo() {
    Log.v(TAG, "playVideo");
    // Create a new media player and set the listeners
    doCleanUp();

    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(uri);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaController = new MediaController(this);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }        
}

public boolean onTouch(View v, MotionEvent event) { 
    Log.v(TAG, "onTouch");
    int position = v.getId();  

    if(position == R.id.backTabImageView) {
        finish();
        return true;   
    }
    if(position == R.id.lessonSurfaceView) {
        Log.v(TAG, "touch event");
        mMediaController.show();
        return true;            
    }
    return false;
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.v(TAG, "onSurfaceCreated");
    playVideo();
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.v(TAG, "onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

@Override
public void onPrepared(MediaPlayer mp) {
    Log.v(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
    mMediaController.setMediaPlayer(this);
    mMediaController.setAnchorView(findViewById(R.id.lessonSurfaceView));

    handler.post(new Runnable() {

    public void run() {
            mMediaController.setEnabled(true);
            mMediaController.show();
    }});
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mMediaPlayer.start();
}

1 个答案:

答案 0 :(得分:3)

尝试实现onControllerHide()方法,看看会发生什么。