Kotlin - Android Audio Recording and Playback using MediaPlayer and MediaRecorder

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Making Runtime Permission Requests in AndroidWorking with the Google Maps Android API in Android Studio


You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book


This chapter will provide an overview of the MediaRecorder class and explain the basics of how this class can be used to record audio or video. The use of the MediaPlayer class to play back audio will also be covered. Having covered the basics, an example application will be created to demonstrate these techniques in action. In addition to looking at audio and video handling, this chapter will also touch on the subject of saving files to the SD card.




Playing Audio

In terms of audio playback, most implementations of Android support AAC LC/LTP, HE-AACv1 (AAC+), HE-AACv2 (enhanced AAC+), AMR-NB, AMR-WB, MP3, MIDI, Ogg Vorbis, and PCM/WAVE formats.

Audio playback can be performed using either the MediaPlayer or the AudioTrack classes. AudioTrack is a more advanced option that uses streaming audio buffers and provides greater control over the audio. The MediaPlayer class, on the other hand, provides an easier programming interface for implementing audio playback and will meet the needs of most audio requirements.

The MediaPlayer class has associated with it a range of methods that can be called by an application to perform certain tasks. A subset of some of the key methods of this class is as follows:

  • create() – Called to create a new instance of the class, passing through the Uri of the audio to be played.
  • setDataSource() – Sets the source from which the audio is to play.
  • prepare() – Instructs the player to prepare to begin playback.
  • start() – Starts the playback.
  • pause() – Pauses the playback. Playback may be resumed via a call to the resume() method.
  • stop() – Stops playback.
  • setVolume() – Takes two floating-point arguments specifying the playback volume for the left and right channels.
  • resume() – Resumes a previously paused playback session.
  • reset() – Resets the state of the media player instance. Essentially sets the instance back to the uninitialized state. At a minimum, a reset player will need to have the data source set again and the prepare() method called.
  • release() – To be called when the player instance is no longer needed. This method ensures that any resources held by the player are released.

In a typical implementation, an application will instantiate an instance of the MediaPlayer class, set the source of the audio to be played and then call prepare() followed by start(). For example:

val mediaPlayer = MediaPlayer()
 
mediaPlayer?.setDataSource("http://www.yourcompany.com/myaudio.mp3")
mediaPlayer?.prepare()
mediaPlayer?.start()

Recording Audio and Video using the MediaRecorder Class

As with audio playback, recording can be performed using a number of different techniques. One option is to use the MediaRecorder class, which, as with the MediaPlayer class, provides a number of methods that are used to record audio:

  • setAudioSource() – Specifies the source of the audio to be recorded (typically this will be MediaRecorder.AudioSource.MIC for the device microphone).
  • setVideoSource() – Specifies the source of the video to be recorded (for example MediaRecorder.VideoSource.CAMERA).
  • setOutputFormat() – Specifies the format into which the recorded audio or video is to be stored (for example MediaRecorder.OutputFormat.AAC_ADTS).
  • setAudioEncoder() – Specifies the audio encoder to be used for the recorded audio (for example MediaRecorder.AudioEncoder.AAC).
  • setOutputFile() – Configures the path to the file into which the recorded audio or video is to be stored.
  • prepare() – Prepares the MediaRecorder instance to begin recording.
  • start() - Begins the recording process.
  • stop() – Stops the recording process. Once a recorder has been stopped, it will need to be completely reconfigured and prepared before being restarted.
  • reset() – Resets the recorder. The instance will need to be completely reconfigured and prepared before being restarted.
  • release() – Should be called when the recorder instance is no longer needed. This method ensures all resources held by the instance are released.

A typical implementation using this class will set the source, output and encoding format and output file. Calls will then be made to the prepare() and start() methods. The stop() method will then be called when recording is to end, followed by the reset() method. When the application no longer needs the recorder instance, a call to the release() method is recommended:

You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book

val mediaRecorder = MediaRecorder()
 
mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
mediaRecorder?.setOutputFile(audioFilePath)
mediaRecorder?.prepare()
mediaRecorder?.start()
.
.
mediaRecorder?.stop()
mediaRecorder?.reset()
mediaRecorder?.release()

