SoundPool

API:

The SoundPool class manages and plays audio resources for applications.

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.

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.

Sounds can be looped by setting a non-zero loop value. A value of -1 causes the sound to loop forever. In this case, the application must explicitly call the stop() function to stop the sound. Any other non-zero value will cause the sound to repeat the specified number of times, e.g. a value of 3 causes the sound to play a total of 4 times.

The playback rate can also be changed. A playback rate of 1.0 causes the sound to play at its original frequency (resampled, if necessary, to the hardware output frequency). A playback rate of 2.0 causes the sound to play at twice its original frequency, and a playback rate of 0.5 causes it to play at half its original frequency. The playback rate range is 0.5 to 2.0.

Priority runs low to high, i.e. higher numbers are higher priority. Priority is used when a call to play() would cause the number of active streams to exceed the value established by the maxStreams parameter when the SoundPool was created. In this case, the stream allocator will stop the lowest priority stream. If there are multiple streams with the same low priority, it will choose the oldest stream to stop. In the case where the priority of the new stream is lower than all the active streams, the new sound will not play and the play() function will return a streamID of zero.

Let's examine a typical use case: A game consists of several levels of play. For each level, there is a set of unique sounds that are used only by that level. In this case, the game logic should create a new SoundPool object when the first level is loaded. The level data itself might contain the list of sounds to be used by this level. The loading logic iterates through the list of sounds calling the appropriate SoundPool.load() function. This should typically be done early in the process to allow time for decompressing the audio to raw PCM format before they are needed for playback.

Once the sounds are loaded and play has started, the application can trigger sounds by calling SoundPool.play(). Playing streams can be paused or resumed, and the application can also alter the pitch by adjusting the playback rate in real-time for doppler or synthesis effects.

Note that since streams can be stopped due to resource constraints, the streamID is a reference to a particular instance of a stream. If the stream is stopped to allow a higher priority stream to play, the stream is no longer be valid. However, the application is allowed to call methods on the streamID without error. This may help simplify program logic since the application need not concern itself with the stream lifecycle.

In our example, when the player has completed the level, the game logic should call SoundPool.release() to release all the native resources in use and then set the SoundPool reference to null. If the player starts another level, a new SoundPool is created, sounds are loaded, and play resumes.


SoundPool类管理和播放应用程序的音频资源。


SoundPool是可以从APK内部的资源或文件系统中的文件加载到内存中的样本集合。 SoundPool库使用MediaPlayer服务将音频解码为原始16位PCM单声道或立体声流。这允许应用程序随压缩流提供,而不必承受CPU负载和播放期间解压缩的延迟。


除了低延迟播放,SoundPool还可以管理一次呈现的音频流的数量。当构建SoundPool对象时,maxStreams参数设置从此单个SoundPool一次可以播放的流的最大数量。 SoundPool跟踪活动流的数量。如果超过流的最大数量,SoundPool将首先根据优先级自动停止先前播放的流,然后根据该优先级中的年龄自动停止。限制流的最大数量有助于限制CPU负载,并降低音频混合将影响视觉效果或UI性能的可能性。


声音可以通过设置非零循环值来循环。值为-1会使声音永远循环。在这种情况下,应用程序必须显式调用stop()函数来停止声音。任何其它非零值将导致声音重复指定的次数,例如。值3使声音总共播放4次。


播放速率也可以更改。回放速率1.0使声音以其原始频率播放(如有必要,重新采样到硬件输出频率)。 2.0的播放速率使声音以其原始频率的两倍播放,并且0.5的播放速率使其以其原始频率的一半播放。播放速率范围为0.5到2.0。


优先级从低到高,即更高的数字是更高的优先级。当调用play()将导致活动流的数量超过在创建SoundPool时由maxStreams参数建立的值时使用优先级。在这种情况下,流分配器将停止最低优先级流。如果有多个流具有相同的低优先级,它将选择最旧的流停止。在新流的优先级低于所有活动流的情况下,新的声音将不会播放,并且play()函数将返回零的streamID。


让我们来看一个典型的用例:游戏包含几个层次的游戏。对于每个级别,存在仅由该级别使用的一组唯一声音。在这种情况下,游戏逻辑应该在加载第一级时创建一个新的SoundPool对象。级别数据本身可能包含此级别要使用的声音列表。加载逻辑通过调用合适的SoundPool.load()函数的声音列表进行迭代。这通常应该在该过程的早期完成,以便在需要用于将音频解压缩到原始PCM格式之前允许时间用于回放。


一旦声音加载并开始播放,应用程序可以通过调用SoundPool.play()触发声音。播放流可以暂停或恢复,并且应用程序还可以通过实时调整播放速率以改变多普勒效应或合成效果来改变音调。


注意,由于流可以由于资源限制而停止,所以streamID是对流的特定实例的引用。如果流被停止以允许较高优先级流播放,则流不再有效。但是,应用程序允许调用streamID上的方法,没有错误。这可以帮助简化程序逻辑,因为应用程序不需要关心流生存周期。


在我们的例子中,当玩家完成关卡时,游戏逻辑应该调用SoundPool.release()来释放所有使用的本地资源,然后将SoundPool引用设置为null。如果玩家开始另一个级别,创建一个新的SoundPool,加载声音,然后恢复播放。




在使用过程中 发现 SoundPool播放音频文件时 会使得两个文件同时播放,无法做到顺序播放。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值