2014-06-19 2 views
3

객관식 질문이있는 수학 게임이 있습니다. 나는 정답은 벨 소리를, 오답은 오인 (oink) 소리를 낸다. 플레이어가 신속하게 답변을 입력하도록 권장하는 시간 지정 재생 모드가 있습니다. 응답 버튼을 천천히 누르면 소리가 잘 들립니다. 그러나 빠른 연속으로 소리가 들리지 않으면.연속적인 버튼 클릭 후 Android 사운드가 재생되지 않습니다.

두 개의 소리는 각각 1 초의 일부입니다. 내가 ting.start();이 정답을 처리하는 방법에

, 그리고 (내가 노력 oink.start();

한 가지가 강제로 내가 원하는 단지를 시작 후 두 소리를 중지하는 것이 었습니다 오답 방법에 아래 참조)하지만 소리는 전혀 재생되지 않습니다! 어떤 제안?

private void handleWrongAnswer(int chosenID) 


{ 
//  Toast.makeText(getApplicationContext(), "Oops, try again.", Toast.LENGTH_SHORT).show(); 
     Button checked_button = (Button) findViewById(chosenID); 
     checked_button.setEnabled(false); 
     checked_button.setClickable(false); 
     checked_button.setBackgroundColor(Color.rgb(255,100,100)); 

     score = score - 1.0/3.0; 
     score_field.setText(String.format("%.2f", score)); 

      ting.stop(); 
      oink.stop(); 
     oink.start(); 
    } 

답변

1

음, 나는 그 소리를 재생하기 위해 Mediaplayer를 사용하고 있다고 가정합니다. 작은 오디오 클립을 재생하고 작은 시간에 반복하려면 soundPool을 사용해야합니다.

당신은 이런 식으로 구현해야합니다 :

private SoundPool soundPool; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // 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); 
} 

와 당신이 그것을 사용하고자 할 때 :

// 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); 
} 

어쨌든이 here에 대한 좋은 자습서를 가지고있다.

관련 문제