Monday 10 August 2015

Playing video from url along with save instance of screen orientation- android

Playing video from url along with save instance of screen orientation


Enjoy!!!!!!!!


public class VideoviewActivity extends Activity {
//sharathyadhav

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

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
       
      if(bundle != null){
      Url = bundle.getString("url", "");// or url of your choice since i send from another activity i use this
      }

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

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

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

// create a progress bar while the video file is loading
progressDialog = new ProgressDialog(VideoviewActivity.this);
// set a title for the progress bar
//progressDialog.setTitle("JavaCodeGeeks 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));
myVideoView.setVideoPath(Url);
} 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);
}

}




xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:background="@color/black"
    android:layout_height="match_parent"  >
<VideoView 
    android:id="@+id/view"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    

</VideoView>
</RelativeLayout>

No comments:

Post a Comment