2013-01-13 81 views
1

这里是我的代码:的Soundpool加载永远不会结束

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循环,并且会一直持续循环。我相信我可能会误解onLoadCompleteListenerSoundPool并且执行不正确。我想确保我的应用程序不会离开onCreate方法,直到完全加载所有五个声音。

我发现,以确保只有一次它被加载我想播放声音的唯一方法是通过使用onStart方法显示while循环。在加载之前,play方法始终返回零。但是,我不喜欢这种方法,因为每次我想要播放声音时都必须使用它。也可能非常低效。

回答

0

我决定去MediaPlayer。以使用更多资源为代价,实现的相对简单性以及更广泛的控制播放方法都有助于转变。

这是我实现的一个简要介绍:

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.readysetgo); 
... 
mediaPlayer.start(); 
while (mediaPlayer.isPlaying()) {} //wait until finished to move on 
... 
相关问题