私は 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、コードを試してみたところ、「ミュート」ボタンで音楽が一時停止し、アプリがクラッシュしなくなりましたが、「再生」ボタンを押すとクラッシュします。