在 android,我成功播放了一个视频,但没有显示媒体控制器。下面进一步给出

In android, i was successful to play a video but media controller is not showing. Further given below

参考: https://google-developer-training.gitbooks.io/android-developer-advanced-course-practicals/unit-5-advanced-graphics-and-views/lesson-13-media/13-1-p-playing-video-with-videoview/13-1-p-playing-video-with-videoview.html

我试图按照参考中的步骤操作,但 activity 中未显示媒体播放器。那么,我怎样才能摆脱这个问题呢?

onCreate方法:

在这里我尝试在获取 VideoView id

后添加 MediaController
mVideoView = findViewById(R.id.video_view);
if (savedInstanceState != null) {
   mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
}
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(mVideoView);

playing video:

(这里是初始化video的方法,使用对应的Uri)

private void initializePlayer() {
    Uri videoUri = getMedia(VIDEO_SAMPLE);
    mVideoView.setVideoURI(videoUri);

    if (mCurrentPosition > 0) {
        mVideoView.seekTo(mCurrentPosition);
    } else {
        // Skipping to 1 shows the first frame of the video.
        mVideoView.seekTo(1);
        }
     mVideoView.start();
}


private Uri getMedia(String mediaName) {
   return Uri.parse("android.resource://" + getPackageName() +
                "/raw/" + mediaName);
}

您使用不当:

这样使用,

mVideoView = findViewById(R.id.video_view);
if (savedInstanceState != null) {
   mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
}
MediaController mediaController = new MediaController(this);
mVideoView.setMediaController(mediaController); // Change in this line.

结帐参考:VideoView

package com.example.sagar.videoplay;

import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    private static final String VIDEO_SAMPLE = "demo";
    private VideoView mVideoView;
    private int mCurrentPosition = 0;
    private static final String PLAYBACK_TIME = "play_time";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mVideoView = findViewById(R.id.video_view);
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
        }
        MediaController mediaController = new MediaController(this);
        mVideoView.setMediaController(mediaController);
    }

    //its a method which takes string as a input and return a url
    private Uri getMedia(String mediaName) {
        return Uri.parse("android.resource://" + getPackageName() +
                "/raw/" + mediaName);
    }

    private void initializePlayer() {
        Uri videoUri = getMedia(VIDEO_SAMPLE);
        mVideoView.setVideoURI(videoUri);

        if (mCurrentPosition > 0) {
            mVideoView.seekTo(mCurrentPosition);
        } else {
            // Skipping to 1 shows the first frame of the video.
            mVideoView.seekTo(1);
        }
        mVideoView.start();
    }

    private void releasePlayer() {
        mVideoView.stopPlayback();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initializePlayer();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releasePlayer();
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mVideoView.pause();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(PLAYBACK_TIME, mVideoView.getCurrentPosition());
    }
}