Ehsan
Ehsan

Reputation: 2781

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>

Upvotes: 7

Views: 21929

Answers (2)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

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

Upvotes: 23

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

try this:

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

    mediaC  = new MediaController(this);
    mediaC.setAnchorView(videoView);
    videoView.setMediaController(mediaC);

    btnPaly.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

        videoPlayer.setVideoPath(videoPath);
        videoPlayer.setMediaController(mediaC);
        mediaC.setAnchorView(view); // I get Null piont exception here
        videoPlayer.start();
      }
    });

  }
}

Upvotes: 0

Related Questions