VideoView Example

In this Example, We play a video on this activity with the help of media controllers. Toast Messages deliver if error occurs and video will finish.

actvity_main.xml

This xml shows only one videoview in this layout.

 




    



MainActivity.java

This activity combines media controllers and videoview. We set the uri for video and perform set on error and completion listener events and display toast message when video is completed or an error is occur while playing the video

package com.example.icowboysradio.myapplication;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivityVideo extends Activity {

    VideoView videoView;
    MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        videoView = (VideoView) findViewById(R.id.videoView);

        if (mediaController == null) {
            mediaController = new MediaController(MainActivityVideo.this);
            mediaController.setAnchorView(videoView);
        }

        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.wildlife));
        videoView.start();

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(getApplicationContext(), "Thank You...!!!", Toast.LENGTH_LONG).show();
            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(getApplicationContext(), "Error Occurs on Play,...", Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }
}