Android: how to play video from assets?

I am making an application in which I have to show a video from a data folder in a Snippet . Can someone help me do this? Do I need to use VideoView in XML?

+31
android video android-videoview
Jul 6 2018-12-12T00:
source share
5 answers

Instead of accessing exams, you should copy the video to the res / raw project folder. Create a raw folder in the res folder. It must be in a supported format (3gp, wmv, mp4) named with lower case, numbers, underscores and periods in its file name: video_file.mp4.

VideoView view = (VideoView)findViewById(R.id.videoView); String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file; view.setVideoURI(Uri.parse(path)); view.start(); 
+68
Jul 6 2018-12-12T00:
source share
 VideoView view = (VideoView)findViewById(R.id.videoView); String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file; view.setVideoURI(Uri.parse(path)); view.start(); 

This is AkashG code, but I remember that R is not from the Android class here. This is from your own project.

+1
Oct 17 '14 at 8:48
source share

I already suffered from the same problem, and you should prefer the res / raw folder project instead of assets. Create a raw folder in the res folder. Save the video files in a supported format (3gp, wmv, mp4) and are named with lower case, numbers, underscores and dots in the file name: filename.3gp in the source folder.

 VideoView videoview = (VideoView) findViewById(R.id.VideoView); String uriPath = "android.resource://your application package name/raw/your wmv/mp4/3gp file in res/raw path without extension"; videoview.setVideoURI(Uri.parse(uriPath)); videoview.start(); 
0
Dec 05 '17 at 11:06 on
source share

Play video (sample.mp4) in res / raw folder with Media Controller

// Import reports

 import android.widget.VideoView; import android.widget.MediaController; public class youractiviy extends Activity { private VideoView videoView; private MediaController mediaController; protected void onCreate(Bundle savedInstanceState) { // Your Startup code videoView = (VideoView) findViewById(R.id.video_view); videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample); mediaController = new MediaController(TestActivity.this); mediaController.setAnchorView(videoView); videoView.setMediaController(mediaController); videoView.start(); } } 

// XML code

 <VideoView android:id="@+id/video_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
0
May 23 '18 at 9:39
source share

First you need to convert the video to InputStream, and then save it in the user's internal storage, then display it and delete this file after the video ends.

 try{ String path = Environment.getExternalStorageDirectory()+"/"+APP_NAME()+"/videos/"+ls+"/" ; InputStream input = getAssets().open("vid/dal.mp4"); String name = System.currentTimeMillis() +".mp4"; File f = new File(path); f.mkdirs(); int size = input.available(); FileOutputStream output = new FileOutputStream(new File(path+name)); byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); total += count; if (size <= total) { break; } } output.flush(); output.close(); input.close(); //Toast.makeText(VideoPlayer.this , "file created !" , Toast.LENGTH_LONG).show(); Uri uri = Uri.parse(path+name) ; videoView.setVideoURI(uri); videoview.start(); }cath(Exception e){ } 
0
Jun 15 '19 at 12:03 on
source share



All Articles