2014-01-11 2 views
0

내 Android 앱에 SoundPool이 하나 있지만 2 개 이상의 SoundPool 인스턴스에 대한 코드를 작성하는 방법을 모르겠습니다. 이 코드를 여러 SoundPools에 적용 할 수 있습니까?여러 개의 사운드 풀을 만드는 방법은 무엇입니까?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    View view = findViewById(R.id.button1); 
    view.setOnTouchListener(this); 

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

    { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, 
       int status) { 
      loaded = true; 

     } 
    }); 

    soundID = soundPool.load(this, R.raw.sound1, 1); 

} 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     // Getting the user sound settings 
     AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
     float actualVolume = (float) audioManager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = (float) audioManager 
       .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     float volume = actualVolume/maxVolume; 
     // Is the sound loaded already? 
     if (loaded) { 
      soundPool.play(soundID, volume, volume, 1, 0, 1f); 
      Log.e("Test", "Played sound"); 
     } 
    } 
    return false; 
} 
+0

@Joeel 저를 도와 줄 수 있습니까? –

답변

1

시도 :

// 8 soundpools 
SoundPool[] soundPool = new SoundPool[8]; 
int[] soundID = new int[8]; 

// You can have more that one set per sound if need, just make more raw sound 
// arrays with different sounds. 

// 8 sounds per soundpool 
int[] rawSounds = new int[]{ R.id.sound1, R.id.sound2, R.id.sound3, 
          R.id.sound4, R.id.sound5, R.id.sound6, 
          R.id.sound7, R.id.sound8 }; 

boolean[] loaded = new boolean[8]; 

// Go through each soundpool 
for(int i = 0; i < 8; i++) 
{ 
    soundPool[i] = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 

    // 8 sounds in one soundpool 
    for(int h = 0; h < 8; h++) 
    { 
     soundID[i] = soundPool.load(this, rawSounds[i], 1); 
    } 

    soundPool[i].setOnLoadCompleteListener(new OnLoadCompleteListener() 
    { 
     Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, 
       int status) 
     { 
      // One boolean per soundpool 
      loaded[i] = true; 
     } 
    }); 
} 

그런 다음 어떤 사운드를 눌러되고있는 것을보고 소리를 재생하기 위해 [I] .play soundID를 사용하는 루프를 사용합니다.

조금 늦더라도 도움이 되길 바랍니다.)

관련 문제