Playing a video on Android device

119 views Asked by At

I searched every where every resource out there and I have tried almost everything, but for some reason my android device doesn't want to play the video on the application. I've tried changing the format to 3gp and mp4 and when I launch the application it just says the video cant play. The video is in my Phone I don't know if is the path that is wrong or something else!!!

Here's my code:

  protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView vid = (VideoView) findViewById(R.id.video);
        vid.setVideoPath("/Phone/Movies/INSTA_Segment_0_x264.mp4");
        vid.setMediaController(new MediaController(this));
        vid.start();
        vid.requestFocus();
    }
1

There are 1 answers

0
Fiido93 On

On Main Activity

package com.yournameproject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class yournameclass extends Activity {

private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the main layout of the activity
    setContentView(R.layout.activity_main);

    //set the media controller buttons
    if (mediaControls == null) {
        mediaControls = new MediaController(yournameclass.this);
    }

    //initialize the VideoView
    myVideoView = (VideoView) findViewById(R.id.video_view);

    // create a progress bar while the video file is loading
    progressDialog = new ProgressDialog(AndroidVideoViewExample.this);
    // set a title for the progress bar
    progressDialog.setTitle("Android Video View Example");
    // set a message for the progress bar
    progressDialog.setMessage("Loading...");
    //set the progress bar not cancelable on users' touch
    progressDialog.setCancelable(false);
    // show the progress bar
    progressDialog.show();

    try {
        //set the media controller in the VideoView
        myVideoView.setMediaController(mediaControls);

        //set the uri of the video to be played
        myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kitkat));

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    myVideoView.requestFocus();
    //we also set an setOnPreparedListener in order to know when the video file is ready for playback
    myVideoView.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mediaPlayer) {
            // close the progress bar and play the video
            progressDialog.dismiss();
            //if we have a position on savedInstanceState, the video playback should start from here
            myVideoView.seekTo(position);
            if (position == 0) {
                myVideoView.start();
            } else {
                //if we come from a resumed activity, video playback will be paused
                myVideoView.pause();
            }
        }
    });

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    //we use onSaveInstanceState in order to store the video playback position for orientation change
    savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
    myVideoView.pause();
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //we use onRestoreInstanceState in order to play the video playback from the stored position 
    position = savedInstanceState.getInt("Position");
    myVideoView.seekTo(position);
}
}

Hope this helpful