Wednesday, February 20, 2013

How to play a streaming video using VideoView class in Android

1. Create a new class called VideoDisplay.java and paste the following code

package com.samples.video
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
 
public class VideoDisplay extends Activity
{
  

 @Override
    public void onCreate(Bundle savedInstanceState)
    { 

        String mp4path = http://www.mywebsite.com/myVideo.mp4;
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
       
        VideoView videoView =(VideoView)findViewById(R.id.videoView);
        videoView.setVideoPath(mp4Path);
        videoView.setMediaController(new MediaController(this));       
        videoView.requestFocus();
       
        videoView.start();
     }

}

2. Create a new layout file named main.xml and paste the following code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
>
 
<VideoView android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>
</LinearLayout>

3. Add the following lines in AndroidManifest.xml file

 <activity android:name=".VideoDisplay">
 </activity>


4. 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"

No comments:

Post a Comment