8.1 Audio in Android

Before diving deep into Android audio framework we shall have a glance over android multimedia framework. Let us not waste time because I am very excited to do this tutorial as it is my favorite section, the multimedia.

8.1.1 Introduction to Android multimedia

When we talk about multimedia it is the most important factor in deciding the performance and utility of mobile devices. If the sound of device is not according to the comfort level of our ears we readily decide to drop that device and buy a new one. Because of a superb multimedia framework of android it shares one of the largest market shares when we consider smart phones or tablet computers market. Multimedia is concerned with processing of audio/video input and output. There are certain functionalities which are meant to be satisfied by multimedia framework:

  • Storing audio and video section of  media
  • Playback audio and video
  • Record audio and video

If we see the architecture of android framework we can see media framework in libraries. You must be wondering what we have to know about the framework. Well we will know about the codec and supported file formats.

Figure Android framework

                                                                 Figure - Android framework

Codec/decode can be rightly defined as a computer program or device able to encode and/or decode digital data stream or signal. A codec encodes the data stream or signal for transmission, storage or encryption and decode at other end for playback or editing purpose.

Figure Codec

                                                                            Figure -  Codec

When we record a multimedia file it is in raw state which accumulates lot of space in memory which is a ruthless misuse of expensive space so we need a codec to compress that huge raw data into a small sophisticated format for storage and transfer. Another important friend came in limelight i.e. the file format. File format defines a strategy for co-existence of different data elements and metadata in a stream or computer file. File format is also known as wrapper format or container.  They always contain coded audio, coded video, topic information, subtitles, etc. From various sources we can conclude that  the most common audio and video codec are as follows:

  1. Audio Codec
    1. AAC
    2. MPEG-4: Audio part 3 and subpart 4
    3. AC3, Dolby Digital codec
    4. AMR: Adaptive Multi-rate audio codec
    5. AMR-WB: Adaptive Multi-rate wide band audio codec
    6. MP2:MPEG ½ audio layer II
    7. MP3: MPEG2 Audio layer III
    8. WMA: Windows Media Audio
    9. RealAudio, Real Network
    10. ALAC: Apple Lossless Audio Codec
  2. Video Codec
    1. MPEG –1, MPEG –1 Part 2
    2. MPEG –2/H.262, MPEG –2 part 2
    3. MPEG –4 ASP, MPEG –4 Part 2
    4. MPEG –4 AVC/H.264, MPEG –4 Part 10
    5. VC –1: Initially developed as a propriety video format by Microsoft before it was released as a formal SMPTE standard video format. The informal name of SMTP 421M video codec standard
    6. VC –2: An open and royalty-free video compression format, Dirac. 2010 the SMPTE standardized Dirac Pro as VC –2.
    7. VC –3: A lessee high-definition video post-production codec engineered for multi-generation compositing with reduced storage and bandwidth requirements.The DNXHD codec was submitted to the SMPTE organization as the framework for the VC-3 family of standard.


After referring various sources, the multimedia framework can be summarized in following diagram:

 Android Multimedia Framework

                                                                          Figure - Android Multimedia Framework

Let us now focus on audio section of multimedia.

8.2.2 Android Audio

We have the MediaPlayer class which primarily controls the playback of audio and video files. We can use MediaPlayer to play the files stored on local files, content providers, application resources or streamed from network URI. First step is to initialize the Media Player to play the media. Then prepare MediaPlayer to play back the audio file. Start the playback. Before completion we may pause it or stop and thus playback is accomplished. Let us create a short and self explanatory example which will demonstrate the use of MediaPlayer.

8.2.3 Android Audio Example

Create a project and name it as you like. I am naming it AudioExample app. This time we would code both our xml file and main activity file. We will create a small application which will play the song when you press the button saying play and it will stop playing song when you press the button saying stop respectively. Now open the activity_main.xml file and code it as shown in the following listing:

android:text="Hello Readers"/>

Figure - Activity_main.xml file of Android Audio Example

The graphical layout of app would look similar to the following snapshot:

Graphical layout of Android Audio Example

Figure - Graphical layout of Android Audio Example

Now you have to save a mp3 file on the resource folder of application. Create a new folder and name it raw under res folder i.e. /res/raw and save the music file inside raw folder. Open the main activity file and code it as shown in the following listing:

publicclassMainActivityextends Activity implementsOnClickListener {
    protectedvoidonCreate(Bundle savedInstanceState) {
        myplayer = MediaPlayer.create(this, R.raw.happy);
        Button stop = (Button) findViewById(R.id.button2);
        Button play = (Button) findViewById(R.id.button1);
        // myplayer.start();
    publicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
    publicvoidonClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.button1:
        case R.id.button2:

Figure - main activity file of  Android Audio Example

Now as you always do, open the manifest file and cross check with following listing:


Figure -  manifest file oAndroid Audio Example

So guys we are done with coding part. Create a suitable emulator and run the application. Press the Play button to listen to the song. Output should be similar to the following snapshot:

Press Play button to listen to song

Figure - Press Play button to listen to song

If you are done with listening then you can stop this by pressing the button saying stop. Output should be similar to the following snapshot:

Figure - Press Stop button to stop the music

Figure - Press Stop button to stop the music

We are done with audio. Hope you liked this section. See you in the next section. Till then keep practicing. Happy App Developing!!!