This is part of an ongoing series of posts about Homesick, my bachelor project.

The Android side of the client is hosted here.

Last post, I set up a button in my app to record and store audio. This time, I’ve set up a play button that will play the audio I recorded.

Once again, the official API guides from Google were very helpful.

The code is very similar to that of recording audio, from last post:

```java RecordingActivity.java https://github.com/carmenh/homesick-app/blob/master/app/src/main/java/com/carmenh/homesick/RecordingActivity.java Source private void startPlaying() { mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(mFileName); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(LOG_TAG, “prepare() failed”); } }


First, we instantiate an object of type android.media.MediaPlayer. This object will be in charge of playing audio we've recorded. In the next line, its data source is set to the audio file at the path we recorded to. This means that the player will play this file.

Finally, it's prepared to start playing and start() is called on it to start playing. Simple!

Pressing the Play button again will stop:

```java RecordingActivity.java https://github.com/carmenh/homesick-app/blob/master/app/src/main/java/com/carmenh/homesick/RecordingActivity.java Source
    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

That’s it! Now I have a simple prototype that records and plays audio on Android.

Buy me a coffee