Reputation: 1208
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?
Upvotes: 4
Views: 1719
Reputation: 2245
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);
}
}
Upvotes: 2
Reputation: 83
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.
Upvotes: 2
Reputation: 8604
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
.
Upvotes: 1