How to play video (.mp4) from assets or raw folder for video purpose?

How can I play mp4 video from my assets or source folder for video purpose?

no matter what I try, I always get:

06-25 14:32:14.070: E/AndroidRuntime(3070): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://mypackage/raw/myvideo } 

I tried:

  Intent tostart = new Intent(Intent.ACTION_VIEW); String movieUrl = "file:///android_asset/myvideo.mp4"; // String movieUrl = "android.resource://" + getPackageName() + "/raw/" + "myvideo.mp4"; tostart.setDataAndType(Uri.parse(movieUrl), "video/*"); startActivity(tostart); 

I can only find answers to where people use video from the SD card, is there any option so that I can use the raw or assets folder?

0
source share
2 answers

You can parse the video URL from the source folder using the following command:

 Uri.parse("android.resource://com.testvideo/" + R.raw.video); 

then try:

0
source
 AssetFileDescriptor afd; try { afd = getAssets().openFd("videofile/video.3gp"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 

Here video.3gp is the file under the folder --- video file β†’ - this is one folder in the resource folder

Using a media planner, you can set this method

 mediaPlayer = new MediaPlayer(); if(mediaPlayer.isPlaying()){ mediaPlayer.reset(); } mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDisplay(surfaceHolder); try { mediaPlayer.setDataSource(afd.getFileDescriptor(), afd .getStartOffset(), afd.getLength()); mediaPlayer.prepare(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mediaPlayer.start(); 
+1
source

Source: https://habr.com/ru/post/948740/


All Articles