Play video file from android internal storage using VideoView

I am trying to play videos stored in Android internal memory. However, no matter what I do, it keeps returning with -1 error or -38 error. Both of them seem to be quite common mistakes, so they are not very clear.

I am wondering if it is possible to use VideoView and not an instance of MediaPlayer to play a video file from local storage.

The steps related to my application include

  • download file from remote URL
  • Saving the file internally (note that I use the convention to ensure its global read permission. I.e.

    openFileOutput(file_name, Context.MODE_WORLD_READABLE); 
  • Reading a media file at a later point from this place and playing it in a video video.

     String filePath = "file://" + getFilesDir()+File.separator+file_name; Uri videoUri = Uri.parse(filePath); Log.d("Video Player", filePath); videoPlayer.setVideoURI(videoUri); 

I also looked at other links in StackOverflow that indicate that I need to implement CustomContentProvider in order to be able to read these files. Is there a direct way to access the uri file and attach it to a video visa, without having to resort to creating a custom content provider and using a media player instead of a video visa.

Other StackOverflow Links Used

+9
source share
3 answers

try it. I explained the procedure for playing video from the source folder at this link: Video player does not work! . However, if you change

 Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName); 

from

 Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"<path to your video>"); 

For instance:

 Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/dcim/camera/2012-05-15_17-50-39_319.3gp"); 

I think that will solve your problem. Remember to provide the necessary permissions for the manifest.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+4
source

Copy it to external storage (temporarily)

I ran into the same problem as you, and resorted to simply temporarily copying the file to external storage before playing it, and then deleting the temporary file.

Here is an example of the code I used for this:

 try { // Copy file to temporary file in order to view it. temporaryFile = generateTemporaryFile(file.getName()); FileUtils.copyFile(file, temporaryFile); previewVideo(temporaryFile, videoView); } catch (IOException e) { e.printStackTrace(); } # Helpers protected File generateTemporaryFile(String filename) throws IOException { String tempFileName = "20130318_010530_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File tempFile = File.createTempFile( tempFileName, /* prefix "20130318_010530" */ filename, /* filename "video.3gp" */ storageDir /* directory "/data/sdcard/..." */ ); return tempFile; } protected void previewVideo(File file, VideoView videoView) { videoView.setVideoPath(file.getAbsolutePath()); MediaController mediaController = new MediaController(this); videoView.setMediaController(mediaController); mediaController.setMediaPlayer(videoView); videoView.setVisibility(View.VISIBLE); videoView.start(); } 
+4
source

It's a bit late in the game here, but if you add FileProvider to your application, you can then create a URI so that VideoView can use.

This is really pretty trivial, and once you are done, you can access your files, for example:

 Uri video = FileProvider.getUriForFile(context, context.getPackageName(), new File(myAbsoluteVideoPath)); myVideoView.setVideoURI(video); 

This can be used to play back videos created in the application and saved on the device.

0
source

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


All Articles