6

我必须使用 MediaPlayer 播放视频,而不是 VideoView。我在 Xml 布局中有一个 SurfaceView,在 Activity 端有一个 MediaPlayer 和一个 MediaController。

我找不到将 MediaController 与 SurfaceView 和 MediaPlayer 一起使用的方法。使用 VideoView 非常简单,但无法弄清楚如何使用 MediaPlayer。

我在网上找到了一些例子,但它们都是关于播放音频,而不是视频。

这是我创建 MediaPlayer 的 surfaceCreated 方法

@Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setDataSource(this, Uri.parse(mUrl));
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.prepare();
            mMediaController = new MediaController(this);

        } catch (Exception e) {
            Log.e(TAG, "MediaPlayer Prepare: " + e.getMessage());
        }
    }

和这个 MediaPlayer onPrepared 方法

@Override
    public void onPrepared(MediaPlayer mp) {

        mVideoWidth = mp.getVideoWidth();
        mVideoHeight = mp.getVideoHeight();
        mSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
        startVideoPlayback();

        mMediaController.setMediaPlayer(this);              

        handler.post(new Runnable() {

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

上面的代码没有在 SurfaceView 上显示 MediaController。
我怎样才能做到这一点?

提前致谢。

4

1 回答 1

15

为此,您必须实施活动

OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl

检查下面的工作代码

package com.example.demo;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements  OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl{
    String url = "your video url";
    MediaPlayer mMediaPlayer ;
    SurfaceView mSurfaceView ;
    SurfaceHolder holder ;
    private ConstantAnchorMediaController controller = null;
    ProgressBar progressBar1 ;
    MediaController mcontroller ;
    Handler handler ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
        SurfaceView v = (SurfaceView) findViewById(R.id.surface); 
        handler = new Handler();
        holder = v.getHolder(); 
        holder.addCallback(this);
        playVideo();

    }
    private void playVideo() {
        try {
            mcontroller = new MediaController(this);
            mMediaPlayer = MediaPlayer.create(this, Uri.parse(url));
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
         mcontroller.show();
         return false;
    }
    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        mMediaPlayer.setDisplay(holder); 
        try {
            mMediaPlayer.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        progressBar1.setVisibility(View.GONE);
        mcontroller.setMediaPlayer(this);
        mcontroller.setAnchorView(findViewById(R.id.surface));
        mcontroller.setEnabled(true);

        handler.post(new Runnable() {
            public void run() {
                mcontroller.show();
            }
        });
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // TODO Auto-generated method stub
    }
    public void start() {
        mMediaPlayer.start();
    }

    public void pause() {
        mMediaPlayer.pause();
    }

    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    public void seekTo(int i) {
        mMediaPlayer.seekTo(i);
    }

    public boolean isPlaying() {
        return mMediaPlayer.isPlaying(); 
    }

    public int getBufferPercentage() {
        return 0;
    }

    public boolean canPause() {
        return true;
    }

    public boolean canSeekBackward() {
        return true;
    }

    public boolean canSeekForward() {
        return true;
    }
}
于 2013-08-08T20:16:10.390 回答