I was trying to follow the steps in the reference but media player is not showing in the activity. So, how can I get rid of this issue?
onCreate
Method:
In here I've tried to add MediaController
after getting the VideoView
id
mVideoView = findViewById(R.id.video_view);
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
}
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(mVideoView);
playing video:
(In here, a method for initializing video, Using corresponded 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);
}
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());
}
}