I am using a video view and I want to set a media controller for it. I did this code but when I click on play button the app crashes with a NullPointerException. I don't know why because I define everything truly.
When I cut mediaC.setAnchorView(view);
the video plays truly but media controller doesn't appear.
Thank you for your answer.
public class VideoActivity extends Activity {
Button btnPaly;
VideoView videoPlayer;
MediaController mediaC;
String videoPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
btnPaly = (Button) findViewById(R.id.btnPlay);
videoPlayer = (VideoView) findViewById(R.id.videoView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
videoPath = bundle.getString(Const.VIDEO_PATH);
}
btnPaly.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
videoPlayer.setVideoPath(videoPath);
videoPlayer.setMediaController(mediaC);
mediaC.setAnchorView(view); // I get NullPointerException here
videoPlayer.start();
}
});
}
}
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
tools:context="simplevideoplayer.app.codenevisha.com.videoplayer.VideoActivity">
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="75dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="-1dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.342"/>
<VideoView
android:id="@+id/videoView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnPlay"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
Tap the video you'd like to watch. At the bottom of the video player, tap full screen .
Step 2: Open res -> layout -> xml (or) main. xml and add following code : In this step we open an xml file and add the code to display a VideoView in our activity. In this step we open MainActivity and add the code to initiate the video view and create an object of MediaController to control the video playback.
Introducing the Android MediaController Class The MediaController will then provide a set of controls allowing the user to manage the playback (such as pausing and seeking backwards/forwards in the video timeline).
You forgot to instantiate the MediaController
, so it's null.
Try this in your onCreate()
method (pay attention to the first line):
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With