In order to record audio, the manifest file for the application must include the android.permission.RECORD_AUDIO permission:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

As outlined in the chapter entitled “Making Runtime Permission Requests in Android”, access to the microphone falls into the category of dangerous permissions. To support Android 6, therefore, a specific request for microphone access must also be made when the application launches, the steps for which will be covered later in this chapter.


About the Example Project

The remainder of this chapter will work through the creation of an example application intended to demonstrate the use of the MediaPlayer and MediaRecorder classes to implement the recording and playback of audio on an Android device.

When developing applications that make use of specific hardware features, the microphone being a case in point, it is important to check the availability of the feature before attempting to access it in the application code. The application created in this chapter will, therefore, also demonstrate the steps involved in detecting the presence of a microphone on the device.

Once completed, this application will provide a very simple interface intended to allow the user to record and playback audio. The recorded audio will need to be stored within an audio file on the device. That being the case, this tutorial will also briefly explore the mechanism for using SD Card storage.

Creating the AudioApp Project

Create a new project in Android Studio, entering AudioApp into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.

On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 14: Android 4.0 (IceCreamSandwich). Continue to proceed through the screens, requesting the creation of an Empty Activity named AudioAppActivity with a corresponding layout resource file named activity_audio_app.

Designing the User Interface

Once the new project has been created, select the activity_audio_app.xml file from the Project tool window and with the Layout Editor tool in Design mode, select the “Hello World!” TextView and delete it from the layout.

Drag and drop three Button views onto the layout. The positioning of the buttons is not of paramount importance to this example, though Figure 74-1 shows a suggested layout using a vertical chain.

Configure the buttons to display string resources that read Play, Record and Stop and give them view IDs of playButton, recordButton, and stopButton respectively.

Select the Play button and, within the Attributes panel, configure the onClick property to call a method named playAudio when selected by the user. Repeat these steps to configure the remaining buttons to call methods named recordAudio and stopAudio respectively.

Android studio audio ui.png 

Checking for Microphone Availability

Attempting to record audio on a device without a microphone will cause the Android system to throw an exception. It is vital, therefore, that the code check for the presence of a microphone before making such an attempt. There are a number of ways of doing this, including checking for the physical presence of the device. An easier approach, and one that is more likely to work on different Android devices, is to ask the Android system if it has a package installed for a particular feature. This involves creating an instance of the Android PackageManager class and then making a call to the object’s hasSystemFeature() method. PackageManager.FEATURE_MICROPHONE is the feature of interest in this case.

For the purposes of this example, we will create a method named hasMicrophone() that may be called upon to check for the presence of a microphone. Within the Project tool window, locate and double-click on the AudioAppActivity.kt file and modify it to add this method:

package com.ebookfrenzy.audioapp
 
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.pm.PackageManager
 
class AudioAppActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_audio_app)
    }
 
    private fun hasMicrophone(): Boolean {
        val pmanager = this.packageManager
        return pmanager.hasSystemFeature(
                PackageManager.FEATURE_MICROPHONE)
    }
}

You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book

Performing the Activity Initialization

The next step is to modify the activity to perform a number of initialization tasks. Remaining within the AudioAppActivity.kt file, modify the code as follows:

.
.
import android.media.MediaRecorder
import android.os.Environment
import android.view.View
import android.media.MediaPlayer
import android.Manifest
 
import kotlinx.android.synthetic.main.activity_audio_app.*
.
.
class AudioAppActivity : AppCompatActivity() {
 
    private var mediaRecorder: MediaRecorder? = null
    private var mediaPlayer: MediaPlayer? = null
 
    private var audioFilePath: String? = null
    private var isRecording = false
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_audio_app)
 
        audioSetup()
    }
 
    private fun audioSetup() {
 
        if (!hasMicrophone()) {
            stopButton.isEnabled = false
            playButton.isEnabled = false
            recordButton.isEnabled = false
        } else {
            playButton.isEnabled = false
            stopButton.isEnabled = false
        }
 
        audioFilePath = Environment.getExternalStorageDirectory()
                .absolutePath + "/myaudio.3gp" 
    }
