Android Video Streaming (VideoView) Tutorial
Last Updated: March 26, 2013
In this tutorial, you will learn how to stream a remote video by using a MediaController and display it into a VideoView in your Android Application. MediaController is a controller for Media Player such as functions like Play/Pause, Rewind, Fast Forward and a progress slider. Videos can be loaded from various sources such as remote servers, cloud storage and internal SD Card. We will create a button and on button click will start to stream a remote video in a new activity. So lets begin…
Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project VideoStreamTutorial.
Application Name : VideoStreamTutorial
Project Name : VideoStreamTutorial
Package Name : com.androidbegin.videostreamtutorial
Open your MainActivity.java and paste the following code.
MainActivity.java
package com.androidbegin.videostreamtutorial; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout from video_main.xml setContentView(R.layout.activity_main); // Locate the button in activity_main.xml button = (Button) findViewById(R.id.MyButton); // Capture button clicks button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // Start NewActivity.class Intent myIntent = new Intent(MainActivity.this, VideoViewActivity.class); startActivity(myIntent); } }); } }
In this activity, we have created a button and on button click will open a new activity. In this tutorial, we have hosted a video file in our server. https://www.androidbegin.com/tutorial/AndroidCommercial.3gp
NOTE : Keep in mind that your video file must be compatible with Android. Check it out compatibility list here.
Next, create an XML file for your MainActivity Graphical Layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file activity_main.xml and paste the following code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/MyButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/button" /> </RelativeLayout>
Next, create a new activity to stream a remote video. Go to File > New > Class and name it VideoViewActivity.java. Select your package named com.androidbegin.videostreamtutorial and click Finish.
Open your VideoViewActivity.java and paste the following code.
package com.androidbegin.videostreamtutorial; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.util.Log; import android.widget.MediaController; import android.widget.VideoView; public class VideoViewActivity extends Activity { // Declare variables ProgressDialog pDialog; VideoView videoview; // Insert your Video URL String VideoURL = "https://www.androidbegin.com/tutorial/AndroidCommercial.3gp"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout from video_main.xml setContentView(R.layout.videoview_main); // Find your VideoView in your video_main.xml layout videoview = (VideoView) findViewById(R.id.VideoView); // Execute StreamVideo AsyncTask // Create a progressbar pDialog = new ProgressDialog(VideoViewActivity.this); // Set progressbar title pDialog.setTitle("Android Video Streaming Tutorial"); // Set progressbar message pDialog.setMessage("Buffering..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); // Show progressbar pDialog.show(); try { // Start the MediaController MediaController mediacontroller = new MediaController( VideoViewActivity.this); mediacontroller.setAnchorView(videoview); // Get the URL from String VideoURL Uri video = Uri.parse(VideoURL); videoview.setMediaController(mediacontroller); videoview.setVideoURI(video); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } videoview.requestFocus(); videoview.setOnPreparedListener(new OnPreparedListener() { // Close the progress bar and play the video public void onPrepared(MediaPlayer mp) { pDialog.dismiss(); videoview.start(); } }); } }
In this activity, we will show a progress bar until the video has been partially loaded then play the video in a VideoView. We have implemented a MediaController to allow users to Play/Pause, Rewind, Fast Forward and a progress slider to control the video.
Next, create an XML file for your VideoViewActivity Graphical Layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file videoview_main.xml and paste the following code.
videoview_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <VideoView android:id="@+id/VideoView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout>
Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml
<resources> <string name="app_name">Video Stream Tutorial</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="button">Stream Video</string> </resources>
In your AndroidManifest.xml, we need to declare an activity and a permissions to allow the application to access to the Internet. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidbegin.videostreamtutorial" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".VideoViewActivity" /> </application> </manifest>
Output:
Source Code
[purchase_link id=”7890″ text=”Purchase to Download Source Code” style=”button” color=”green”]
This is all really helpful! However, if you are having trouble manually coding or you have no idea whatsoever on how to code, I know a few alternatives that can help! Of course, one of them is, in my opinion, the best! I am talking about the live video streaming library for Android, provided by Streamaxia (https://www.streamaxia.com/opensdk-android-rtmp-library/) and about their app made especially for easy live streaming: https://www.streamaxia.com/broadcastme-whitelabel-app/ I hope this helps some of you!
RaluG
Android Video Streaming (VideoView) Tutorial
Can we seek the online video ? I tried to stream one and videoView.canSeekForward() returns false.
Jainam Jhaveri
Android Video Streaming (VideoView) Tutorial
How play video along with downloading..(Like you tube buffering play video)
Ranjitha N
Android Video Streaming (VideoView) Tutorial
it stretced for portrait video. how could you solve it?
Hang Nadi
Android Video Streaming (VideoView) Tutorial