Handling Media with Android - Tutorial | Android Tutorial

Overview PAGE TOP

This tutorial describes the usage of the Android media API for playing and recording sound. It is based on Eclipse 3.7, Java 1.6 and Android 4.0.3 (Ice Cream Sandwich).

1.1. Playing sound PAGE TOP

Android provides two main API's for playing sounds. The first one via the SoundPool class and the other one via the MediaPlayer class.

SoundPool can be used for small audio clips. It can repeat sounds and play several sounds simultaneously. The sound files played with SoundPool should not exceed 1 MB.

SoundPool does load the file asynchronously. As of Android API8 it is possible to check if the loading is complete via a OnLoadCompleteListener.

Android supports different audio streams for different purposes. The phone volume button can be configured to control a specific audio stream, e.g. during a call the volume button allow increase / decrease the caller volume. To set the button to control the sound media stream set the audio type in your application.

context.setVolumeControlStream(AudioManager.STREAM_MUSIC);

MediaPlayer is better suited for longer music and movies.

1.2. MediaRecorder PAGE TOP

The android.media.MediaRecorder class can be used to record audio and video. To use MediaRecorder you need to set the source device and the format.

1.3. Adding to the Media library PAGE TOP

You can add new Media to the Android media library. Via an Intent you can tell the media application on the device that new content is available. The following demonstrates that.

If you touch your activity your sound should be played. It will use the current volume settings.

4. Tutorial: Record media via MediaRecorder PAGE TOP

We will create an application which will start playing a sound once the finger touches the display. Create an Android project called "de.vogella.android.media.soundrecording" with an activity called RecordSound.

Add to the AndroidManifest.xml file the permission to write to the SD card and to record audio data.

You should be able to record a sound, if you press the "Start Recording" button. Once you press the stop recording button, the recording will be stopped and the file will be added to your media library.

Confirm vote

Please check your content

It appears as though you entered a website, phone number, or email address.
Please, do not exchange your Skype/email contact on the platform, for any questions regarding the product do
not hesitate to make your demand here, your question could be useful for future buyers on the platform.
Moreover, writing on the comments area allows us to have a record of the issues and to act in case of
any problem.