Videoview Illustration To Play Video From Internet

Posted on

Example of VideoView to play video from Internet:

package com.blogspot.android_er.androidvideoview;  import; import; import android.os.Bundle; import; import android.widget.Toast; import android.widget.VideoView;  populace shape MainActivity extends AppCompatActivity {      VideoView myVideoView;      String videoSource =             "";     Uri uriVideoSource;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myVideoView = (VideoView)findViewById(;          prepareVideo();     }      someone 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 =             novel MediaPlayer.OnCompletionListener() {                  @Override                 populace void onCompletion(MediaPlayer arg0) {                     Toast.makeText(MainActivity.this, "End of Video",                             Toast.LENGTH_LONG).show();                 }             };      MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =             novel MediaPlayer.OnPreparedListener() {                  @Override                 populace void onPrepared(MediaPlayer mp) {                      long duration = myVideoView.getDuration(); //in millisecond                     Toast.makeText(MainActivity.this,                             "Duration: " + duration + " (ms)",                             Toast.LENGTH_LONG).show();                  }             };      MediaPlayer.OnErrorListener myVideoViewErrorListener =             novel MediaPlayer.OnErrorListener() {                  @Override                 populace boolean onError(MediaPlayer mp, int what, int extra) {                      String errWhat = "";                     switch (what){                         illustration MediaPlayer.MEDIA_ERROR_UNKNOWN:                             errWhat = "MEDIA_ERROR_UNKNOWN";                             break;                         illustration MediaPlayer.MEDIA_ERROR_SERVER_DIED:                             errWhat = "MEDIA_ERROR_SERVER_DIED";                             break;                         default: errWhat = "unknown what";                     }                      String errExtra = "";                     switch (extra){                         illustration MediaPlayer.MEDIA_ERROR_IO:                             errExtra = "MEDIA_ERROR_IO";                             break;                         illustration MediaPlayer.MEDIA_ERROR_MALFORMED:                             errExtra = "MEDIA_ERROR_MALFORMED";                             break;                         illustration MediaPlayer.MEDIA_ERROR_UNSUPPORTED:                             errExtra = "MEDIA_ERROR_UNSUPPORTED";                             break;                         illustration 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();                     render true;                 }             }; }  


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""     xmlns: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=""         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 inwards AndroidManifest.xml.

When I tested it on Nexus vii running Android 5.1.1, command bar (Play/Pause, Forward as well as Backward) is shown over the VideoView. But non shown on RedMi two running Android 5.0, Android Emulator of Nexus 6P phone, Nexus vii tablet running Marshmallow, as well as Nexus 5x telephone running Android N.

attempt on Nexus vii running Android 5.1.1:

attempt on RedMi two running Android 5.0

attempt on Nexus 6P telephone (Emulator) running Marshmallow

attempt on Nexus ix tablet (Emulator) running Marshmallow

attempt on Nexus 5X (Emulator) running Android due north

To runing this illustration on Android Emulator, convey to manual choose “Hardware = GLES 2.0” inwards Emulated Performance of Graphics.

– Add MediaController to VideoView, to render controls of Play/Pause, Forward as well as Backward.

MediaPlayer illustration to play video from Internet
– Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT as well as ACTION_PICK, as well as play inwards VideoView.