Issue
I have been working on a blackjack game lately, and I had the program running smoothly until I started integrating audio from my raw folder into my project. now, It crashes every time I run the app. I have been trying to solve this for about a week now, but without success. Perhaps somebody may be able to solve this puzzle. Here's the block of code that I am struggling with:
final Button redeal = (Button) findViewById(R.id.redeal);
redeal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer m = new MediaPlayer();
m = MediaPlayer.create(getBaseContext(), R.raw.shuffle);
String path = "android.resource://" + "/" + R.raw.shuffle;
try {
//m.setDataSource(context, Uri.parse(path));
m.prepare();
m.start();
redeal.playSoundEffect(R.raw.shuffle);
reDeal();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Here is my logcat:
01-16 10:43:47.974: E/MediaPlayer(2245): Should have subtitle controller already set
01-16 10:43:47.975: E/MediaPlayer(2245): prepareAsync called in state 8
01-16 10:43:47.975: D/AndroidRuntime(2245): Shutting down VM
01-16 10:43:47.975: E/AndroidRuntime(2245): FATAL EXCEPTION: main
01-16 10:43:47.975: E/AndroidRuntime(2245): Process: com.pantoine.blackjack, PID: 2245
01-16 10:43:47.975: E/AndroidRuntime(2245): java.lang.IllegalStateException
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.media.MediaPlayer._prepare(Native Method)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.media.MediaPlayer.prepare(MediaPlayer.java:1125)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.pantoine.blackjack.Blackjack$3.onClick(Blackjack.java:85)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.view.View.performClick(View.java:4756)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.view.View$PerformClick.run(View.java:19749)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Handler.handleCallback(Handler.java:739)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Handler.dispatchMessage(Handler.java:95)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Looper.loop(Looper.java:135)
01-16 10:43:4701-16 10:43:47.974: E/MediaPlayer(2245): Should have subtitle controller already set
01-16 10:43:47.975: E/MediaPlayer(2245): prepareAsync called in state 8
01-16 10:43:47.975: D/AndroidRuntime(2245): Shutting down VM
01-16 10:43:47.975: E/AndroidRuntime(2245): FATAL EXCEPTION: main
01-16 10:43:47.975: E/AndroidRuntime(2245): Process: com.pantoine.blackjack, PID: 2245
01-16 10:43:47.975: E/AndroidRuntime(2245): java.lang.IllegalStateException
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.media.MediaPlayer._prepare(Native Method)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.media.MediaPlayer.prepare(MediaPlayer.java:1125)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.pantoine.blackjack.Blackjack$3.onClick(Blackjack.java:85)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.view.View.performClick(View.java:4756)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.view.View$PerformClick.run(View.java:19749)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Handler.handleCallback(Handler.java:739)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Handler.dispatchMessage(Handler.java:95)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.os.Looper.loop(Looper.java:135)
01-16 10:43:47.975: E/AndroidRuntime(2245): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-16 10:43:47.975: E/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Native Method)
01-16 10:43:47.975: E/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Method.java:372)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-16 10:43:50.110: V/BlackjackSelect(2268): My Context is com.pantoine.blackjack
01-16 10:43:50.113: V/BlackjackSelect(2268): Spinner: player[1] = Evan
01-16 10:43:50.115: V/BlackjackSelect(2268): OnResume
01-16 10:43:50.299: D/(2268): HostConnection::get() New Host Connection established 0xa692bca0, tid 2268.975: E/AndroidRuntime(2245): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-16 10:43:47.975: E/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Native Method)
01-16 10:43:47.975: E/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Method.java:372)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-16 10:43:47.975: E/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-16 10:43:50.110: V/BlackjackSelect(2268): My Context is com.pantoine.blackjack
01-16 10:43:50.113: V/BlackjackSelect(2268): Spinner: player[1] = Evan
01-16 10:43:50.115: V/BlackjackSelect(2268): OnResume
01-16 10:43:50.299: D/(2268): HostConnection::get() New Host Connection established 0xa692bca0, tid 2268
Any ideas on how to get this to work?
Solution
Try this code in your onClick() function:
MediaPlayer mp =MediaPlayer.create(YOUR_CLASS_NAME.this, R.raw.shuffle);
mp.start();
Answered By - GiuseppeLabanca
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.