Open Mp4 Using Intent.Action_Open_Document, Action_Get_Content Together With Action_Pick, Together With Play Inwards Videoview.

Posted on

This instance demonstrate how to opened upwards video file of mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT as well as ACTION_PICK, as well as play inward VideoView, also add MediaController to the VideoView.



This instance tin laissez passer on the sack also play videos stored inward Google Drive:
(tested on XiaoMi RedMi two running Android 4.4.4)


MainActivity.java

package com.blogspot.android_er.myopenmp4;  import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView;  world degree MainActivity extends AppCompatActivity {      Button btnOpenDocument, btnGetContent, btnPick;     TextView info;     VideoView myVideoView;      Uri videoFileUri = null;      terminal static int RQS_OPEN_DOCUMENT = 1;     terminal static int RQS_GET_CONTENT = 2;     terminal static int RQS_PICK = 3;      MediaController mediaController;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         information = (TextView) findViewById(R.id.info);         myVideoView = (VideoView)findViewById(R.id.vview);         btnOpenDocument = (Button)findViewById(R.id.opendocument);         btnGetContent = (Button)findViewById(R.id.getcontent);         btnPick = (Button)findViewById(R.id.pick);         btnOpenDocument.setOnClickListener(btnOpenDocumentOnClickListener);         btnGetContent.setOnClickListener(btnGetContentOnClickListener);         btnPick.setOnClickListener(btnPickOnClickListener);          mediaController = novel MediaController(MainActivity.this);         myVideoView.setMediaController(mediaController);      }      somebody void prepareVideo(){          Toast.makeText(MainActivity.this,                 videoFileUri.toString(),                 Toast.LENGTH_LONG).show();         myVideoView.setVideoURI(videoFileUri);          myVideoView.setOnCompletionListener(myVideoViewCompletionListener);         myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);         myVideoView.setOnErrorListener(myVideoViewErrorListener);          myVideoView.requestFocus();         myVideoView.start();      }      MediaPlayer.OnCompletionListener myVideoViewCompletionListener =             novel MediaPlayer.OnCompletionListener() {                  @Override                 world void onCompletion(MediaPlayer arg0) {                     Toast.makeText(MainActivity.this, "End of Video",                             Toast.LENGTH_LONG).show();                 }             };      MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =             novel MediaPlayer.OnPreparedListener() {                  @Override                 world 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                 world boolean onError(MediaPlayer mp, int what, int extra) {                      String errWhat = "";                     switch (what){                         instance MediaPlayer.MEDIA_ERROR_UNKNOWN:                             errWhat = "MEDIA_ERROR_UNKNOWN";                             break;                         instance MediaPlayer.MEDIA_ERROR_SERVER_DIED:                             errWhat = "MEDIA_ERROR_SERVER_DIED";                             break;                         default: errWhat = "unknown what";                     }                      String errExtra = "";                     switch (extra){                         instance MediaPlayer.MEDIA_ERROR_IO:                             errExtra = "MEDIA_ERROR_IO";                             break;                         instance MediaPlayer.MEDIA_ERROR_MALFORMED:                             errExtra = "MEDIA_ERROR_MALFORMED";                             break;                         instance MediaPlayer.MEDIA_ERROR_UNSUPPORTED:                             errExtra = "MEDIA_ERROR_UNSUPPORTED";                             break;                         instance 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;                 }             };      View.OnClickListener btnOpenDocumentOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent();             intent.setAction(Intent.ACTION_OPEN_DOCUMENT);             intent.addCategory(Intent.CATEGORY_OPENABLE);             intent.setType("video/mp4");             startActivityForResult(                     Intent.createChooser(intent, "ACTION_OPEN_DOCUMENT"),                     RQS_OPEN_DOCUMENT);         }     };      View.OnClickListener btnGetContentOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent();             intent.setType("video/mp4");             intent.setAction(Intent.ACTION_GET_CONTENT);             startActivityForResult(Intent.createChooser(                     intent, "ACTION_GET_CONTENT"), RQS_GET_CONTENT);         }     };      View.OnClickListener btnPickOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent(Intent.ACTION_PICK,                     android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);             startActivityForResult(                     Intent.createChooser(intent, "ACTION_PICK"),                     RQS_PICK);         }     };      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK) {             if(requestCode == RQS_OPEN_DOCUMENT                     || requestCode == RQS_GET_CONTENT                     || requestCode == RQS_PICK){                  videoFileUri = data.getData();                 info.setText(videoFileUri.toString());                  prepareVideo();             }         }     } }  

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.myopenmp4.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/opendocument"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_OPEN_DOCUMENT" />      <Button         android:id="@+id/getcontent"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_GET_CONTENT" />      <Button         android:id="@+id/pick"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_PICK" />      <TextView         android:id="@+id/info"         android:layout_width="fill_parent"         android:layout_height="wrap_content" />      <VideoView         android:id="@+id/vview"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout>  

uses-permission of “android.permission.READ_EXTERNAL_STORAGE” is needed inward AndroidManifest.xml.

 This instance demonstrate how to opened upwards video file of mp Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT as well as ACTION_PICK, as well as play inward VideoView.Download the files .

Related:
Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT as well as ACTION_PICK, alongside checking as well as requesting permission at runtime.
– VideoView instance to play video from Internet.