.
.
}

The added code begins by obtaining references to the three button views in the user interface. Next, the previously implemented hasMicrophone() method is called to ascertain whether the device includes a microphone. If it does not, all the buttons are disabled, otherwise only the Stop and Play buttons are disabled.

The next line of code needs a little more explanation:

audioFilePath = Environment.getExternalStorageDirectory()
                .absolutePath + "/myaudio.3gp"

The purpose of this code is to identify the location of the SD card storage on the device and to use that to create a path to a file named myaudio.3gp into which the audio recording will be stored. The path of the SD card (which is referred to as external storage even though it is internal to the device on many Android devices) is obtained via a call to the getExternalStorageDirectory() method of the Android Environment class.

When working with external storage it is important to be aware that such activity by an application requires permission to be requested in the application manifest file. For example:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Implementing the recordAudio() Method

When the user touches the Record button, the recordAudio() method will be called. This method will need to enable and disable the appropriate buttons and configure the MediaRecorder instance with information about the source of the audio, the output format and encoding, and the location of the file into which the audio is to be stored. Finally, the prepare() and start() methods of the MediaRecorder object will need to be called. Combined, these requirements result in the following method implementation in the AudioAppActivity.kt file:

fun recordAudio(view: View) {
    isRecording = true
    stopButton.isEnabled = true
    playButton.isEnabled = false
    recordButton.isEnabled = false
 
    try {
        mediaRecorder = MediaRecorder()
        mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
        mediaRecorder?.setOutputFormat(
                MediaRecorder.OutputFormat.THREE_GPP)
        mediaRecorder?.setOutputFile(audioFilePath)
        mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
        mediaRecorder?.prepare()
    } catch (e: Exception) {
        e.printStackTrace()
    }
    mediaRecorder?.start()
}

You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book

Implementing the stopAudio() Method

The stopAudio() method is responsible for enabling the Play button, disabling the Stop button and then stopping and resetting the MediaRecorder instance. The code to achieve this reads as outlined in the following listing and should be added to the AudioAppActivity.kt file:

fun stopAudio(view: View) {
 
    stopButton.isEnabled = false
    playButton.isEnabled = true
 
    if (isRecording) {
        recordButton.isEnabled = false
        mediaRecorder?.stop()
        mediaRecorder?.release()
        mediaRecorder = null
        isRecording = false
    } else {
        mediaPlayer?.release()
        mediaPlayer = null
        recordButton.isEnabled = true
    }
}

Implementing the playAudio() method

The playAudio() method will simply create a new MediaPlayer instance, assign the audio file located on the SD card as the data source and then prepare and start the playback:

fun playAudio(view: View) {
    playButton.isEnabled = false
    recordButton.isEnabled = false
    stopButton.isEnabled = true
 
    mediaPlayer = MediaPlayer()
    mediaPlayer?.setDataSource(audioFilePath)
    mediaPlayer?.prepare()
    mediaPlayer?.start()
}

Configuring and Requesting Permissions

Before testing the application, it is essential that the appropriate permissions be requested within the manifest file for the application. Specifically, the application will require permission to record audio and to access the external storage (SD card). Within the Project tool window, locate and double-click on the AndroidManifest.xml file to load it into the editor and modify the XML to add the two permission tags:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ebookfrenzy.audioapp" >
 
    <uses-permission android:name=
                 "android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".AudioAppActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name=
                     "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The above steps will be adequate to ensure that the user enables these permissions when the app is installed on devices running versions of Android pre-dating Android 6.0. Both microphone and external storage access are categorized in Android as being dangerous permissions because they give the app the potential to compromise the user’s privacy. In order for the example app to function on Android 6 or later devices, therefore, code needs to be added to specifically request these two permissions at app runtime.

You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book

Edit the AudioAppActivity.kt file and begin by adding some additional import directives and constants to act as request identification codes for the permissions being requested:

