Example - How to used Media Controller in Android?
The MediaController introduced android APL Level 1. A view contain control for media player ,like play/Pause buttons and fast forward ,rewind and progress slider etc.
the mediacontroller default set of control and put them in window floating above your application.
activity_main.xml
...................................................................................................................................................................
..................................................................................................................................................................
MainActivity.java
..................................................................................................................................................................
activity_main.xml
...................................................................................................................................................................
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/my_heder_img"
android:layout_gravity="center_horizontal|top" />
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center" /> </FrameLayout>
..................................................................................................................................................................
MainActivity.java
..................................................................................................................................................................
package com.example.sarvesh.testproject;
import android.app.Activity; import android.app.ProgressDialog; 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 MainActivity 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); setContentView(R.layout.activity_main); if (mediaControls == null) { mediaControls = new MediaController(MainActivity.this); } myVideoView = (VideoView) findViewById(R.id.video_view); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("Android MediaController View Example"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(false); progressDialog.show(); try { myVideoView.setMediaController(mediaControls); myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.nicole_scherzinger_ft_pitbul)); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } myVideoView.requestFocus(); myVideoView.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { progressDialog.dismiss(); myVideoView.seekTo(position); if (position == 0) { myVideoView.start(); } else { myVideoView.pause(); } } }); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putInt("Position", myVideoView.getCurrentPosition()); myVideoView.pause(); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); position = savedInstanceState.getInt("Position"); myVideoView.seekTo(position); } }...................................................................................................................................................................
0 comments