【问题标题】:Android: Is there a way to detect if a sound from SoundPool is still playingAndroid:有没有办法检测来自 SoundPool 的声音是否仍在播放
【发布时间】:2018-01-26 09:38:10
【问题描述】:

我正在写一个音板。我希望这样当一个人按下按钮播放声音时,它会首先查看声音是否仍在播放。所以它可以停止那个声音并开始新的声音。

这是一种查看声音 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);

}

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以使用AudioManagerisMusicActive()方法。

    【讨论】:

      【解决方案2】:
      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) 检查是否有任何音乐处于活动状态。

      【讨论】:

      • Android 类 AudioSystem 现在已替换为 AudioManager 类,似乎没有像 isStreamActive() 这样的方法。
      • 不工作:(此方法不再可用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多