为我的Android游戏创建了一个简单的静音按钮,但它崩溃了应用程序?

时间:2013-06-08 23:59:43

标签: android crash mute

我正在为Android创建一款游戏,所以我决定创建一个静音按钮来暂停音乐。我添加了以下代码

for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {

            if (inBounds(event, 4, 3, 80, 35)) {
                 if (AndroidMusic.mediaPlayer.isPlaying() == true) {
                     AndroidMusic.mediaPlayer.setVolume(0.0f,0.0f);
                 }

            }

        }
    }

所以当你触摸4,3,80,35(静音按钮)的任何地方时,它会检查音乐是否正在播放,如果是,它会将音量设置为0.0f。 我也尝试过:

AndroidMusic.mediaPlayer.pause();

但它也没有用。

当我删除此代码的这一部分时,它完全可以正常工作,但是当我再次放置它们时,菜单会加载,但是当你触摸任何地方时(即使是播放按钮或空白空间),它会停止然后崩溃。

编辑:这是LogCat,

06-09 09:59:38.565: I/Process(24310): Sending signal. PID: 24310 SIG: 9
06-09 09:59:43.075: D/dalvikvm(26060): GC_FOR_ALLOC freed 69K, 10% free 12147K/13443K, paused 41ms, total 41ms
06-09 09:59:43.080: I/dalvikvm-heap(26060): Grow heap (frag case) to 13.647MB for 768016-byte allocation
06-09 09:59:43.095: D/dalvikvm(26060): GC_FOR_ALLOC freed <1K, 10% free 12896K/14215K, paused 16ms, total 16ms
06-09 09:59:43.140: D/dalvikvm(26060): GC_CONCURRENT freed 5K, 10% free 12903K/14215K, paused 15ms+12ms, total 45ms
06-09 09:59:43.145: V/SoundPoolThread(26060): beginThread
06-09 09:59:43.145: V/SoundPoolThread(26060): run
06-09 09:59:43.145: V/MediaPlayer-JNI(26060): native_setup
06-09 09:59:43.145: V/MediaPlayer(26060): constructor
06-09 09:59:43.150: V/MediaPlayer(26060): setListener
06-09 09:59:43.150: V/MediaPlayer-JNI(26060): setDataSourceFD: fd 43
06-09 09:59:43.150: V/MediaPlayer(26060): setDataSource(43, 241934, 1635707)
06-09 09:59:43.160: V/MediaPlayer(26060): setVideoSurfaceTexture
06-09 09:59:43.160: V/MediaPlayer(26060): prepare
06-09 09:59:43.165: V/MediaPlayer(26060): message received msg=5, ext1=0, ext2=0
06-09 09:59:43.165: V/MediaPlayer(26060): New video size 0 x 0
06-09 09:59:43.165: V/MediaPlayer(26060): callback application
06-09 09:59:43.165: V/MediaPlayer(26060): back from callback
06-09 09:59:43.165: V/MediaPlayer(26060): message received msg=1, ext1=0, ext2=0
06-09 09:59:43.165: V/MediaPlayer(26060): prepared
06-09 09:59:43.165: V/MediaPlayer(26060): signal application thread
06-09 09:59:43.165: V/MediaPlayer(26060): callback application
06-09 09:59:43.165: V/MediaPlayer(26060): prepare complete - status=0
06-09 09:59:43.165: V/MediaPlayer(26060): back from callback
06-09 09:59:43.165: V/MediaPlayer-JNI(26060): setLooping: 1
06-09 09:59:43.165: V/MediaPlayer(26060): MediaPlayer::setLooping
06-09 09:59:43.165: V/MediaPlayer-JNI(26060): setVolume: left 0.850000  right 0.850000
06-09 09:59:43.165: V/MediaPlayer(26060): MediaPlayer::setVolume(0.850000, 0.850000)
06-09 09:59:43.165: V/MediaPlayer(26060): isPlaying: 0
06-09 09:59:43.165: V/MediaPlayer-JNI(26060): isPlaying: 0
06-09 09:59:43.165: V/MediaPlayer-JNI(26060): start
06-09 09:59:43.165: V/MediaPlayer(26060): start
06-09 09:59:43.180: V/MediaPlayer(26060): isPlaying: 1
06-09 09:59:43.180: V/MediaPlayer-JNI(26060): isPlaying: 1
06-09 09:59:43.185: I/MediaPlayer(26060): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
06-09 09:59:43.230: D/libEGL(26060): loaded /system/lib/egl/libEGL_mali.so
06-09 09:59:43.235: D/libEGL(26060): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-09 09:59:43.235: D/libEGL(26060): loaded /system/lib/egl/libGLESv2_mali.so
06-09 09:59:43.240: D/(26060): Device driver API match
06-09 09:59:43.240: D/(26060): Device driver API version: 10
06-09 09:59:43.240: D/(26060): User space API version: 10 
06-09 09:59:43.240: D/(26060): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
06-09 09:59:43.270: D/OpenGLRenderer(26060): Enabling debug mode 0
06-09 09:59:43.295: D/dalvikvm(26060): GC_FOR_ALLOC freed 77K, 9% free 12942K/14215K, paused 12ms, total 12ms
06-09 09:59:43.295: I/dalvikvm-heap(26060): Grow heap (frag case) to 14.423MB for 768016-byte allocation
06-09 09:59:43.315: D/dalvikvm(26060): GC_CONCURRENT freed <1K, 9% free 13691K/14983K, paused 12ms+2ms, total 23ms
06-09 09:59:43.315: D/dalvikvm(26060): WAIT_FOR_CONCURRENT_GC blocked 11ms
06-09 09:59:43.350: D/dalvikvm(26060): GC_FOR_ALLOC freed 1K, 9% free 13695K/14983K, paused 12ms, total 12ms
06-09 09:59:43.350: I/dalvikvm-heap(26060): Grow heap (frag case) to 15.892MB for 1536016-byte allocation
06-09 09:59:43.375: D/dalvikvm(26060): GC_CONCURRENT freed <1K, 9% free 15195K/16519K, paused 11ms+2ms, total 25ms
06-09 09:59:43.375: D/dalvikvm(26060): WAIT_FOR_CONCURRENT_GC blocked 13ms
06-09 09:59:43.430: D/dalvikvm(26060): GC_FOR_ALLOC freed <1K, 9% free 15196K/16519K, paused 17ms, total 18ms
06-09 09:59:43.435: I/dalvikvm-heap(26060): Grow heap (frag case) to 17.358MB for 1536016-byte allocation
06-09 09:59:43.465: D/dalvikvm(26060): GC_CONCURRENT freed <1K, 8% free 16696K/18055K, paused 12ms+2ms, total 27ms
06-09 09:59:43.465: D/dalvikvm(26060): WAIT_FOR_CONCURRENT_GC blocked 12ms
06-09 09:59:43.510: D/dalvikvm(26060): GC_FOR_ALLOC freed <1K, 8% free 16696K/18055K, paused 12ms, total 12ms
06-09 09:59:43.520: I/dalvikvm-heap(26060): Grow heap (frag case) to 21.313MB for 4147216-byte allocation
06-09 09:59:43.540: D/dalvikvm(26060): GC_CONCURRENT freed <1K, 7% free 20746K/22151K, paused 12ms+2ms, total 23ms
06-09 09:59:43.730: D/dalvikvm(26060): GC_FOR_ALLOC freed 1503K, 12% free 21044K/23687K, paused 15ms, total 15ms
06-09 09:59:43.810: D/dalvikvm(26060): GC_FOR_ALLOC freed 1501K, 12% free 21042K/23687K, paused 15ms, total 15ms
06-09 09:59:43.815: I/dalvikvm-heap(26060): Grow heap (frag case) to 25.557MB for 4147216-byte allocation
06-09 09:59:43.845: D/dalvikvm(26060): GC_CONCURRENT freed <1K, 10% free 25092K/27783K, paused 12ms+1ms, total 26ms
06-09 09:59:46.130: V/MediaPlayer(26060): isPlaying: 1
06-09 09:59:46.130: V/MediaPlayer-JNI(26060): isPlaying: 1
06-09 09:59:46.130: V/MediaPlayer-JNI(26060): pause
06-09 09:59:46.130: V/MediaPlayer(26060): pause
06-09 09:59:47.450: W/dalvikvm(26060): threadid=13: thread exiting with uncaught exception (group=0x410ba2a0)
06-09 09:59:47.455: E/AndroidRuntime(26060): FATAL EXCEPTION: Thread-4402
06-09 09:59:47.455: E/AndroidRuntime(26060): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
06-09 09:59:47.455: E/AndroidRuntime(26060):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
06-09 09:59:47.455: E/AndroidRuntime(26060):    at java.util.ArrayList.get(ArrayList.java:304)
06-09 09:59:47.455: E/AndroidRuntime(26060):    at com.kilobolt.robotgame.MainMenuScreen.update(MainMenuScreen.java:35)
06-09 09:59:47.455: E/AndroidRuntime(26060):    at com.kilobolt.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:47)
06-09 09:59:47.455: E/AndroidRuntime(26060):    at java.lang.Thread.run(Thread.java:856)

