Wednesday, 10 August 2011

How to Play Video in Media Player without Display inbuilt Media Player Buttons?

Put below code into your Main Activity File.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setFixedSize(176, 144);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mediaPlayer = new MediaPlayer();
     
    String stringPath = "/sdcard/flying_kiss.mp4";
       
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset();
    }

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDisplay(surfaceHolder);
       
    try {
        mediaPlayer.setDataSource(stringPath);
        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaPlayer.start();
}


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surfaceview"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <Button android:id="@+id/mBtnmore" android:layout_width="wrap_content"
        android:layout_height="35dip" android:text="More"
        android:layout_marginRight="5dip" android:layout_alignParentRight="true"
        android:layout_marginTop="8dip" />
    <Button android:id="@+id/mBtnhints" android:layout_width="wrap_content"
        android:layout_marginBottom="4dip" android:layout_height="35dip"
        android:text="Hints" android:layout_marginRight="5dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

4 comments:

  1. How can we get the rtsp link can you please help i'm not getting correct solution
    Contact Me:kirankumar667.2015@gmail.com



    Thanks for your code.

    ReplyDelete
  2. What is the surfaceHolder? What is the class for surfaceHolder object? Kindly contact me with my gmail: parthipanblisslogix@gmail.com

    ReplyDelete
  3. please give full information related to Play Video in Media Player without Display inbuilt Media Player. it is not final solution

    ReplyDelete