Android 4.2 with 4 MediaPlayers = "Can't play this video"
Asked Answered
T

1

7

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.

Trypanosome answered 5/4, 2013 at 16:36 Comment(1)
did you get success to play 4 videos in one screen becuase when i am trying to play 4 videos in same screen in 4 videoview it shows me message of "Video cant be played",Please help meMantelet
H
1

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 :

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

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 just FFMPEG. 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.

Hypercatalectic answered 5/4, 2013 at 18:43 Comment(7)
Hey, thanks for the replay. Unfortunately it doesn't help me because I actually need all the 4 instances of the MediaPlayer in the same time. I am sure that no other application is using the resources because I'm rebooting the device. It's strange that on other Android versions it works.Trypanosome
When you say other Android versions, I assume you were testing this on other phones as well. I don't see why you would have to keep a reference on 4 MediaPlayers at the same time. Even if you're running completely different Activities, Services or Applications, you should be instanciating the MediaPlayer on-demand, and release it as soon as the playback is over or whatever reason that might lead the playback to stop. And then, again, reinstanciate it when playback is required.Hypercatalectic
I need to show 4 perspectives of the same object in parallel on the same screen.Trypanosome
That is 4 videos on the same Activity ?Hypercatalectic
yes, 4 videos need to be running simultaneous in the same ActivityTrypanosome
Can you please write how you solved this problem? I have the same issueMoskowitz
If you use the Android MediaPlayer only, you cannot, since the underlying low level codecs will not be able to be instanciated in the case you are running concurrent instances already.Hypercatalectic

© 2022 - 2024 — McMap. All rights reserved.