Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set MediaController for a VideoView

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>
like image 913
Ehsan Avatar asked Apr 26 '17 09:04

Ehsan


People also ask

How do I make Android videos full screen?

Tap the video you'd like to watch. At the bottom of the video player, tap full screen .

How to display a video in android Studio?

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.

What is the use of media controller class in Android?

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).


1 Answers

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);
like image 198
Bhupat Bheda Avatar answered Oct 21 '22 15:10

Bhupat Bheda