Solucionado (ver solução)
Solucionado
(ver solução)
9
respostas

Problema com som SoundPool Está desuso

Bom tarde

Parece que no Android Studio está em desuso vi alguns outros exemplos mas na implantação não funcionou muito bem... Poderia me ajudar neste pequeno problema, desde já agradeço....

03-07 16:36:05.040 17317-17317/br.com.alura.jumper I/art: Not late-enabling -Xcheck:jni (already on)
03-07 16:36:05.041 17317-17317/br.com.alura.jumper I/art: Late-enabling JIT
03-07 16:36:05.212 17317-17317/br.com.alura.jumper I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
03-07 16:36:06.292 17317-17317/br.com.alura.jumper W/System: ClassLoader referenced unknown path: /data/app/br.com.alura.jumper-1/lib/arm
03-07 16:36:06.721 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 20.772ms
03-07 16:36:07.366 17317-17338/br.com.alura.jumper E/WVMExtractor: Failed to open libwvm.so: dlopen failed: library "libwvm.so" not found
03-07 16:36:07.500 17317-17343/br.com.alura.jumper I/OMXClient: Using client-side OMX mux.
03-07 16:36:07.711 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 7.566ms
03-07 16:36:07.780 17317-17328/br.com.alura.jumper I/art: Background sticky concurrent mark sweep GC freed 5386(358KB) AllocSpace objects, 1(28KB) LOS objects, 0% free, 4MB/4MB, paused 1.809ms total 232.331ms
03-07 16:36:07.793 17317-17328/br.com.alura.jumper W/art: Suspending all threads took: 12.178ms
03-07 16:36:07.986 17317-17328/br.com.alura.jumper I/art: Background partial concurrent mark sweep GC freed 132(6KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 9MB/13MB, paused 961us total 186.265ms
03-07 16:36:08.630 17317-17328/br.com.alura.jumper W/art: Suspending all threads took: 9.694ms
03-07 16:36:10.230 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 23.521ms
03-07 16:36:11.219 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 10.669ms
03-07 16:36:12.219 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 9.846ms
03-07 16:36:13.719 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 6.552ms
03-07 16:36:14.249 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 35.710ms
03-07 16:36:14.740 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 25.053ms
03-07 16:36:15.732 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 7.163ms
03-07 16:36:17.230 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 9.829ms
03-07 16:36:17.731 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 8.777ms
03-07 16:36:18.230 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 7.605ms
03-07 16:36:18.740 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 16.015ms
03-07 16:36:19.220 17317-17423/br.com.alura.jumper D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-07 16:36:19.230 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 5.591ms
03-07 16:36:19.312 17317-17317/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xac83b540, tid 17317
03-07 16:36:19.533 17317-17423/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xac83b600, tid 17423
03-07 16:36:19.544 17317-17423/br.com.alura.jumper I/OpenGLRenderer: Initialized EGL, version 1.4
03-07 16:36:19.674 17317-17423/br.com.alura.jumper W/EGL_emulation: eglSurfaceAttrib not implemented
03-07 16:36:19.674 17317-17423/br.com.alura.jumper W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae962320, error=EGL_SUCCESS
03-07 16:36:19.741 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 15.504ms
03-07 16:36:19.885 17317-17354/br.com.alura.jumper D/: HostConnection::get() New Host Connection established 0xac83b200, tid 17354
03-07 16:36:19.901 17317-17317/br.com.alura.jumper I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
03-07 16:36:23.535 17317-17354/br.com.alura.jumper W/Surface: WARNING: Surface's mNativeObject (0xffffffffac7c2e00) != mLockedObject (0xffffffffac7c2700)
03-07 16:36:27.773 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 28.433ms
03-07 16:36:27.798 17317-17317/br.com.alura.jumper E/InputEventReceiver: Exception dispatching input event.
03-07 16:36:27.803 17317-17317/br.com.alura.jumper E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void br.com.alura.jumper.engine.Som.toca(int)' on a null object reference
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at br.com.alura.jumper.elements.Passaro.pula(Passaro.java:47)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at br.com.alura.jumper.engine.Game.onTouch(Game.java:91)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:9290)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:9514)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:323)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:135)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Native Method)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-07 16:36:27.852 17317-17317/br.com.alura.jumper E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 16:36:27.854 17317-17317/br.com.alura.jumper D/AndroidRuntime: Shutting down VM
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime: FATAL EXCEPTION: main
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime: Process: br.com.alura.jumper, PID: 17317
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void br.com.alura.jumper.engine.Som.toca(int)' on a null object reference
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at br.com.alura.jumper.elements.Passaro.pula(Passaro.java:47)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at br.com.alura.jumper.engine.Game.onTouch(Game.java:91)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:9290)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:9514)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:323)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-07 16:36:27.867 17317-17317/br.com.alura.jumper E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 16:36:32.621 17317-17324/br.com.alura.jumper W/art: Suspending all threads took: 71.987ms
03-07 16:36:32.626 17317-17317/br.com.alura.jumper I/Process: Sending signal. PID: 17317 SIG: 9
package br.com.alura.jumper.engine;


import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import br.com.alura.jumper.R;

public class Som {

    private SoundPool soundPool;
    public static int PULO;

    public Som(Context context){
        soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        PULO = soundPool.load(context, R.raw.pulo, 1);
    }

    public void toca(int som){
        soundPool.play(som, 1,1,1,0,1);
    }
}
package br.com.alura.jumper.elements;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;


import br.com.alura.jumper.R;
import br.com.alura.jumper.engine.Som;
import br.com.alura.jumper.graphic.Cores;
import br.com.alura.jumper.graphic.Tela;

public class Passaro {

    public static final float X = 100;
    public static final int RAIO = 50;
    private static final Paint VERMELHO = Cores.getCorDoPassaro();
    private float altura;
    private Tela tela;
    private Bitmap passaro;
    private Som som;

    public Passaro(Tela tela, Context context, Som som){
        this.tela = tela;
        this.altura = 100;
        Bitmap bp = BitmapFactory.decodeResource(context.getResources(), R.drawable.passaro);
        this.passaro =  Bitmap.createScaledBitmap(bp, RAIO*2, RAIO*2,false);
    }

    public void desenhaNo(Canvas canvas){
        //canvas.drawCircle(X,altura, RAIO, VERMELHO);
        canvas.drawBitmap(passaro, X - RAIO, altura - RAIO, null);

    }

    public void cai() {
        boolean chegouNoChao = altura + RAIO > tela.getAltura();
        if(!chegouNoChao){
            this.altura += 5;
        }
    }

    public void pula() {
        if(altura - RAIO > 0) {
            som.toca(Som.PULO);
            this.altura -= 150;
        }
    }

    public float getAltura() {
        return this.altura;
    }
}
package br.com.alura.jumper.engine;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import br.com.alura.jumper.R;
import br.com.alura.jumper.elements.Cano;
import br.com.alura.jumper.elements.Canos;
import br.com.alura.jumper.elements.GameOver;
import br.com.alura.jumper.elements.Passaro;
import br.com.alura.jumper.elements.Pontuacao;
import br.com.alura.jumper.graphic.Tela;

public class Game extends SurfaceView implements Runnable, View.OnTouchListener {

    private final Context context;
    private boolean isRunning = true;
    private SurfaceHolder holder =  getHolder();
    private Passaro passaro;
    private Canos canos;
    private Canvas canvas;
    private Bitmap background;
    private Tela tela;
    private Pontuacao pontuacao;
    private Som som;


    public Game(Context context) {
        super(context);
        this.context = context;

        tela = new Tela(context);
        som = new Som(context);

        inicializaElementos();
        setOnTouchListener(this);

    }

    private void inicializaElementos(){
        passaro = new Passaro(tela,context,som);
        pontuacao = new Pontuacao();
        canos = new Canos(tela,pontuacao, context);
        Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.background);
        background = Bitmap.createScaledBitmap(back, back.getWidth(), tela.getAltura(),false);
    }

    @Override
    public void run() {
        while (isRunning){
            if(!holder.getSurface().isValid()) continue;
            canvas = holder.lockCanvas();

            canvas.drawBitmap(background, 0, 0, null);

            passaro.desenhaNo(canvas);
            passaro.cai();

            canos.desenhaNo(canvas);
            canos.move();

            pontuacao.desenhaNo(canvas);

            if(new VerificadorDeColisao(passaro, canos).temColisao()){
                new GameOver(tela).desenhaNo(canvas);
                isRunning = false;
            }
            holder.unlockCanvasAndPost(canvas);
        }
    }



    public void inicia() {
        isRunning = true;
    }

    public void pausa() {
        isRunning = false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        passaro.pula();
        return false;
    }
}
9 respostas
solução!

Sobre a classe SoundPool estar em desuso, na realidade é só a forma de instanciar essa classe que mudou.

Na sua classe Som, onde você faz:

    public Som(Context context){
        soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        PULO = soundPool.load(context, R.raw.pulo, 1);
    }

É só fazer:

    public Som(Context context){
        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setMaxStreams(3);
        soundPool = builder.build();
        PULO = soundPool.load(context, R.raw.pulo, 1);
    }

Sobre o erro no seu código: olhando a stacktrace fica fácil ver o que é

java.lang.NullPointerException: Attempt to invoke virtual method 'void br.com.alura.jumper.engine.Som.toca(int)' on a null object reference

at br.com.alura.jumper.elements.Passaro.pula(Passaro.java:47)

Olhando seu código, faltou você fazer o seguinte na sua classe Passaro:

public Passaro(Tela tela, Context context, Som som){
    this.som = som;
    //...
}

Deu certinho mas deu um problema referente a api 21, olhei na internet e em alguns casos eles usam android.os.Build.VERSION.SDK_INT) == 21 dentro de um if bom não sei se vai fazer alguma diferença...

Isso acontece pois esse Builder do SoundPool só está disponível a partir do Android 5 (API 21). Na realidade, esse if deveria ser assim:

if(android.os.Build.VERSION.SDK_INT >= 21) {
    //usa o builder
} else {
   //usa o construtor
}

Como ficaria no exemplo que vc me passou...

if(android.os.Build.VERSION.SDK_INT >= 21) {
        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setMaxStreams(3);
        soundPool = builder.build();
} else {
        soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);

}

Só para confirmar, a instrução abaixo entra dentro do if e else... dependendo a situação...

PULO = soundPool.load(context, R.raw.pulo, 0);

Essa linha fica fora do if-else.

if(android.os.Build.VERSION.SDK_INT >= 21) {
        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setMaxStreams(3);
        soundPool = builder.build();
} else {
        soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}

PULO = soundPool.load(context, R.raw.pulo, 0);

Obrigado deu certo...

Mas queria fazer algumas perguntas, fecho esse tópico e abro outro... ou posso abrir outro...

Fecha esse e abre outro. ;)