Mar 18, 2011

Using VideoView to play mp4 from sdcard

Using VideoView to play mp4 from sdcard

First of all, download/save a mp4 file in SD Card of the Android device, eg: "/sdcard/Video/Android in Spaaaaaace!_low.mp4".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   >
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>
</LinearLayout>


package com.AnVideoView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class AnVideoView extends Activity {
 
 String SrcPath = "/sdcard/Video/Android in Spaaaaaace!_low.mp4";
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
       myVideoView.setVideoPath(SrcPath);
       myVideoView.setMediaController(new MediaController(this));
       myVideoView.requestFocus();
       myVideoView.start();
   }
}



Related Post:
- A simple example using VideoView to play 3gp from YouTube

Updated:
Using VideoView to play mp4, with MediaController enable/disable

18 comments:

  1. Hi,

    I m new to android,I used this code I can see Video static (not running,only image from video) and controller pls tell me where I m wrong.

    ReplyDelete
  2. Please let me know your test environment. I test on N1 without problem.

    ReplyDelete
    Replies
    1. I have Problem on emulator-5554..
      i shows "This video cannot be Played..
      How i set a srcPath.. through Program.
      where i keep a Mp4 file..
      Reply me ...

      Delete
  3. really nice tutorial, but how to play large video please help me

    ReplyDelete
  4. This is great and it helped for what I was putting together.

    Let's say you are releasing the apk in the market, how do you get the video to the buyer? How does it get onto the SD card?

    ReplyDelete
  5. I have Problem on emulator-5554..
    i shows "This video cannot be Played..
    How i set a srcPath.. through Program.
    where i keep a Mp4 file..
    Reply me ...

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I would just like to say thanks, for your instructions worked first time.

    ReplyDelete
  8. Please use: Environment.getExternalStorageDirectory().getPath() instead of "/sdcard"

    ReplyDelete
  9. how to solve this type of error

    02-21 12:58:34.490:E/MediaPlayer(393): error (1, -2147483648) 02-21 12:58:34.490: E/MediaPlayer(393): Error (1,-2147483648)

    PLz....

    ReplyDelete
    Replies
    1. That means that either a) the format of the mp4 cannot be played. Convert it or b) the path and name is wrong, you missed something. Check what is contained in your variable and match it to the location of your file

      Delete
  10. Hi guys I need a bit help. I am trying to play streaming videos from www.hittvchannels.com into my app but I get popup message says Can't play this video.

    ReplyDelete
  11. HI how to show the player controsl such as "Play","Pause",Current position, progress, stop etc. ?

    ReplyDelete
    Replies
    1. The MediaController already set in this example, tap on the video to display it. Read the update post Using VideoView to play mp4, with MediaController enable/disable.

      Delete
  12. thanx ,verry nyc code examle video view

    ReplyDelete

Infolinks In Text Ads