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.

How to Play mp4 Video in Android Using Remote URL ?

Add Below Code into your MainActivity.java file.

@Override
protected void onCreate(Bundle savedInstanceState)
     // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.videodisplay);
        String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view&amp; current=1376992942447_242.mp4";
        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(link);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
    }
}

Enjoy :-)

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