2013-02-28 67 views
1

我正在寫一個聲卡。我喜歡它,所以當一個人按下按鈕播放聲音時,它會首先看到聲音是否仍在播放。因此,它可以停止這個聲音,並開始新的。Android:有沒有辦法檢測SoundPool聲音是否仍在播放

是thier的方式,看是否有聲音ID還在玩,我jsing下面的代碼來開始我的聲音

// Sound code 
void StartSound(int id) 
{ 
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    float actualVolume = (float) audioManager 
      .getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager 
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

    float volume=(float) ((float)cGlobals.gVol/100.00); 
    // Is the sound loaded already? 

     soundPool.play(id, volume, volume, 1, 0, 1f); 

} 

回答

0
class AudioSystem{ 
    /* 
    * Checks whether the specified stream type is active. 
    * 
    * return true if any track playing on this stream is active. 
    */ 
    public static native boolean isStreamActive(int stream, int inPastMs); 
} 

而對於流:

/** The audio stream for phone calls */ 
public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; 
/** The audio stream for system sounds */ 
public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; 
/** The audio stream for the phone ring */ 
public static final int STREAM_RING = AudioSystem.STREAM_RING; 
/** The audio stream for music playback */ 
public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC; 
/** The audio stream for alarms */ 
public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM; 
/** The audio stream for notifications */ 
public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; 
/** @hide The audio stream for phone calls when connected to bluetooth */ 
public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO; 
/** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */ 
public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED; 
/** The audio stream for DTMF Tones */ 
public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF; 
/** @hide The audio stream for text to speech (TTS) */ 
public static final int STREAM_TTS = AudioSystem.STREAM_TTS; 

檢查無論您想要檢查什麼類型的音頻流。例如,AudioSystem.isStreamActive(STREAM_MUSIC, 0)檢查是否有音樂處於活動狀態。

+1

Android類'AudioSystem'現在被['AudioManager'](http://developer.android.com/reference/android/media/AudioManager.html)類取代,看起來好像沒有像' isStreamActive()'。 – Micer 2013-11-03 17:11:19

+0

不工作:(此方法不再可用。 – 2015-03-30 11:52:14

1

您可以使用isMusicActive()方法AudioManager

相關問題