从 raw 播放音频时 avd 崩溃

avd crashes when audio played from raw

我最近一直在玩 21 点游戏,在我开始将 raw 文件夹中的音频集成到我的项目之前,我的程序 运行 运行得很顺利。现在,每次我 运行 应用程序时它都会崩溃。我已经尝试解决这个问题大约一个星期了,但没有成功。也许有人可以解决这个难题。这是我正在努力处理的代码块:

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();
        }
    }
});

这是我的 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.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.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

关于如何让它工作的任何想法?

在您的 onClick() 函数中尝试此代码:

MediaPlayer mp =MediaPlayer.create(YOUR_CLASS_NAME.this, R.raw.shuffle);
mp.start();