2014-02-13 33 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);在我的代码? –