Saturday, March 29, 2014

How to play video in Android video view


Playing video in Android from a file or from a stream over the network is relatively simple as long as the video is in an acceptable format. The supported formats are
  • MP4 (MPEG-4), H.263, H.264 AVC (for Android 3.0+) & VP8 (for Android 4.3+)
Method I – Video File is kept in res/raw folder

1.      Create folder called “raw” in res folder if it does not exist and put video file in raw folder.
Note: Try with the video in following link
(This video was played in emulator - Android 4.4, API 19)

2.      Add this to xml file

<VideoView
       android:id="@+id/video_view"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

3.      Call “PlayVideo” method in “onCreate” method.

public void playVideo(){
mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" +getPackageName()+"/"+R.raw.lenovo));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
  
}  

“lenovo” is the name of the video file without it’s extension.
I was able to play .avi .mp4 and .3gp video files but not .flv in the actual device. (I used SONY Xperia P - Android 4.1.2)


Method II – Video File is kept in SD card

Ø  If video files are kept in SD card
Then set path:
mVideoView.setVideoPath(sdcard/file_ name_ with_extension");

Update:

          Video in the SD card is store in
          Internal storage>Android><Package_name><files>

            So that the path would be set as

            
File root = Environment.getExternalStorageDirectory();
String externalFilesDir = c.getExternalFilesDir(null).toString();
String videoResource = externalFilesDir +"/"+video_filename with extension;              //video_filename as String
mVideoView.setVideoPath(videoResource);
mVideoView.start();

If you want to put videos to emulator SD card go to
           
                 DDMS>File Explorer> Click pull files to SD card


Ø  Sometimes it will fail saying “Transfer Error Occurred, Read only file system

Then try following:
·         Go to .android > avd >select emulator > go to properties > uncheck read only.
·         Go to AVD manger in eclipse > Edit emulator > Keep value to SD card memory 
      and try remounting SD card

Ø  Sometimes you won’t be able to uncheck read only. I don’t know
what to do then L.


You would better try with actual device. J
Connect your phone to PC .  

Go to Internal Storage > Android >Data > create folder by your package name > make a folder                   named “files” > put video file to files folder > copy paste . apk to phone and run.


Ø  If you see a message saying "Sorry, this video cannot be played"

    Try changing the name of your video file by excluding any white spaces. It looks like lower Android OS versions cannot play video files containing white spaces in their file name. For example, change "My Video.mp4" to "MyVideo.mp4".

Ø  If you still cannot play the videos check whether the video is in H.264 AVC Baseline profile.

P.S.
Instead using video View we can use media player in Android.
Refer link: