Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (retired 2014) Pretty Little Things Adding Sounds

Adding Sounds

The sound for the Android App keeps crashing, I have tried to rename the sound file and still keeps crashing. So frustrating. private void playSound() { MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball); player.start(); player.setOnCompletionListener(new OnCompletionListener() {

@Override public void onCompletion(MediaPlayer mp) { mp.release();}

5 Answers

Your code seems fine

private void playSound(){
        player = MediaPlayer.create(this, R.raw.crystal_ball);
        player.start();
        player.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

Maybe you could try one the following

  • Try increasing the ram in the emulator, by going to the AVD manager and editing the current AVD
  • Try looking into the log cat console to see if there are any registered errors that might help you understand better
  • Update your graphics driver, in some cases this causes the crash
  • Try creating a new AVD and deploying that
  • Update the android tools and API from the Android SDK manager

I hope that helps, though I'll update you incase I find a more definite answer.

I am looking in the logs and see this before it crashes. 02-14 15:21:24.747: D/dalvikvm(1081): Not late-enabling CheckJNI (already on) 02-14 15:21:26.207: D/dalvikvm(1081): GC_FOR_ALLOC freed 35K, 4% free 2807K/2920K, paused 70ms, total 71ms 02-14 15:21:26.447: D/dalvikvm(1081): GC_FOR_ALLOC freed 1K, 4% free 3279K/3392K, paused 57ms, total 58ms 02-14 15:21:26.647: D/gralloc_goldfish(1081): Emulator without GPU emulation detected. 02-14 15:21:30.567: D/dalvikvm(1081): GC_FOR_ALLOC freed 15K, 3% free 3339K/3432K, paused 18ms, total 19ms 02-14 15:21:30.647: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 3% free 3786K/3880K, paused 16ms, total 17ms 02-14 15:21:30.727: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 3% free 4233K/4328K, paused 17ms, total 17ms 02-14 15:21:30.807: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 3% free 4680K/4776K, paused 18ms, total 18ms 02-14 15:21:30.887: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 2% free 5127K/5224K, paused 18ms, total 19ms 02-14 15:21:30.987: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 2% free 5574K/5672K, paused 26ms, total 26ms 02-14 15:21:31.137: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 2% free 6468K/6568K, paused 19ms, total 19ms 02-14 15:21:31.287: D/dalvikvm(1081): GC_FOR_ALLOC freed <1K, 2% free 7362K/7464K, paused 20ms, total 21ms 02-14 15:21:31.497: D/dalvikvm(1081): GC_FOR_ALLOC freed 1K, 2% free 8704K/8808K, paused 20ms, total 21ms 02-14 15:21:31.777: D/dalvikvm(1081): GC_FOR_ALLOC freed 1K, 2% free 10492K/10600K, paused 21ms, total 21ms 02-14 15:21:32.047: D/dalvikvm(1081): GC_FOR_ALLOC freed 1K, 1% free 12281K/12392K, paused 23ms, total 23ms 02-14 15:21:32.197: D/AndroidRuntime(1081): Shutting down VM 02-14 15:21:32.197: W/dalvikvm(1081): threadid=1: thread exiting with uncaught exception (group=0xb2b0aba8) 02-14 15:21:32.207: E/AndroidRuntime(1081): FATAL EXCEPTION: main 02-14 15:21:32.207: E/AndroidRuntime(1081): Process: com.example.crystalball, PID: 1081 02-14 15:21:32.207: E/AndroidRuntime(1081): android.content.res.Resources$NotFoundException: File res/raw/crystal_ball.wpl from drawable resource ID #0x7f040000 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.content.res.Resources.openRawResourceFd(Resources.java:1087) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.media.MediaPlayer.create(MediaPlayer.java:844) 02-14 15:21:32.207: E/AndroidRuntime(1081): at com.example.crystalball.MainActivity.playSound(MainActivity.java:66) 02-14 15:21:32.207: E/AndroidRuntime(1081): at com.example.crystalball.MainActivity.access$4(MainActivity.java:65) 02-14 15:21:32.207: E/AndroidRuntime(1081): at com.example.crystalball.MainActivity$1.onClick(MainActivity.java:41) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.view.View.performClick(View.java:4438) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.view.View$PerformClick.run(View.java:18422) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.os.Handler.handleCallback(Handler.java:733) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:95) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:136) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-14 15:21:32.207: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method) 02-14 15:21:32.207: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:515) 02-14 15:21:32.207: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-14 15:21:32.207: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-14 15:21:32.207: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method) 02-14 15:21:32.207: E/AndroidRuntime(1081): Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.content.res.AssetManager.openNonAssetFdNative(Native Method) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:429) 02-14 15:21:32.207: E/AndroidRuntime(1081): at android.content.res.Resources.openRawResourceFd(Resources.java:1084) 02-14 15:21:32.207: E/AndroidRuntime(1081): ... 15 more

Could this be the issue? 02-14 15:44:00.150: E/AndroidRuntime(1034): Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

I figured it out! Silly me, I needed to download the files on the right hand side of the video. The very top one. Saved to desktop, dragged to raw folder and voila! Sound and no crash!

Hello guys .

My code seems ok :

private void playSound { MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball); player.start(); player.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();

        }
    });
    }


        //Update the label with our dynamic answer
        mAnswerLabel.setText(answer);

        animateCrystalBall();
        animateAnswer();
        playSound();
    }
});

But i have this errors for this lines:

This line : private void playSound:

   - Multiple markers at this line;
- void is an invalid type for the variable playSound;
- Syntax error, insert ";" to complete  FieldDeclaration;

And this line: playSound();

  • The method playSound() is undefined for the type new View.OnClickListener(){} ;

Also it says to do this :

  • Create method playSound()
  • Create method playSound() in type 'MainActivity'.

I don't understand this. I allready created 'playSound' as method, why is not working?

I've found it it was a silly mystiping , instean 'private void playSound()', i was typing 'private void playSound'. Sorry for the disturbance .