Reputation: 8548
edit3: Ahaaa! MP crashed when SP "gives sample 14 not READY"...
edit 2: I also have a SoundPool in my activity what plays short sound effects and, obviously, it somehow kills the MediaPlayer. Like the MediaPlayer loads, plays about a second and then dies when SP is loaded. If I load MediaPlayer at a later time, when SP is already loaded, MP will start playing fine, but will die if I play a few sound FX in SP...
old: I'm trying to play a music sound file, about 2 minutes long...
try {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.naturesounds );
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
yet it throws
06-29 16:50:52.359: E/MediaPlayer(3772): prepareAsync called in state 8
06-29 16:50:52.359: W/System.err(3772): java.lang.IllegalStateException
06-29 16:50:52.359: W/System.err(3772): at android.media.MediaPlayer.prepare(Native Method)
06-29 16:50:52.359: W/System.err(3772): at engineDemo.com.main.fillWorld(main.java:132)
06-29 16:50:52.359: W/System.err(3772): at engineDemo.com.main.onCreate(main.java:83)
06-29 16:50:52.359: W/System.err(3772): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 16:50:52.359: W/System.err(3772): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-29 16:50:52.359: W/System.err(3772): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-29 16:50:52.359: W/System.err(3772): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-29 16:50:52.359: W/System.err(3772): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-29 16:50:52.359: W/System.err(3772): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 16:50:52.359: W/System.err(3772): at android.os.Looper.loop(Looper.java:130)
06-29 16:50:52.359: W/System.err(3772): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-29 16:50:52.359: W/System.err(3772): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 16:50:52.359: W/System.err(3772): at java.lang.reflect.Method.invoke(Method.java:507)
06-29 16:50:52.359: W/System.err(3772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-29 16:50:52.363: W/System.err(3772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-29 16:50:52.363: W/System.err(3772): at dalvik.system.NativeStart.main(Native Method)
Any ideas what might be wrong or what's the best way to play background music?
Thanks!
edit:
edited like this
final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.naturesounds);
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
public void onPrepared(MediaPlayer arg0) {
Log.e("ready!","ready!");
mediaPlayer.start();
}} );
it does show the Log, but still nothing plays...
Upvotes: 2
Views: 15185
Reputation: 46856
Example from the develop docs on MediaPlayer:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
mp.start();
}
});
//Below this line is the example from docs.
/*----------------------------------------------------*/
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
//mediaPlayer.start(); // no need to call prepare(); create() does that for you
Your MediaPlayer is already Prepared() you don't need to call that after create.
Upvotes: 10