.
.
import android.widget.Toast
import android.support.v4.content.ContextCompat
import android.support.v4.app.ActivityCompat
.
.
class AudioAppActivity : AppCompatActivity() {
.
.
    private val RECORD_REQUEST_CODE = 101
    private val STORAGE_REQUEST_CODE = 102
.
.

Next, a method needs to be added to the class, the purpose of which is to take as arguments the permission to be requested and the corresponding request identification code. Remaining with the AudioAppActivity.kt class file, implement this method as follows:

private fun requestPermission(permissionType: String, requestCode: Int) {
    val permission = ContextCompat.checkSelfPermission(this,
            permissionType)
 
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                arrayOf(permissionType), requestCode
        )
    }
}

Using the steps outlined in the “Making Runtime Permission Requests in Android” chapter of this book, the above method verifies that the specified permission has not already been granted before making the request, passing through the identification code as an argument.

When the request has been handled, the onRequestPermissionsResult() method will be called on the activity, passing through the identification code and the results of the request. The next step, therefore, is to implement this method within the AudioAppActivity.kt file as follows:

override fun onRequestPermissionsResult(requestCode: Int,
             permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
        RECORD_REQUEST_CODE -> {
 
            if (grantResults.isEmpty() || grantResults[0] 
               != PackageManager.PERMISSION_GRANTED) {
 
                recordButton.isEnabled = false
 
                Toast.makeText(this,
                        "Record permission required",
                        Toast.LENGTH_LONG).show()
            } else {
                requestPermission(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        STORAGE_REQUEST_CODE)
            }
            return
        }
        STORAGE_REQUEST_CODE -> {
 
            if (grantResults.isEmpty() || grantResults[0] 
                  != PackageManager.PERMISSION_GRANTED) {
                recordButton.isEnabled = false
                Toast.makeText(this,
                        "External Storage permission required",
                        Toast.LENGTH_LONG).show()
            }
            return
        }
    }
}

The above code checks the request identifier code to identify which permission request has returned before checking whether or not the corresponding permission was granted. If the user grants permission to access the microphone the code then proceeds to request access to the external storage. In the event that either permission was denied, a message is displayed to the user indicating the app will not function. In both instances, the record button is also disabled.

All that remains prior to testing the app is to call the newly added requestPermission() method for microphone access when the app launches. Remaining in the AudioAppActivity.kt file, modify the audioSetup() method as follows:

private fun audioSetup() {
 
    if (!hasMicrophone()) {
        stopButton.isEnabled = false
        playButton.isEnabled = false
        recordButton.isEnabled = false
    } else {
        playButton.isEnabled = false
        stopButton.isEnabled = false
    }
 
    audioFilePath = Environment.getExternalStorageDirectory()
            .absolutePath + "/myaudio.3gp"
 
    requestPermission(Manifest.permission.RECORD_AUDIO,
            RECORD_REQUEST_CODE)
 
}

Testing the Application

Compile and run the application on an Android device containing a microphone, allow the requested permissions and touch the Record button. After recording, touch Stop followed by Play, at which point the recorded audio should play back through the device speakers. If running on Android 6.0 or later, note that the app requests permission to use the external storage and to record audio when first launched.

Summary

The Android SDK provides a number of mechanisms for the implementation of audio recording and playback. This chapter has looked at two of these, in the form of the MediaPlayer and MediaRecorder classes. Having covered the theory of using these techniques, this chapter worked through the creation of an example application designed to record and then play back audio. In the course of working with audio in Android, this chapter also looked at the steps involved in ensuring that the device on which the application is running has a microphone before attempting to record audio. The use of external storage in the form of an SD card was also covered.


You are reading a sample chapter from the Android Studio 3.0 / Android 8 Edition book.

Purchase the fully updated Android Studio 3.2 / Android 9 / Jetpack Edition of this publication in eBook ($29.99) or Print ($45.99) format

Android Studio 3.2 Development Essentials - Kotlin Edition Print and eBook (ePub/PDF/Kindle) editions contain 96 chapters and over 800 pages

Buy Print Preview Book



PreviousTable of ContentsNext
Making Runtime Permission Requests in AndroidWorking with the Google Maps Android API in Android Studio