Applying MediaCodec On An Open Source Android Audio Player

OpenMax (in short OMX) is a royalty free, non-proprietary, and a cross platform set of routines. It targets the interfaces of multimedia processing. This was designed with efficiency that makes large quantities of multimedia data to be processed in a controlled and optimised manner which includes video and audio codecs, graphics and so on. This is mostly intended for some embedded devices. OpenMax have three layers of interfaces, namely: application layer (AL), integration layer (IL) and development layer (DL). The interface between media framework like StageFright or MediaCodec API on Android is the OpenMax IL.

To know more about this follow this link: https://en.wikipedia.org/wiki/OpenMAX

MediaCodec API was introduced with Android 16 and is said to be useful in accessing low-level media codec, namely: encoder/decoder components which is a part of the Android OS. There are three reasons that makes this convenient. These are the following reasons:

  • Licensing complications can be avoided. Fees can be avoided by applying the built-in AAC codecs by the MediaCodec Class since AAC requires all manufacturers or developers of AAC codecs to have a patent license.
  • Long established computational hotspots can be replaced within the standardized media codecs. You can simply use built-in, standardised versions instead of incorporating codecs on each app.
  • Highly optimised hardware accelerated codec routines will be accessible.

The Incorporated codecs on Android

There are numerous types of codecs and plenty of codecs in most android devices that is now on the market.

The codes below will iterate through the built-in codecs.

 

<li></li>
<li>int numCodecs = MediaCodecList.getCodecCount();</li>
<li>for (int i = 0; i &lt; numCodecs; i++) {</li>
<li>MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);</li>
<li><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><strong>String</strong></a>[] types = codecInfo.getSupportedTypes();</li>
<li>}</li>
<li></li>

 

 

Interpreting MediaCodec

The amazing thing about the new API is that it permits you to feed a input buffer and getting the outcome of the output buffer for only a few steps. You’re encoding/decoding will be accelerated depending on some specific needs. You cant find anymore better performance than this one putting on account that OMX does some of the work that is used in dedicated hardware ans this does mean an improved speed.

To get you started, we shall show you the concept and some sample codes. Let’s say fopr example that we need to create a media playes for AAC and also a MP# audios files but before that we will use the free and fast embedded decoders. The code that is shown here will work with any format supported by your device.  By using MediaCodec API, we can do all the things that is mentioned above.

To start, pleases click this link : http://developer.android.com/reference/android/media/MediaCodec.html

There are also some classes we need to explore, namely;  MediaExtractor (http://developer.android.com/reference/android/media/MediaExtractor.html) which reads bytes from the encoded data whether its online streams, embedded resources or local files. Another one is the MediaFormat (http://developer.android.com/reference/android/media/MediaFormat.html) which is useful when reading a type of encoded files and every details that is connected to the content.  This article is mainly focus on demonstrating on how to build an AudioPlayer, but the API which is provided with Android 16 (or can be a newer version) can also be used in creating a video player by simply following the same process and show the frames by simply adding a surface view. Lastly, decoding audio to PCM, is is recommended to use the AudioTrack (http://developer.android.com/reference/android/media/AudioTrack.html) API to have the sounds to actually come out of the speakers.

The Audio Player’s Construction

There are a lot of ways to create an audio player though it is definitely recommended to have a player class that accepts the audio data source, that also includes some basic commands like start,pause,stop and given that the events is through an interface or a handler, to notify us on the status of progress. Playback can be done all at once on the same or separate thread that is called by the player object. Mostly the latter is used but it really depends on the particular application.

To be more convenient, a software is designed as an Android Library project which can easily be added on your work and it is named the “OpenMXPlayer”. The functional diagram is shown below :

mediacodec

Having the interface exposed by the MediaExtractor, a large variation of the content can be fed on the player such as live streams , online podcasts or local files. Embedded resources (in res/raw or in assets) can also work. We now have a player that is higly versatile, optimised and fast that can manage popular files that includes Mp3, AAC, Vorbis, WMA, etc.

The client can associate an interface and get notified:

 

<li></li>
<li><strong>public</strong> <strong>interface</strong> PlayerEvents {</li>
<li><strong>public</strong> void onStart(<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><strong>String</strong></a> mime, int sampleRate,int channels, long duration);</li>
<li><strong>public</strong> void onPlay();</li>
<li><strong>public</strong> void onPlayUpdate(int percent, long currentms, long totalms);</li>
<li><strong>public</strong> void onStop();</li>
<li><strong>public</strong> void onError();</li>
<li>}</li>
<li></li>

 

 

You can have an advanced audio content using just a few lines of code. The codes is:‘

 

<li></li>
<li>OpenMXPlayer player = <strong>new</strong> OpenMXPlayer();</li>
<li>setDataSource("/mnt/sdcard/Metallica - Nothing else matters.mp3");</li>
<li>play();</li>
<li></li>

 

If you want to set the listener up, its fine but you’ll definitely want to get notifications about the progress and events. You can simply put an URL address of a radio station or other online audio resources instead of just a local MP3.

 

 

Leave a Comment: