Hi all, How to play an audio during splash screen. Guidance needed.
+1
A:
You can play audio files using the MediaPlayer class.
Example
MediaPlayer player = new MediaPlayer();
player.setDataSource("/sdcard/audiotrack.mp3");
player.prepare();
player.start();
Chaoz
2010-07-30 09:30:53
can u be more specified plz.do u hv some code snippet ?regards
shishir.bobby
2010-07-30 10:01:24
Updated my post with some code.
Chaoz
2010-07-30 12:17:11
thanks man... appreicated
shishir.bobby
2010-07-31 08:55:31
+1
A:
My way to do this (no external sound needed, since I put my soundfile in my resources-folder):
In onCreate:
mp = MediaPlayer.create(getBaseContext(), R.raw.sound); /*Gets your
soundfile from res/raw/sound.ogg */
mp.start(); //Starts your sound
//Continue with your run/thread-code here
Remember to have the sound in .ogg-format; it's fully supported in Android.
An important thing below about handling the sound when the Splash Screen activity is stopped:
There are two general ways to manage the Splash Screen (and the sound inside it) when it's stopped:
Destroy the whole activity:
protected void onStop() { super.onStop(); ur.removeCallbacks(myRunnable); /*If the application is stopped; remove the callback, so the next time the application starts it shows the Splash Screen again, and also, so the thread-code, don't continue after the application has stopped */ finish(); onDestroy(); }
Or you can just stop the sound in onStop:
protected void onStop() { super.onStop(); if(mp.isPlaying()){ //Must check if it's playing, otherwise it may be a NPE mp.pause(); //Pauses the sound ur.removeCallbacks(myRunnable); } }
If you choose the second alternative you also have to start your Callback and MediaPlayer in the onStart-method.
I prefer the first alternative.
Julian Assange
2010-07-30 15:49:33
Should be noted that before Android 2.2 users only have limited memory to save applications in. It's better to store them on the external memory because of this. If you have a few small sound files it wouldn't matter - but if you have many it will.
Chaoz
2010-07-31 10:09:45