如何使用下面的MediaController创建videoView

时间:2014-07-21 12:20:21

标签: android mobile android-videoview mediacontroller

我正在以编程方式创建videoView,它需要保持原样

    videoView = new VideoView(context);

    mediaController = new MediaController(context);
    mediaController.findFocus();
    mediaController.setEnabled(true);
    mediaController.show(0);

    String url = "android.resource://" + context.getPackageName() + "/"
            + R.raw.video;

    videoView.setVideoURI(Uri.parse(url));
    videoView.requestFocus();
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            (ViewSizeUtl.getWidthOfDisplay(context)) * 2 / 3,
            (ViewSizeUtl.getHeightOfDisplay(context) * 2 / 3));
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,
            RelativeLayout.TRUE);
    videoView.setLayoutParams(layoutParams);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    addView(videoView);

我需要mediaController保持在viewView的下方。

1 个答案:

答案 0 :(得分:4)

在媒体播放器中添加媒体控制器非常简单。使您的活动实现MediaPlayerControl并添加未实现的方法。代码如下:

我使用的是来自此处的Api演示代码http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

public class ActivityVedioPlay extends Activity implements
        OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
        OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl {
.....

    private MediaPlayer mMediaPlayer;
    private MediaController mcontroller;
    private Handler handler = new Handler();
.....
@Override
    public boolean onTouchEvent(MotionEvent event) {
        /*
         * the MediaController will hide after 3 seconds - tap the screen to
         * make it appear again
         */
        mcontroller.show();
        return false;
    }
......
private void playVideo() {
        doCleanUp();
        try {
            path = getIntent().getStringExtra("url");
            if (path == "") {
                Toast.makeText(ActivityVedioPlay.this, "URL Not found",
                        Toast.LENGTH_LONG).show();
            }
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mcontroller = new MediaController(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
......

public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
        mcontroller.setMediaPlayer(this);
        mcontroller.setAnchorView(findViewById(R.id.mediaplayer_surfaceview_container));
        handler.post(new Runnable() {

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

//mediacontroller implemented methods

    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;
    }