Audio Sampler Example

This example shows how to record a sound using the Android media APIs with parameters selected from a user interface.

A screenshot of the application.

We import the classes and modules needed by our application. The most relevant classes are those from the module.

from import File, FileOutputStream
from java.lang import Math, Runnable, Thread
from java.nio import ByteBuffer
from java.text import SimpleDateFormat
from java.util import Date
from import AudioFormat, AudioRecord, MediaRecorder
from android.os import Environment
from android.view import View, ViewGroup
from android.widget import AdapterView, Button, GridLayout, LinearLayout, \
                           Spinner, TextView
from serpentine.activities import Activity
from serpentine.adapters import StringListAdapter

The AudioSamplerActivity class is derived from the standard Activity class and represents the application. Android will create an instance of this class when the user runs it.

class AudioSamplerActivity(Activity):

    __interfaces__ = [AdapterView.OnItemSelectedListener, Runnable,

The class declares that it implements the AdapterView.OnItemSelectedListener, View.OnClickListener and Runnable interfaces.

The initialisation method simply calls the corresponding method in the base class.

    def __init__(self):
        self.recording = False
        self.encodings = [AudioFormat.ENCODING_PCM_8BIT,
        self.sampleRates = [9600, 22050, 44100, 48000]
        self.channels = [AudioFormat.CHANNEL_IN_MONO,
        self.encoding = AudioFormat.ENCODING_PCM_8BIT
        self.sampleRate = 9600 = AudioFormat.CHANNEL_IN_MONO

The onCreate method calls the corresponding method in the base class to help set up the activity, and we set up the user interface.

    def onCreate(self, bundle):
        Activity.onCreate(self, bundle)
        channelLabel = TextView(self)
        channelLabel.setText("Input channels:")
        self.channelSpinner = Spinner(self)
        adapter = StringListAdapter(["Mono", "Stereo"])
        sampleRateLabel = TextView(self)
        sampleRateLabel.setText("Sample rate:")
        self.sampleRateSpinner = Spinner(self)
        l = []
        for i in self.sampleRates:
        adapter = StringListAdapter(l)
        encodingLabel = TextView(self)
        encodingLabel.setText("Output encoding:")
        self.encodingSpinner = Spinner(self)
        adapter = StringListAdapter(["8-bit", "16-bit"])
        grid = GridLayout(self)
        lp = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        lp.bottomMargin = 12

We create a button with a "Play sound" label and register the activity as its listener for click callbacks.

        self.button = Button(self)
        self.button.setText("Start recording")

We also create a text view to show the location of the last file written.

        self.textView = TextView(self)

The button and text view are placed in a vertical layout which is used as the main content in the activity.

        layout = LinearLayout(self)

The next two methods are the implementation of the AdapterView.OnItemSelectedListener interface.

When an item in one of the spinners is selected, this method is called with the parent view (the spinner in this case), the view displaying the item data, the position of the item in the spinner and the item's ID. We use the parent and position to extract data from the relevant list that we defined earlier before calling the tryFormat method to see if the parameters are valid.

    def onItemSelected(self, parent, view, position, id):
        if parent == self.channelSpinner:
   = self.channels[position]
        elif parent == self.sampleRateSpinner:
            self.sampleRate = self.sampleRates[position]
        elif parent == self.encodingSpinner:
            self.encoding = self.encodings[position]

If no item is selected then do nothing, keeping the existing parameters.

    def onNothingSelected(self, parent):

The following method tries the current set of parameters and sets up an AudioRecord instance if successful.

    def tryFormat(self):
        bufferSize = AudioRecord.getMinBufferSize(self.sampleRate,
  , self.encoding)
        if bufferSize == AudioRecord.ERROR_BAD_VALUE:

We use the existing parameters for the audio data we want to record. If we cannot use these parameters, we simply return early and disable the record button.

We create a byte array of the required size, allocating at least enough data for one second of mono, 8-bit audio data - less if more channels or 16-bit audio is being recorded.

        bufferSize = Math.max(bufferSize, self.sampleRate)
        self.audioBuffer = array(byte, bufferSize)

We create an AudioRecord instance, passing information about the sample data to ensure that it will be played correctly.

        self.recorder = AudioRecord(MediaRecorder.AudioSource.MIC,
            self.sampleRate,, self.encoding, bufferSize)
        if self.recorder.getState() == AudioRecord.STATE_INITIALIZED:

The onClick method is called whenever the button defined above is clicked. If the activity was registered as a listener with other buttons then we would distinguish between them using the View object passed to this method.

    def onClick(self, view):
        if not self.recording:

If we are not recording when the button is pressed then we create a file to write to, open an output stream to direct data to that file, and we start recording.

            self.button.setText("Stop recording")
            self.file = self.createFile()
   = FileOutputStream(self.file)
            self.recording = True

We also create a thread, passing the instance of this class to it as a Runnable for it to execute, and start it.

            self.thread = Thread(self)

Otherwise, we stop the recorder and interrupt the thread in order to stop processing audio data. We reset the button and print the name of the file we created to the TextView.

            self.button.setText("Start recording")
            self.recording = False
            self.textView.setText("Written " + self.file.getAbsolutePath())

As a Runnable, this class provides a run method that can be executed in another context, and we use a thread to ensure that this method is run in the background while the application continues to respond to the user in the foreground. The method reads data from the audio buffer and writes it to an output stream as long as the recording flag is set and the thread is running.

    def run(self):
        while self.recording:
            size =, 0, len(self.audioBuffer))
            if size > 0:

The final method is used to conveniently create a file in the device's external storage area, returning a File object if successful or None if not.

    @args(File, [])
    def createFile(self):
        if Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED:
            return None

If no external storage is mounted then return None immediately.

We obtain the directory on the external storage device that is used to store music.

        storageDir = Environment.getExternalStoragePublicDirectory(

If possible, we create a subdirectory for this example, returning None to indicate failure.

        subdir = File(storageDir, "AudioSampler")
        if not subdir.exists():
            if not subdir.mkdirs():
                return None

Finally, we use the SimpleDateFormat class to create a file name based on the current date, then use this to create a File object which we return.

        dateString = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        return File(subdir, dateString + ".raw")