Monday, April 11, 2016

VideoView example to play video from Internet

Example of VideoView to play video from Internet:


MainActivity.java
package com.blogspot.android_er.androidvideoview;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

VideoView myVideoView;

String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);

prepareVideo();
}

private void prepareVideo(){

Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();

uriVideoSource = Uri.parse(videoSource);

myVideoView.setVideoURI(uriVideoSource);

myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);

myVideoView.requestFocus();
myVideoView.start();

}

MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};

MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {

long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();

}
};

MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {

String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}

String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";

}

Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidvideoview.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>


uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.

When I tested it on Nexus 7 running Android 5.1.1, control bar (Play/Pause, Forward and Backward) is shown over the VideoView. But not shown on RedMi 2 running Android 5.0, Android Emulator of Nexus 6P phone, Nexus 7 tablet running Marshmallow, and Nexus 5x phone running Android N.

test on Nexus 7 running Android 5.1.1:

test on RedMi 2 running Android 5.0

test on Nexus 6P phone (Emulator) running Marshmallow

test on Nexus 9 tablet (Emulator) running Marshmallow

test on Nexus 5X (Emulator) running Android N

Remark:
To runing this example on Android Emulator, have to manual select "Hardware = GLES 2.0" in Emulated Performance of Graphics.



Next:
Add MediaController to VideoView, to provide controls of Play/Pause, Forward and Backward.

Related:
- MediaPlayer example to play video from Internet
Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.


No comments:

Post a Comment