我做了初始化mediaPlayer,这是AndroidMusic类:

package com.kilobolt.framework.implementation;

import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;

import com.kilobolt.framework.Music;

public class AndroidMusic implements Music, OnCompletionListener, OnSeekCompleteListener, OnPreparedListener, OnVideoSizeChangedListener {
    public static MediaPlayer mediaPlayer;
    boolean isPrepared = false;

    public AndroidMusic(AssetFileDescriptor assetDescriptor) {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
                    assetDescriptor.getStartOffset(),
                    assetDescriptor.getLength());
            mediaPlayer.prepare();
            isPrepared = true;
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnSeekCompleteListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnVideoSizeChangedListener(this);

        } catch (Exception e) {
            throw new RuntimeException("Couldn't load music");
        }
    }

    @Override
    public void dispose() {

         if (this.mediaPlayer.isPlaying()){
               this.mediaPlayer.stop();
                }
        this.mediaPlayer.release();
    }

    @Override
    public boolean isLooping() {
        return mediaPlayer.isLooping();
    }

    @Override
    public boolean isPlaying() {
        return this.mediaPlayer.isPlaying();
    }

    @Override
    public boolean isStopped() {
        return !isPrepared;
    }

    @Override
    public void pause() {
        if (this.mediaPlayer.isPlaying())
            mediaPlayer.pause();
    }



    @Override
    public void play() {
        if (this.mediaPlayer.isPlaying())
            return;

        try {
            synchronized (this) {
                if (!isPrepared)
                    mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void muteSound() {

    }

    @Override
    public void setLooping(boolean isLooping) {
        mediaPlayer.setLooping(isLooping);
    }

    @Override
    public void setVolume(float volume) {
        mediaPlayer.setVolume(volume, volume);
    }

    @Override
    public void stop() {
         if (this.mediaPlayer.isPlaying() == true){
        this.mediaPlayer.stop();

       synchronized (this) {
           isPrepared = false;
        }}
    }

    @Override
    public void onCompletion(MediaPlayer player) {
        synchronized (this) {
            isPrepared = false;
        }
    }

    @Override
    public void seekBegin() {
        mediaPlayer.seekTo(0);

    }


    @Override
    public void onPrepared(MediaPlayer player) {
        // TODO Auto-generated method stub
         synchronized (this) {
               isPrepared = true;
            }

    }

    @Override
    public void onSeekComplete(MediaPlayer player) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onVideoSizeChanged(MediaPlayer player, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void CurrVolume(float volume) {
        // TODO Auto-generated method stub

    }


}

HalR,我尝试了你的代码并使“静音”按钮暂停音乐并且不会使应用程序崩溃,但是当我按下“播放”按钮时它仍然会崩溃。

1 个答案:

答案 0 :(得分:1)

很难知道,部分代码。但考虑到你所展示的内容,以及它看起来如何“锅炉板”,我怀疑你还没有初始化你的mediaPlayer。

您应该检查代码以查看mediaPlayer是否存在。像这样:

if (AndroidMusic.mediaPlayer != null) {
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {

            if (inBounds(event, 4, 3, 80, 35)) {
                 if (AndroidMusic.mediaPlayer.isPlaying() == true) {
                     AndroidMusic.mediaPlayer.setVolume(0.0f,0.0f);
                 }

            }

        }
    }
}