Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 'unsupported' codec in VideoView

I've a stream with contains a audio/video stream. The video encoding is H.264 AVC and the audio encoding is G.711 µ-law (PCMU). (I have no control over the output format.)

When I try to display the video into a VideoView frame, the device says that it cannot play the video. I guess that's because Android doesn't support the aforementioned audio encoding.

Is there a way to somehow display the selected stream in the VideoView?

  • Can I just use a Java code snippet to 'compute' the codec? I've seen the class android.net.rtp.AudioCodec, which contains a field PCMU, and I can imagine that class would be useful.
  • Or do I have to insert a library or some native code (FFmpeg) into my application and use that? If yes, how?

How to fix it?

like image 521
MC Emperor Avatar asked Jan 31 '26 09:01

MC Emperor


1 Answers

If you want to manipulate a stream before you display it, you are unfortunately getting into tricky territory on an Android device - if you do have any possibility of doing the conversion on the serve side it will likely by much easier (and you should also usually have more 'horsepower' on the server side).

Assuming that you do need to convert on the server side, then a technique you can use is to stream the video from the server to your app, convert it as needed and then 'stream' from a localhost server in your app to a VideoView in your app. Roughly the steps are:

  • Stream the encrypted file from the server as usual, 'chunk by chunk'
  • On your Android device, read from the stream and convert each chunk as it is received
  • Using a localhost http server on your Android device, now 'serve' the converted chunks to the MediaPlayer (the media player should be set up to use a URL pointing at your localhost http server)

An example of this approach, I believe, is LibMedia: sure: http://libeasy.alwaysdata.net (note, this is not a free library, AFAIK).

For the actual conversion you can use ffmpeg as you suggest - there are a number of ffmpeg wrapper for Android projects that you can either use or look at to design your own. Some examples:

  • http://hiteshsondhi88.github.io/ffmpeg-android-java/
  • https://github.com/jhotovy/android-ffmpeg

Take a look at the note about libffmpeginvoke in the second link in particular if you are planning to write your won wrapper.

One thing to be aware of is that compressions techniques, in particular video compression, often use an approach where one packet or frame is compressed relative to the frames before it (and sometimes even the frames after it). For example the first frame might be a 'key frame', the next five frames might just contain data to explain how they differ from the key frame, and the the seventh frame might be another key frame and so on. This means you generally want a 'chunk' which has all the required key frames if you are converting the chunk from one format to another.

I don't think you will have this problem converting from PCM to AAC audio, but it useful to be aware of the concept anyway.

like image 111
Mick Avatar answered Feb 01 '26 21:02

Mick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!