Android provides two main API’s for playing sounds.
- SoundPool
- MediaPlayer
SoundPool
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.
Example Source Code
Load Sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.beep, 1);
Play Sound
// Is the sound loaded if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); }
Stopping Sound
soundPool.stop(soundID); soundID = soundPool.load(this, R.raw.beep, counter);
MediaPlayer
Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio,video e.t.c. MediaPlayer is better suited for longer music and movies.
Initialize
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
Start/Stop Music
mediaPlayer.start(); mediaPlayer.pause();
On call to start() method, the music will start playing from the beginning. If this method is called again after the pause() method, the music would start playing from where it is left and not from the beginning.
Restart Music
In order to start music from the beginning, you have to call reset() method. Its syntax is given below.
mediaPlayer.reset();
Move to a particular position
mediaPlayer.seekTo(10);
This method takes an integer, and move song to that particular second
Below are the other methods available.
getCurrentDuration()
This method returns the current position of song in milliseconds
getDuration()
This method returns the total time duration of song in milliseconds
release()
This method releases any resource attached with MediaPlayer object
setVolume(float leftVolume, float rightVolume)
This method sets the up down volume for this player
setDataSource(FileDescriptor fd)
This method sets the data source of audio/video file
selectTrack(int index)
This method takes an integer, and select the track from the list on that particular index
getTrackInfo()
This method returns an array of track information
MediaRecorder
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
You may need two permissions in the Android Manifest for recording audio.
Add to the AndroidManifest.xml file the permission to write to the SD card and to record audio data.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO" />
Start Recording
File sampleDir = Environment.getExternalStorageDirectory(); try { audiofile = File.createTempFile("my_sound", ".3gp", sampleDir); } catch (IOException e) { Log.e(TAG, "Error while accessing SDCARD"); return; } recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(audiofile.getAbsolutePath()); recorder.prepare(); recorder.start();
Stop Recording
recorder.stop(); recorder.release();
Done.