2014-02-13 31 views
-1

在我的應用程序中,我播放聲音,如果用戶按佈局。但問題是如果用戶一次又一次地按佈局。一遍又一遍地重複着。如果聲音播放,我希望佈局變爲禁用,並在聲音結束時啓用。以便用戶在完成後能夠再次播放聲音。如何根據聲音播放禁用和啓用佈局android

代碼 -

public class Boy1 extends Activity implements OnTouchListener { 

    private SoundPool soundPool; 
    private int soundID; 
    boolean loaded = false; 
    private LinearLayout layout1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.boy); 
     layout1 = (LinearLayout) findViewById (R.id.layout1); 
     layout1.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.test, 1); 

    } 

    @Override 
    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

沒有它整個代碼。 –

+0

我使用的是soundpool不是媒體播放器,正如大多數人所建議的那樣,對於持續時間較長的聲音使用soundpool。 –

+0

你知道音頻聲音的持續時間嗎? –

回答

0

保持聲音的布爾是打還是不......,並基於該禁用或啓用佈局如下...

if (isSoundPlaying) { 
    layout1.setEnabled(false); 
} else { 
    layout1.setEnabled(true); 
} 

更新:

當聲音開始播放時,然後寫入layout1.setEnabled (假);它會禁用佈局。據我所知soundPool.play(soundID,音量,音量,1,0,1f);啓動聲音鋪設然後寫...

soundPool.play(soundID, volume, volume, 1, 0, 1f); 
layout1.setEnabled(false); 

而當聲音播放停止然後寫這將使佈局...

layout1.setEnabled(true); 
+0

我必須添加這個?和哪裏? –

+0

看到我更新的答案。 –

+0

並在哪裏添加layout1.setEnabled(true);在我的代碼? –