Android the fastest way to load and play sound in application

Android-Droid picture Android-Droid · Sep 1, 2011 · Viewed 8.7k times · Source

I'm working on a project in which I have to load 6 different sounds in one activity and play all sound on button click.The sound file are not so big,but the problem is that maybe they will be more.So my question is which is the fastest way to load sound files in a single activity.For test purposes I used res/raw folder to store the sound files and tried with two different method to play the files,but the result did not satisfied me.Here is the two different types of code :

Method 1:

Button first = (Button) findViewById(R.id.one);
Button second = (Button) findViewById(R.id.two);
first.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumtwo);
                mp.start();
                mp.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        mp.release();
                    }

                });
            }
        });

second.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumone);
                mp.start();
                mp.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        mp.release();
                    }

                });
            }
        });

Method 2:

    private SoundPool spool;
    private int soundID,soundID2;

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

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundID = spool.load(this, R.raw.drumone, 1);
        soundID2 = spool.load(this, R.raw.drumtwo, 1);

        Button three = (Button) findViewById(R.id.three);



        three.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Sound();

            }
        });



    }

    public void Sound(){
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        spool.play(soundID, volume, volume, 1, 0, 1f);
    };

But the problem is that the both methods are slow..I need to find a way a faster way to play the sounds..but now there is like almost a second after I click the button to play the sound.

Can you give me some advice how to play the sounda faster.Should I load them into a cache when the application starts or save them in database or somewhere else (I don't think database is a good option as a matter of fact,but I want to hear some suggestions).Or maybe load them from assets folder,but I think it still going to be slow.

So any ideas or suggestions? Thanks in advance!

Answer

Reed picture Reed · Sep 1, 2011

You could create the media players in your onCreate and then the buttons will just make them play. That would be the easiest solution, I would say.

Alternatively, depending on how important it is to you and how much work you want to do, you could consider using JetPlayer:

here's android development page for media:
http://developer.android.com/guide/topics/media/index.html

The page on the JetPlayer class:
http://developer.android.com/reference/android/media/JetPlayer.html

and the page on creating the JET files:
http://developer.android.com/guide/topics/media/jet/jetcreator_manual.html

If you implemented this it would likely work best but would definitely be the most work.

JetPlayer basically lets you have one audio file (i think it's a MIDI file) with multiple tracks that you mute and unmute as you please. I have no personal experience with it and have just read the docs some, but it seems like it would be very useful for any situation with more than one sound.

Edit: Also, it's worth mentioning this mailing list and in case the link ever changes this google search in case anyone is interested in android audio topics.