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
implements SurfaceHolder.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;
}
}
}
I
will be happy if you will provide your feedback or follow this blog. Any suggestion
and help will be appreciated.
Thank
you :)
this is what I get
ReplyDelete07-11 10:17:42.058: D/AndroidRuntime(8749): Shutting down VM
07-11 10:17:42.058: W/dalvikvm(8749): threadid=1: thread exiting with uncaught exception (group=0x412843a8)
07-11 10:17:42.066: E/AndroidRuntime(8749): FATAL EXCEPTION: main
07-11 10:17:42.066: E/AndroidRuntime(8749): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demosurfaceview/com.example.demosurfaceview.MainActivity}: java.lang.IllegalArgumentException: The surface has been released
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.os.Looper.loop(Looper.java:137)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-11 10:17:42.066: E/AndroidRuntime(8749): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 10:17:42.066: E/AndroidRuntime(8749): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 10:17:42.066: E/AndroidRuntime(8749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 10:17:42.066: E/AndroidRuntime(8749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-11 10:17:42.066: E/AndroidRuntime(8749): at dalvik.system.NativeStart.main(Native Method)
07-11 10:17:42.066: E/AndroidRuntime(8749): Caused by: java.lang.IllegalArgumentException: The surface has been released
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.media.MediaPlayer._setVideoSurface(Native Method)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.media.MediaPlayer.setDisplay(MediaPlayer.java:668)
07-11 10:17:42.066: E/AndroidRuntime(8749): at com.example.demosurfaceview.MainActivity.Create_Surface(MainActivity.java:87)
07-11 10:17:42.066: E/AndroidRuntime(8749): at com.example.demosurfaceview.MainActivity.onCreate(MainActivity.java:54)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.Activity.performCreate(Activity.java:5104)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-11 10:17:42.066: E/AndroidRuntime(8749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-11 10:17:42.066: E/AndroidRuntime(8749): ... 11 more
This comment has been removed by the author.
ReplyDeleteWill update you soon.
ReplyDeleteyou can visit this link..
ReplyDeletehttp://hasmukhbhadani.blogspot.in/search/label/Video-Play%20Live%20Streaming%20in%20Android.
i will correct it and update soon.