Challenge: Problem with VideoView and MediaController


#1

Has anyone tried playing the video with VideoView and MediaController? I tried but without much luck.
I did notice however that when instantiating MediaController, the compiler requires you to put in two parameters MediaController(Context,Token).
It is odd because the developer Android page did not list any constructor requiring this signature. I suspect that it requires min API of 21?
I had to create a new MediaSession and use getSessionToken to get the code to pass. Anyone know how to use VideoView & MediaController to play the video?

public class VideoContainerFragment extends Fragment {

    private Context mContext;
    private MediaPlayer   mPlayer;
    private MediaController mController;
    private VideoView mVideoView;
    private SurfaceView   mSurfaceView;
    private Button mPlayButton;
    private Button mPauseButton;
    private Button mStopButton;
    private MediaSession mSession;
    private static final String TAG ="debug";
    private static final String MEDIA_SESSION = "media_session";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        mContext = container.getContext();
        View v = inflater.inflate(R.layout.fragment_hello_moon_video, container, false);

   /**     mSurfaceView = (SurfaceView) v.findViewById(R.id.videoSurface);
        mSurfaceHolder = mSurfaceView.getHolder();  */

        mVideoView = (VideoView)v.findViewById(R.id.videoSurface);
        mSession = new MediaSession(mContext,MEDIA_SESSION);
        mController = new MediaController(getActivity(),mSession.getSessionToken());

...

#2

Hi all,

It turns out I imported the wrong MediaController. I should have imported:

import android.widget.MediaController;

instead of

import android.media.session.MediaController

here is what the result looks like

i.imgur.com/ixVAa7l.png?1

package com.bignerdranch.android.hellomoon;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;




public class VideoContainerFragment extends Fragment {

    private MediaPlayer   mPlayer;
    private MediaController mController;
    private VideoView mVideoView;
    private SurfaceView   mSurfaceView;
    private Button mPlayButton;
    private Button mPauseButton;
    private Button mStopButton;
    private static final String TAG ="debug";
    private static final String MEDIA_SESSION = "media_session";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View v = inflater.inflate(R.layout.fragment_hello_moon_video, container, false);

   /**     mSurfaceView = (SurfaceView) v.findViewById(R.id.videoSurface);
        mSurfaceHolder = mSurfaceView.getHolder();  */

        mVideoView = (VideoView)v.findViewById(R.id.videoSurface);
        mController = new MediaController(getActivity());


        Uri resourceUri = Uri.parse("android.resource://" + "com.bignerdranch.android.hellomoon/raw/apollo_17_stroll");

        mController.setAnchorView(mVideoView);
        mVideoView.setVideoURI(resourceUri);
        mVideoView.setMediaController(mController);

        mPlayButton = (Button)v.findViewById(R.id.hellomoon_video_playButton);
        mPlayButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){


            mVideoView.start();


             /**   if(mPlayer!=null) {
                    mPlayer.start();
                } else {
                    stop();
                    mPlayer = MediaPlayer.create(getActivity(), R.raw.apollo_17_stroll);
                   mPlayer.setDisplay(mSurfaceHolder);


                    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion(MediaPlayer mp) {
                            stop();
                        }
                    });

                    mPlayer.start();
                } */
                }

        });


        mPauseButton = (Button)v.findViewById(R.id.hellomoon_video_pauseButton);
        mPauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



           /**     if(mPlayer!=null) {
                    mPlayer.pause();
                } else {
                    ;
                }  */

            }
        });



        mStopButton = (Button)v.findViewById(R.id.hellomoon_video_stopButton);
        mStopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){


           /**     if(mPlayer != null){
                    mPlayer.release();
                    mPlayer = null;
                }  */
            }
        });



        return v;
    }

    class SurfaceHolderCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder)
        {


        }

        @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { }
        @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { }
    }

    class VideoCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mediaPlayer)
        {
            getActivity().finish();
        }
    }

    class VideoPreparedListener implements MediaPlayer.OnPreparedListener {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer)
        {

        }
    }

    public void stop() {
        if(mPlayer != null){
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayer.stop();
    }


}