I have a problem that I have been trying to find a solution for a long time. My situation is as follows:
I have an app that downloads zipped videos and unzips them at the application's private folder and more specifically at a subfolder. For example at /data/data/my.app.package.name.here/files/assets/assets-955.
Inside this folder the video is unzipped. The unzipping process is completed successfully since I can pull and view the video without problems when running the app on the emulator.
I then have another activity that is accessing this folder, finds the video file and tries to open it. At this point I get an error that "Sorry, this video cannot be played" with the following error stack:
01-30 17:36:17.770: D/ContentDemoActivity(6757): File: /data/data/xxxx/files/assets/assets-955/bank_2.mp4
01-30 17:36:17.830: I/MediaPlayer(6757): prepareAsync called in state 4
01-30 17:36:17.830: E/MediaPlayer(6757): error (1, -2147483648)
01-30 17:36:17.860: E/MediaPlayer(6757): Error (1,-2147483648)
01-30 17:36:17.860: D/VideoView(6757): Error: 1,-2147483648
01-30 17:36:19.370: E/MediaPlayer(6757): stop called in state 0
01-30 17:36:19.370: E/MediaPlayer(6757): error (-38, 0)
01-30 17:36:19.370: W/MediaPlayer(6757): mediaplayer went away with unhandled events
The code with which I am trying to play the video is pretty basic:
mView = (VideoView) findViewById(R.id.videoView);
mMediaPlayer = new MediaPlayer();
mView.requestFocus();
mHolder = mView.getHolder();
Log.d(tag, "Populating content. Assets path: " + mAssetsPath);
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
mView.setVideoURI(Uri.parse(files[0].toString()));
mView.setMediaController(new MediaController(this));
and the layout of the activity has a plain VideoView, nothing fancy there.
The strangest thing is that for testing purposes I used the same video, this time loading it from the "raw" folder and it runs smoothly without problem. In that case though I had to load it with:
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bank_2);
mVideoView.setVideoURI(video);
mVideoView.start();
I would do the same with the downloaded videos but there doesn't seem to be any function at the API that will allow me to load a video Uri from the application's private folder.
I have found various solutions by using file descriptors, listeners for the videoView, flags indicating MODE_WORLD_READABLE, pre-calculation of the dimensions of the videoView, etc but none of them had positive results.
In a nutshell, my questions are:
Thanks.
After CommonsWare suggestion, I went with the following implementation:
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.start();
Log.d(tag, "4");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(tag, "5");
Unfortunately, this time I get the following errors:
01-31 12:40:11.480: D/ContentDemoActivity(15896): File: /data/data/com.houseofradon.meb/files/assets/assets-955/bank_2.mp4
01-31 12:40:11.480: D/ContentDemoActivity(15896): 1
01-31 12:40:11.480: D/ContentDemoActivity(15896): 2
01-31 12:40:11.500: D/ContentDemoActivity(15896): 3
01-31 12:40:11.500: E/MediaPlayer(15896): start called in state 2
01-31 12:40:11.500: E/MediaPlayer(15896): error (-38, 0)
01-31 12:40:11.500: D/ContentDemoActivity(15896): 4
01-31 12:40:11.500: D/ContentDemoActivity(15896): 5
01-31 12:40:11.530: E/MediaPlayer(15896): Error (-38,0)
So, something happens when the media player starts. Error code -38 doesn't seem to mean anything specific as I found here.
Any idea what I am missing ???
I now use a mediaPlayer and a SurfaceView to do the whole process along with a surfaceHolder listener. Here is the code:
mMediaPlayer = new MediaPlayer();
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(tag, "surfaceChanged");
try {
mMediaPlayer.setDisplay(mHolder);
Log.d(tag, "7");
mMediaPlayer.start();
Log.d(tag, "8");
} catch (IllegalStateException e) {
e.printStackTrace();
}
Log.d(tag, "9");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(tag, "surfaceCreated");
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.setVolume(100, 100);
Log.d(tag, "4");
mMediaPlayer.prepare();
Log.d(tag, "5");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(tag, "6");
}
I can listen to the audio of the video but the picture is just a plain black color. I also get an error almost at the end of the video playback that says:
01-31 14:26:01.300: W/AudioSystem(17165): AudioFlinger server died!
01-31 14:26:01.300: W/IMediaDeathNotifier(17165): media server died
01-31 14:26:01.300: E/MediaPlayer(17165): error (100, 0)
01-31 14:26:01.300: E/MediaPlayer(17165): Error (100,0)
I am using an actual device, Samsung Galaxy Tab 10.1. Any ideas ?
Why do I get those errors which according to what I have found online are errors that are related with problematic encoding of the video file ?
Because the media playback engine runs in its own process, and it does not have rights to read your file.
What is the best things to use in my case, a VideoView or a surfaceView ?
A VideoView
contains a SurfaceView
. Whether you use VideoView
or a combination of MediaPlayer and SurfaceView
is up to you.
Which is the ideal method to load a video from the application's private folder and be able to play it?
Either create a ContentProvider
that can serve up your local file and use the provider Uri
instead of the Uri
to a local file, or create the local file using openFileOutput()
and MODE_WORLD_READABLE
.