Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play video file from internal storage android using VideoView

Am trying to play a video stored in the internal storage in Android. However, no matter what I do it keeps coming back with either a -1 error or a -38 error. Both seem to be rather generic errors hence are not much intelligible.

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

The steps involved in my app include,

  1. downloading a file from a remote url
  2. Storing the file in internal storage (note i use the convention for ensuring that it has global read permissions. i.e

    openFileOutput(file_name, Context.MODE_WORLD_READABLE);
    
  3. Reading the media file back at a later point from this location, and playing it in a videoView.

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

I also went through other links in StackOverflow which point out that I need to implement a CustomContentProvider to be able to read these files. Is there a direct way of accessing the file uri and setting it to the videoView without having to resorting to creating a custom content provider and using a mediaPlayer instead of a videoView.

Other StackOverflow references used

  1. Android - Load video from private folder of app
  2. Can a videoview play a video stored on internal storage?
like image 507
baradas Avatar asked May 15 '12 11:05

baradas


1 Answers

please try this. I have explained the procedure to play video from raw folder on this link: Video player not workig! . On that, if you modify

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

with

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

For example:

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

I think will solve your issue. Remember to give the necessary permissions on the manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 92
Basil Avatar answered Oct 16 '22 21:10

Basil