Android SoundPool 最大同时播放音频量maxStreams简介

 SoudPool的构造方法的第一个参数maxStreams表示当前SoundPool最大能够同时播放多少个音频,具体可以看如下SoudPool构造方法的源码

/**
 * Constructor. Constructs a SoundPool object with the following
 * characteristics:
 *
 * @param maxStreams the maximum number of simultaneous streams for this
 *                   SoundPool object
 * @param streamType the audio stream type as described in AudioManager 
 *                   For example, game applications will normally use
 *                   {@link AudioManager#STREAM_MUSIC}.
 * @param srcQuality the sample-rate converter quality. Currently has no
 *                   effect. Use 0 for the default.
 * @return a SoundPool object, or null if creation failed
 * @deprecated use {@link SoundPool.Builder} instead to create and configure a
 *     SoundPool instance
 */
public SoundPool(int maxStreams, int streamType, int srcQuality) {
    this(maxStreams,
            new AudioAttributes.Builder().setInternalLegacyStreamType(streamType).build());
    PlayerBase.deprecateStreamTypeForPlayback(streamType, "SoundPool", "SoundPool()");
}

 在SoudPool源码里还有如下一段对maxStreams的说明,总结为如下几点

  • SoundPool能够管理同一时间内音频播放的数量
  • SoundPool跟踪当前播放的音频数量,若数量超了,会按一定规则停止先前播放的音频
  • 数量超过maxStreams停止播放的规则为,首先按音频的优先级,然后按音频已经播放的时长
  • 限制maxStreams的数量,减小对UI交互性能的影响
 * <p>In addition to low-latency playback, SoundPool can also manage the number
 * of audio streams being rendered at once. When the SoundPool object is
 * constructed, the maxStreams parameter sets the maximum number of streams
 * that can be played at a time from this single SoundPool. SoundPool tracks
 * the number of active streams. If the maximum number of streams is exceeded,
 * SoundPool will automatically stop a previously playing stream based first
 * on priority and then by age within that priority. Limiting the maximum
 * number of streams helps to cap CPU loading and reducing the likelihood that
 * audio mixing will impact visuals or UI performance.</p> 

maxStreams大小的设置应该根据具体需求具体场景来定,先预估同一时间最多可能有多少个音频同时播放,然后将maxStreams设置为该值,太小了容易出现有的音频没播放完就被停止,设置太大浪费CPU资源,影响性能。

posted @ 2019-07-23 20:20  野猿新一  阅读(56)  评论(0编辑  收藏  举报