Saturday, November 27, 2010

Play foreground and background music using SoundPool and MediaPlayer

Merge the previous exercises "A simple exercise to play MIDI audio using MediaPlayer" and "Play audio resources using SoundPool", to implement a app to play audio using both MediaPlayer and SoundPool.

Play foreground and background music using SoundPool and MediaPlayer

Copy midi and ogg sound file into res/raw folder.

Modify main.xml to have four buttons to control the playing of the sounds.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/playmediaplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY MediaPlayer -"
/>
<Button
android:id="@+id/pausemediaplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE MediaPlayer -"
/>
<Button
android:id="@+id/playsoundpool"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY SoundPool -"
/>
<Button
android:id="@+id/pausesoundpool"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE SoundPool -"
/>
</LinearLayout>


Modify AndroidAudioPlayer.java.
package com.exercise.AndroidAudioPlayer;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidAudioPlayer extends Activity {

MediaPlayer mediaPlayer;
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
int soundID = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mediaPlayer = MediaPlayer.create(this, R.raw.midi_sound);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
//soundPoolMap.put(soundID, soundPool.load(this, R.raw.midi_sound, 1));
soundPoolMap.put(soundID, soundPool.load(this, R.raw.fallbackring, 1));

Button buttonPlayMediaPlayer = (Button)findViewById(R.id.playmediaplayer);
Button buttonPauseMediaPlayer = (Button)findViewById(R.id.pausemediaplayer);
Button buttonPlaySoundPool = (Button)findViewById(R.id.playsoundpool);
Button buttonPauseSoundPool = (Button)findViewById(R.id.pausesoundpool);
buttonPlayMediaPlayer.setOnClickListener(buttonPlayMediaPlayerOnClickListener);
buttonPauseMediaPlayer.setOnClickListener(buttonPauseMediaPlayerOnClickListener);
buttonPlaySoundPool.setOnClickListener(buttonPlaySoundPoolOnClickListener);
buttonPauseSoundPool.setOnClickListener(buttonPauseSoundPoolOnClickListener);
}

Button.OnClickListener buttonPlayMediaPlayerOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
Toast.makeText(AndroidAudioPlayer.this,
"soundPool.pause()",
Toast.LENGTH_LONG).show();
}
}
};

Button.OnClickListener buttonPauseMediaPlayerOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
Toast.makeText(AndroidAudioPlayer.this,
"soundPool.pause()",
Toast.LENGTH_LONG).show();
}
}
};

Button.OnClickListener buttonPlaySoundPoolOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float leftVolume = curVolume/maxVolume;
float rightVolume = curVolume/maxVolume;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(soundID, leftVolume, rightVolume, priority, no_loop, normal_playback_rate);

Toast.makeText(AndroidAudioPlayer.this,
"soundPool.play()",
Toast.LENGTH_LONG).show();
}
};

Button.OnClickListener buttonPauseSoundPoolOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundPool.pause(soundID);
Toast.makeText(AndroidAudioPlayer.this,
"soundPool.pause()",
Toast.LENGTH_LONG).show();
}
};

}


Download the files.

1 comment:

Apikoros said...

Great! Thanks for this.