Purpose: There is some circumstances When we do not want display inbuilt seek-bar of video for forward and back video then this is best way to play video.
Main.Xml:
Class For Play video:
Main.Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/video_top_lyt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:background="@drawable/done" >
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Play Video" />
</RelativeLayout>
<SurfaceView
android:id="@+id/screen_tutorial_video_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</SurfaceView>
</RelativeLayout>
Class For Play video:
public class PlayLiveTv extends BottomMenu_Event implementsSurfaceHolder.Callback
{
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String videoPath ="Video URl here...";
private static ProgressDialog progressDialog;
RelativeLayout rel;
@Override
protected void onPause()
{
super.onPause();
releaseMediaPlayer();
}
@Override
protected void onDestroy()
{
super.onDestroy();
releaseMediaPlayer();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_livetv);
progressDialog = ProgressDialog.show(PlayLiveTv.this, "","Loading.....", true);
mPreview = (SurfaceView) findViewById(R.id.screen_tutorial_video_surface);
rel=(RelativeLayout) findViewById(R.id.video_top_lyt);
Create_Surface();
mPreview.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(rel.isShown())
{
rel.setVisibility(View.GONE);
}
else
{
rel.setVisibility(View.VISIBLE);
}
return false;
}
});
}
//_______________________Methods___________________________
private void Create_Surface()
{
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(getWindow().getWindowManager().getDefaultDisplay().getWidth());
//holder.setFixedSize(100, 100);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(holder);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height)
{
}
public void surfaceCreated(SurfaceHolder holder)
{
Play_Video();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
releaseMediaPlayer();
}
private void Play_Video()
{
try
{
if(videoPath!=null)
{
mMediaPlayer.setDataSource(videoPath);
mMediaPlayer.prepare();
progressDialog.dismiss();
}
}
catch (IllegalArgumentException e)
{e.printStackTrace();}
catch (IllegalStateException e)
{e.printStackTrace();}
catch (IOException e)
{e.printStackTrace();}
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
releaseMediaPlayer();
finish();
}
});
}
private void releaseMediaPlayer()
{
if (mMediaPlayer != null)
{
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}