1

これが私のコードです:

    import java.util.Locale;

    import android.app.Activity;
    import android.content.Context;
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.media.SoundPool.OnLoadCompleteListener;
    import android.os.Bundle;
    import android.os.Vibrator;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.speech.tts.TextToSpeech;
    import android.speech.tts.TextToSpeech.OnInitListener;

    public class GameActivity extends Activity implements OnGestureListener, OnInitListener {

        private SoundPool soundPool;
        private int wallSound, moveSound, completeSound, restartSound, readysetgoSound;
        boolean loaded = false;
        float actualVolume, maxVolume, volume;

        GestureDetector gDetector;
        Vibrator vibrator;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            gDetector = new GestureDetector(this);
            vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

            // Set the hardware buttons to control the music
            this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
            // Load the sound
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    loaded = true;
                }
            });

            wallSound = soundPool.load(this, R.raw.wall, 1);
            moveSound = soundPool.load(this, R.raw.move, 1);
            completeSound = soundPool.load(this, R.raw.complete, 1);
            restartSound = soundPool.load(this, R.raw.restart, 1);
            readysetgoSound = soundPool.load(this, R.raw.readysetgo, 1);

            // Getting the user sound settings
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            volume = actualVolume / maxVolume;

            while (!loaded) {
                Log.e("Load Status", "Loading...");
            }
        }

        public void onStart()   {
            super.onStart();
            while(soundPool.play(readysetgoSound, volume, volume, 1, 0, 1f) == 0)   {}
        }
...

私が経験している問題は、サウンドファイルがいつロードされるかをチェックすることです。このonCreateメソッドでは、最後のwhileループに注目してください。理論的には、私が理解しているように、サウンドがロードされると、コードは次に進むはずです。ただし、アプリはwhileループを離れることはなく、永久に繰り返し処理を続けます。私はそれを誤解し、onLoadCompleteListener間違っSoundPoolて実装しているのではないかと思います。onCreate5つのサウンドがすべて完全に読み込まれるまで、アプリがメソッドを離れないようにしたいです。

必要なサウンドがロードされた後にのみ再生されることを確認する唯一の方法は、onStartメソッドに示されているwhileループを使用することです。ロードされるまで、playメソッドは常にゼロを返します。ただし、サウンドを再生するたびにこれを使用する必要があるため、このアプローチは好きではありません。おそらく非常に非効率的でもあります。

4

1 に答える 1

0

MediaPlayerを使うことにしました。より多くのリソースを使用するという犠牲を払って、実装の比較的容易さと再生を制御するためのメソッドのより広い広がりは、シフトを保証しました。

これが私の実装の簡単な概要です:

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.readysetgo);
...
mediaPlayer.start();
while (mediaPlayer.isPlaying()) {} //wait until finished to move on
...
于 2013-01-13T14:12:37.647 に答える