Pages

Monday 12 October 2015

Android play video programmatically

Use following code to play video.

vvVideo = (VideoView) findViewById(R.id.vvVideo);
// use this to play video from raw resource
  vvVideo.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.main));
// use this to play video from url (streaming)
  vvVideo.setVideoURI(Uri.parse("http://..."));
// if possible do not use height to match_parent. It may create problem. I am not sure about this.
// play video from asset
  vvVideo.setVideoURI(Uri.parse("file:///android_asset/path/to/your.mp4"));
// play video from sdcard
  vvVideo.setVideoURI(Uri.fromFile(new File("/sdcard/cats.jpg")));


  vvVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
    // video play complete
   }
  });

  vvVideo.start();

use following xml

<VideoView
        android:id="@+id/vvVideo"
        android:layout_width="match_parent"
        android:layout_height="488dp"
        android:layout_centerInParent="true"
        />

add read external storage permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

1 comment :

  1. Slottyro Casino & Resort - Mapyro
    Slottyro Casino 강원도 출장샵 & Resort is a Wedding Venue 광주 출장안마 in Las 광명 출장마사지 Vegas, 시흥 출장안마 NV. Read reviews, view photos, see special offers, and contact Slottyro Casino & Resort 전라남도 출장샵 directly on

    ReplyDelete