Whenever I'm trying to load at least 4 mediaPlayers, one of them will corrupt the video it's trying to load and trigger an Android OS message "Can't play this video"
Other information:
For 3 mediaPlayers everything works fine.
On other Android versions, different from 4.2, the same code with the same 4 video works.
The 4 video can be played independently on the device. There is no format problem.
After starting the program and getting the "Can't play this video" message, the video can no longer be played in any other application unless I reset the device.
I tried this both with VideoViews or independent MediaPlayers displayed on surfaceViews.
I replicated the error on more devices running Android 4.2. On android 4.1.2 and other android 4 versions I do not recall the code worked fine.
On Android, the idea is that everything related to media codecs is hidden from the developer which has to use a consistent and unique API :
MediaPlayer
.When you play a media, would it be a stream or something located on the external device, the low level codecs/parsers are instanciated every time an application will be needing their help.
However, it occurs that for particular reasons related to hardware decoding, some codecs, cannot be instantiated more than once. As a matter of fact, every application must be releasing resources (codecs instances for instance) when they do not need them anymore by calling
MediaPlayer.release()
in a valid state.In fact, what I'm saying is illustrated in the documentation of
release
on the Android Developers website :So, either you are not calling
release
when you are done playing back, or another app is holding a reference on this kind of resources.EDIT :
If you need to be rendering several videos on the same
Activity
, you have two choices. As I said in my response, what you originally wanted is not possible because of low-level issues, neither it is on iOS by the way.What you can try to do though is :
If the medias you are playing are not real-time streamed content, you could wrap the 4 videos into a single one, using one of the widely available free video editors. Then render the video in full screen in your Activity, it will look like you have 4
Views
.If they are real-time/non recorded content, keep the first video as is. I assume every video is encoded using the same codec/container. What you might be trying is to transcode the 3 other videos so they use a different codec and a different format. Make sure you are transcoding to a codec/container that is supported by Android. This might potentially force Android to use different decoders in the same time. I think this is overkill compared to the result you're expecting.
Lastly, you could use a different backend for decoding such as
MediaPlayer
+FFMPEG
or justFFMPEG
. But again, even if it works this will be, I think, a huge overkill.To sum this up, you have to make compromises in order for this to work.