4

I am working on an app, wherein when a new activity is started, it should start playing a sound. So I used mediaplayer to play the sound in oncreate and It worked fine. But when I tried to use soundpool instead, by loading and playing it in oncreate of the activity. Its is not playing. I choose soundpool, since its better than mediaplayer.

what might be the issue? doesn't soundpool work in oncreate?

Adarsh H S
  • 1,208
  • 6
  • 21
  • 42
  • 1
    u can use soundpool in oncreate...and please remember mediaplayer is to play large files... soundpool is to play small sound like tick,etc... – Goofy Jun 29 '12 at 04:30
  • I am playing a small sound something like "welcome" which works fine when I tried playing it on click of a button. but when I do the same in oncreate, its not working. – Adarsh H S Jun 29 '12 at 04:50
  • can you post your implementation when creating soundpool in onCreate and how using it. – Niko Jun 29 '12 at 05:07

3 Answers3

2

Alternatively , you could override OnWindowFocusChanged to play the file when the Activity starts ...

Something like this ...

  public class YourActivity extends Activity {
  .
  .
  .
   private Handler mHandler;

   public void onCreate(Bundle savedInstanceState) {
   .
   .
   .
   this.mHandler = new Handler();

   .
   .
   .

   }



public void onWindowFocusChanged(boolean paramBoolean)
      {

 if (paramBoolean)
                 {
          this.mHandler.postDelayed(new Runnable()
          {
            public void run()
            {

                // Refer to the answer above to create the play function for your soundfile ... 


            YourActivity.this.play("The sound from the sound pool");
            }
          }
          , 1000L);
      }

}

diptia
  • 2,135
  • 21
  • 21
2

Maybe you just need to put a sleep in the onCreate method.

I had pretty much same problem trying to write an app that would sometimes need to play a sound as soon as it woke up. Eventually after much trial and error I discovered that it was putting the error "sample NOT READY" in the log output. The problem was that loading a sound file happens asynchronously, and if you try to play the sound before it has loaded it fails.

There is supposedly a mechanism you can use called setOnLoadCompleteListener, but I've yet to see an example of how this is actually useful. In the example shown above from mirroredAbstraction (assuming it works as advertised) all that would happen if the sound is not loaded yet is that it would not play the sound, which is pretty much the same as what you have now.

If that example somehow magically "fixed" your problem then I would suggest that it was only because all the extra overhead in the two method calls basically give your sound time to load before it was played. You could probably have achieved the same outcome with a simple SystemClock.sleep(100) in your onCreate between the load the play.

Depending on the size of your sound you might need to lengthen the delay, but a bit of experimentation with differing delays should tell you how much you need.

Kevin Golding
  • 83
  • 1
  • 6
  • Sorry 'm replying late. +1 for your response. I solved the problem by loading soundpools in async thread, and also to make use of setOnLoadCompleteListener() to check if all soundpools are loaded before playing it. Just sharing info incase you are still looking for solution. :) – Adarsh H S Jun 11 '13 at 07:31
1

You can play it anywhere,

Ill demonstrate with a simple example

Create a method initializeSoundPool

private void initializeSoundPool(){
        mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    loaded = true;
                }
            });
            soundID = mSoundPool.load(this, R.raw.glassbreak, 1);        
        }

Then create a method playFile

private void playFile(){
 AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
         float actualVolume = (float) audioManager
                 .getStreamVolume(AudioManager.STREAM_MUSIC);
         float maxVolume = (float) audioManager
                 .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
         float volume = actualVolume / maxVolume;
         if (loaded) {
             mSoundPool.play(soundID, volume, volume, 1, 0, 1f);
             Log.e("Test", "Played sound");
         }
}

Then in onCreate call them like this

    private  SoundPool mSoundPool; 
    private int soundID;
    boolean loaded = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spxml);
        initializeSoundPool();
            playFile();
    }

Or even better call initializeSoundPool in onCreate and then call playFile in onResume.

Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • I would like to call both the initializesoundpool() and playfile() methods outside the onCreate(). But when I try that the sound is playing. Can you please help in this... – Parthiban M Feb 10 '15 at 07:04