Harsh
Harsh

Reputation: 356

Play Sequence of audio files with SoundPool class of Android

I have three sound files in raw folder. I want to play them one after another with no delay between them. I tried them playing with below code but all three files are playing together, not one after another. All three files are less then 50 kb. I also want to change their playback rate, that's why I am using SoundPool instead of MediaPlayer.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
     final int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 

     mSoundPoolMap.put(1, mSoundPool.load(this, R.raw.nancy, 1));
     mSoundPoolMap.put(2, mSoundPool.load(this, R.raw.cymbal, 1));
     mSoundPoolMap.put(3, mSoundPool.load(this, R.raw.njs, 1));

    Button SoundButton = (Button)findViewById(R.id.Button);
    SoundButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 0, 1f);
            mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);
            mSoundPool.play(mSoundPoolMap.get(3), streamVolume, streamVolume, 1, 0, 1f);

        }
    });

Upvotes: 2

Views: 2930

Answers (2)

Tabish Saifullah
Tabish Saifullah

Reputation: 19

I got same problem, so I create my own logic. Her it is:-

I have three audio file in my raw folder. I want to run these three file in sequence. To do this I follow this flow:-

Step1:- On clicking Button a thread will be generated.

Step2:- Thread will start a timer of 20 milliseconds.

Step3:- In timer it will check if media player is not playing. If media player is not playing it will call a function to start media player.

Step4:- In function which will start the media player have switch logic which will decide which audio file will be ON ( with help of a variable v).

Here is my code:-

public class MainActivity extends Activity {
int v=0;
TimerTask time;

MediaPlayer mp ;
Button btn;
void playmp(int a)
{

    switch (a)
    {
        case 0: 
        {
            mp = MediaPlayer.create(this,R.raw.a);
            mp.start();
            v++;
            break;

        }

   case 1: 
   {
    mp = MediaPlayer.create(this,R.raw.b);
    mp.start();
    v++;
    break;

   }
   case 2: 
   {
    mp = MediaPlayer.create(this,R.raw.c);
    mp.start();
    v++;
    break;


   }
    }
   }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn= (Button) findViewById(R.id.button1);
        mp = MediaPlayer.create(this,R.raw.a);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                //Creating Thread

                Thread thread = new Thread()
                {
                    @Override
                    public void run() {
                        try {

                                sleep(1);

                                // Body Of Timer
                                time = new TimerTask() {
                                    @Override
                                    public void run() {

                                        //Perform background work here
                                    if(!mp.isPlaying())
                                    {

                                    playmp(v);  

                                    }

                                    }
                                };
                                 //Starting Timer
                                Timer timer = new Timer();
                                timer.schedule(time, 10, 20);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };

                thread.start();



            }
        });
    }

where "a","b"and "c" are my audio file name . If you want repeat audio after completing all three , replace this:-

case 2: 
       {
        mp = MediaPlayer.create(this,R.raw.c);
        mp.start();
        v++;
        break;


       }

by this

case 2: 
       {
        mp = MediaPlayer.create(this,R.raw.c);
        mp.start();
        v=0;
        break;


       }

Upvotes: 0

SD_Guru
SD_Guru

Reputation: 339

In order to do what you're trying to do, you'll probably have to create your own custom AsyncTask that takes in an array of SoundPool objects (or perhaps a HashMap), the implementation along the lines of:

private class SoundPoolTask extends AsyncTask <Soundpool, String, String>
{
    @Override
    protected String doInBackground(SoundPool... params) {
        try {               
            //For each SoundPool object
               // int soundLength = however long the SoundPool object is
               // Play the sound
               // Thread.sleep(soundLength);        
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {}

    @Override
    protected void onPostExecute(String result) {
      //Clean up your class objects
    }
}

You'll probably have to do some sort of calculation with the original length of the sound and the playrate to determine how long each sound is. I hope this helps!

Upvotes: 1

Related Questions