1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.content.res.AssetFileDescriptor; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.os.Message; 26 import android.os.ParcelFileDescriptor; 27 import android.util.AndroidRuntimeException; 28 import android.util.Log; 29 30 import java.io.File; 31 import java.io.FileDescriptor; 32 import java.util.concurrent.atomic.AtomicReference; 33 34 35 /** 36 * The SoundPool class manages and plays audio resources for applications. 37 * 38 * <p>A SoundPool is a collection of sound samples that can be loaded into memory 39 * from a resource inside the APK or from a file in the file system. The 40 * SoundPool library uses the MediaCodec service to decode the audio 41 * into raw 16-bit PCM. This allows applications 42 * to ship with compressed streams without having to suffer the CPU load 43 * and latency of decompressing during playback.</p> 44 * 45 * <p>Soundpool sounds are expected to be short as they are 46 * predecoded into memory. Each decoded sound is internally limited to one 47 * megabyte storage, which represents approximately 5.6 seconds at 44.1kHz stereo 48 * (the duration is proportionally longer at lower sample rates or 49 * a channel mask of mono). A decoded audio sound will be truncated if it would 50 * exceed the per-sound one megabyte storage space.</p> 51 * 52 * <p>In addition to low-latency playback, SoundPool can also manage the number 53 * of audio streams being rendered at once. When the SoundPool object is 54 * constructed, the maxStreams parameter sets the maximum number of streams 55 * that can be played at a time from this single SoundPool. SoundPool tracks 56 * the number of active streams. If the maximum number of streams is exceeded, 57 * SoundPool will automatically stop a previously playing stream based first 58 * on priority and then by age within that priority. Limiting the maximum 59 * number of streams helps to cap CPU loading and reducing the likelihood that 60 * audio mixing will impact visuals or UI performance.</p> 61 * 62 * <p>Sounds can be looped by setting a non-zero loop value. A value of -1 63 * causes the sound to loop forever. In this case, the application must 64 * explicitly call the stop() function to stop the sound. Any other non-zero 65 * value will cause the sound to repeat the specified number of times, e.g. 66 * a value of 3 causes the sound to play a total of 4 times.</p> 67 * 68 * <p>The playback rate can also be changed. A playback rate of 1.0 causes 69 * the sound to play at its original frequency (resampled, if necessary, 70 * to the hardware output frequency). A playback rate of 2.0 causes the 71 * sound to play at twice its original frequency, and a playback rate of 72 * 0.5 causes it to play at half its original frequency. The playback 73 * rate range is 0.5 to 2.0.</p> 74 * 75 * <p>Priority runs low to high, i.e. higher numbers are higher priority. 76 * Priority is used when a call to play() would cause the number of active 77 * streams to exceed the value established by the maxStreams parameter when 78 * the SoundPool was created. In this case, the stream allocator will stop 79 * the lowest priority stream. If there are multiple streams with the same 80 * low priority, it will choose the oldest stream to stop. In the case 81 * where the priority of the new stream is lower than all the active 82 * streams, the new sound will not play and the play() function will return 83 * a streamID of zero.</p> 84 * 85 * <p>Let's examine a typical use case: A game consists of several levels of 86 * play. For each level, there is a set of unique sounds that are used only 87 * by that level. In this case, the game logic should create a new SoundPool 88 * object when the first level is loaded. The level data itself might contain 89 * the list of sounds to be used by this level. The loading logic iterates 90 * through the list of sounds calling the appropriate SoundPool.load() 91 * function. This should typically be done early in the process to allow time 92 * for decompressing the audio to raw PCM format before they are needed for 93 * playback.</p> 94 * 95 * <p>Once the sounds are loaded and play has started, the application can 96 * trigger sounds by calling SoundPool.play(). Playing streams can be 97 * paused or resumed, and the application can also alter the pitch by 98 * adjusting the playback rate in real-time for doppler or synthesis 99 * effects.</p> 100 * 101 * <p>Note that since streams can be stopped due to resource constraints, the 102 * streamID is a reference to a particular instance of a stream. If the stream 103 * is stopped to allow a higher priority stream to play, the stream is no 104 * longer valid. However, the application is allowed to call methods on 105 * the streamID without error. This may help simplify program logic since 106 * the application need not concern itself with the stream lifecycle.</p> 107 * 108 * <p>In our example, when the player has completed the level, the game 109 * logic should call SoundPool.release() to release all the native resources 110 * in use and then set the SoundPool reference to null. If the player starts 111 * another level, a new SoundPool is created, sounds are loaded, and play 112 * resumes.</p> 113 */ 114 public class SoundPool extends PlayerBase { 115 static { System.loadLibrary("soundpool"); } 116 117 // SoundPool messages 118 // 119 // must match SoundPool.h 120 private static final int SAMPLE_LOADED = 1; 121 122 private final static String TAG = "SoundPool"; 123 private final static boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 124 125 private final AtomicReference<EventHandler> mEventHandler = new AtomicReference<>(null); 126 127 private long mNativeContext; // accessed by native methods 128 129 private boolean mHasAppOpsPlayAudio; 130 131 private final AudioAttributes mAttributes; 132 133 /** 134 * Constructor. Constructs a SoundPool object with the following 135 * characteristics: 136 * 137 * @param maxStreams the maximum number of simultaneous streams for this 138 * SoundPool object 139 * @param streamType the audio stream type as described in AudioManager 140 * For example, game applications will normally use 141 * {@link AudioManager#STREAM_MUSIC}. 142 * @param srcQuality the sample-rate converter quality. Currently has no 143 * effect. Use 0 for the default. 144 * @return a SoundPool object, or null if creation failed 145 * @deprecated use {@link SoundPool.Builder} instead to create and configure a 146 * SoundPool instance 147 */ SoundPool(int maxStreams, int streamType, int srcQuality)148 public SoundPool(int maxStreams, int streamType, int srcQuality) { 149 this(maxStreams, 150 new AudioAttributes.Builder().setInternalLegacyStreamType(streamType).build()); 151 PlayerBase.deprecateStreamTypeForPlayback(streamType, "SoundPool", "SoundPool()"); 152 } 153 SoundPool(int maxStreams, AudioAttributes attributes)154 private SoundPool(int maxStreams, AudioAttributes attributes) { 155 super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_JAM_SOUNDPOOL); 156 157 // do native setup 158 if (native_setup(maxStreams, attributes, getCurrentOpPackageName()) != 0) { 159 throw new RuntimeException("Native setup failed"); 160 } 161 mAttributes = attributes; 162 163 // FIXME: b/174876164 implement session id for soundpool 164 baseRegisterPlayer(AudioSystem.AUDIO_SESSION_ALLOCATE); 165 } 166 167 /** 168 * Release the SoundPool resources. 169 * 170 * Release all memory and native resources used by the SoundPool 171 * object. The SoundPool can no longer be used and the reference 172 * should be set to null. 173 */ release()174 public final void release() { 175 baseRelease(); 176 native_release(); 177 } 178 native_release()179 private native final void native_release(); 180 finalize()181 protected void finalize() { release(); } 182 183 /** 184 * Load the sound from the specified path. 185 * 186 * @param path the path to the audio file 187 * @param priority the priority of the sound. Currently has no effect. Use 188 * a value of 1 for future compatibility. 189 * @return a sound ID. This value can be used to play or unload the sound. 190 */ load(String path, int priority)191 public int load(String path, int priority) { 192 int id = 0; 193 try { 194 File f = new File(path); 195 ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, 196 ParcelFileDescriptor.MODE_READ_ONLY); 197 if (fd != null) { 198 id = _load(fd.getFileDescriptor(), 0, f.length(), priority); 199 fd.close(); 200 } 201 } catch (java.io.IOException e) { 202 Log.e(TAG, "error loading " + path); 203 } 204 return id; 205 } 206 207 /** 208 * Load the sound from the specified APK resource. 209 * 210 * Note that the extension is dropped. For example, if you want to load 211 * a sound from the raw resource file "explosion.mp3", you would specify 212 * "R.raw.explosion" as the resource ID. Note that this means you cannot 213 * have both an "explosion.wav" and an "explosion.mp3" in the res/raw 214 * directory. 215 * 216 * @param context the application context 217 * @param resId the resource ID 218 * @param priority the priority of the sound. Currently has no effect. Use 219 * a value of 1 for future compatibility. 220 * @return a sound ID. This value can be used to play or unload the sound. 221 */ load(Context context, int resId, int priority)222 public int load(Context context, int resId, int priority) { 223 AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId); 224 int id = 0; 225 if (afd != null) { 226 id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority); 227 try { 228 afd.close(); 229 } catch (java.io.IOException ex) { 230 //Log.d(TAG, "close failed:", ex); 231 } 232 } 233 return id; 234 } 235 236 /** 237 * Load the sound from an asset file descriptor. 238 * 239 * @param afd an asset file descriptor 240 * @param priority the priority of the sound. Currently has no effect. Use 241 * a value of 1 for future compatibility. 242 * @return a sound ID. This value can be used to play or unload the sound. 243 */ load(AssetFileDescriptor afd, int priority)244 public int load(AssetFileDescriptor afd, int priority) { 245 if (afd != null) { 246 long len = afd.getLength(); 247 if (len < 0) { 248 throw new AndroidRuntimeException("no length for fd"); 249 } 250 return _load(afd.getFileDescriptor(), afd.getStartOffset(), len, priority); 251 } else { 252 return 0; 253 } 254 } 255 256 /** 257 * Load the sound from a FileDescriptor. 258 * 259 * This version is useful if you store multiple sounds in a single 260 * binary. The offset specifies the offset from the start of the file 261 * and the length specifies the length of the sound within the file. 262 * 263 * @param fd a FileDescriptor object 264 * @param offset offset to the start of the sound 265 * @param length length of the sound 266 * @param priority the priority of the sound. Currently has no effect. Use 267 * a value of 1 for future compatibility. 268 * @return a sound ID. This value can be used to play or unload the sound. 269 */ load(FileDescriptor fd, long offset, long length, int priority)270 public int load(FileDescriptor fd, long offset, long length, int priority) { 271 return _load(fd, offset, length, priority); 272 } 273 274 /** 275 * Unload a sound from a sound ID. 276 * 277 * Unloads the sound specified by the soundID. This is the value 278 * returned by the load() function. Returns true if the sound is 279 * successfully unloaded, false if the sound was already unloaded. 280 * 281 * @param soundID a soundID returned by the load() function 282 * @return true if just unloaded, false if previously unloaded 283 */ unload(int soundID)284 public native final boolean unload(int soundID); 285 286 /** 287 * Play a sound from a sound ID. 288 * 289 * Play the sound specified by the soundID. This is the value 290 * returned by the load() function. Returns a non-zero streamID 291 * if successful, zero if it fails. The streamID can be used to 292 * further control playback. Note that calling play() may cause 293 * another sound to stop playing if the maximum number of active 294 * streams is exceeded. A loop value of -1 means loop forever, 295 * a value of 0 means don't loop, other values indicate the 296 * number of repeats, e.g. a value of 1 plays the audio twice. 297 * The playback rate allows the application to vary the playback 298 * rate (pitch) of the sound. A value of 1.0 means play back at 299 * the original frequency. A value of 2.0 means play back twice 300 * as fast, and a value of 0.5 means playback at half speed. 301 * 302 * @param soundID a soundID returned by the load() function 303 * @param leftVolume left volume value (range = 0.0 to 1.0) 304 * @param rightVolume right volume value (range = 0.0 to 1.0) 305 * @param priority stream priority (0 = lowest priority) 306 * @param loop loop mode (0 = no loop, -1 = loop forever) 307 * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0) 308 * @return non-zero streamID if successful, zero if failed 309 */ play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)310 public final int play(int soundID, float leftVolume, float rightVolume, 311 int priority, int loop, float rate) { 312 // FIXME: b/174876164 implement device id for soundpool 313 baseStart(0); 314 return _play(soundID, leftVolume, rightVolume, priority, loop, rate); 315 } 316 317 /** 318 * Pause a playback stream. 319 * 320 * Pause the stream specified by the streamID. This is the 321 * value returned by the play() function. If the stream is 322 * playing, it will be paused. If the stream is not playing 323 * (e.g. is stopped or was previously paused), calling this 324 * function will have no effect. 325 * 326 * @param streamID a streamID returned by the play() function 327 */ pause(int streamID)328 public native final void pause(int streamID); 329 330 /** 331 * Resume a playback stream. 332 * 333 * Resume the stream specified by the streamID. This 334 * is the value returned by the play() function. If the stream 335 * is paused, this will resume playback. If the stream was not 336 * previously paused, calling this function will have no effect. 337 * 338 * @param streamID a streamID returned by the play() function 339 */ resume(int streamID)340 public native final void resume(int streamID); 341 342 /** 343 * Pause all active streams. 344 * 345 * Pause all streams that are currently playing. This function 346 * iterates through all the active streams and pauses any that 347 * are playing. It also sets a flag so that any streams that 348 * are playing can be resumed by calling autoResume(). 349 */ autoPause()350 public native final void autoPause(); 351 352 /** 353 * Resume all previously active streams. 354 * 355 * Automatically resumes all streams that were paused in previous 356 * calls to autoPause(). 357 */ autoResume()358 public native final void autoResume(); 359 360 /** 361 * Stop a playback stream. 362 * 363 * Stop the stream specified by the streamID. This 364 * is the value returned by the play() function. If the stream 365 * is playing, it will be stopped. It also releases any native 366 * resources associated with this stream. If the stream is not 367 * playing, it will have no effect. 368 * 369 * @param streamID a streamID returned by the play() function 370 */ stop(int streamID)371 public native final void stop(int streamID); 372 373 /** 374 * Set stream volume. 375 * 376 * Sets the volume on the stream specified by the streamID. 377 * This is the value returned by the play() function. The 378 * value must be in the range of 0.0 to 1.0. If the stream does 379 * not exist, it will have no effect. 380 * 381 * @param streamID a streamID returned by the play() function 382 * @param leftVolume left volume value (range = 0.0 to 1.0) 383 * @param rightVolume right volume value (range = 0.0 to 1.0) 384 */ setVolume(int streamID, float leftVolume, float rightVolume)385 public final void setVolume(int streamID, float leftVolume, float rightVolume) { 386 // unlike other subclasses of PlayerBase, we are not calling 387 // baseSetVolume(leftVolume, rightVolume) as we need to keep track of each 388 // volume separately for each player, so we still send the command, but 389 // handle mute/unmute separately through playerSetVolume() 390 _setVolume(streamID, leftVolume, rightVolume); 391 } 392 393 @Override playerApplyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @Nullable VolumeShaper.Operation operation)394 /* package */ int playerApplyVolumeShaper( 395 @NonNull VolumeShaper.Configuration configuration, 396 @Nullable VolumeShaper.Operation operation) { 397 return -1; 398 } 399 400 @Override playerGetVolumeShaperState(int id)401 /* package */ @Nullable VolumeShaper.State playerGetVolumeShaperState(int id) { 402 return null; 403 } 404 405 @Override playerSetVolume(boolean muting, float leftVolume, float rightVolume)406 void playerSetVolume(boolean muting, float leftVolume, float rightVolume) { 407 // not used here to control the player volume directly, but used to mute/unmute 408 _mute(muting); 409 } 410 411 @Override playerSetAuxEffectSendLevel(boolean muting, float level)412 int playerSetAuxEffectSendLevel(boolean muting, float level) { 413 // no aux send functionality so no-op 414 return AudioSystem.SUCCESS; 415 } 416 417 @Override playerStart()418 void playerStart() { 419 // FIXME implement resuming any paused sound 420 } 421 422 @Override playerPause()423 void playerPause() { 424 // FIXME implement pausing any playing sound 425 } 426 427 @Override playerStop()428 void playerStop() { 429 // FIXME implement pausing any playing sound 430 } 431 432 /** 433 * Similar, except set volume of all channels to same value. 434 * @hide 435 */ setVolume(int streamID, float volume)436 public void setVolume(int streamID, float volume) { 437 setVolume(streamID, volume, volume); 438 } 439 440 /** 441 * Change stream priority. 442 * 443 * Change the priority of the stream specified by the streamID. 444 * This is the value returned by the play() function. Affects the 445 * order in which streams are re-used to play new sounds. If the 446 * stream does not exist, it will have no effect. 447 * 448 * @param streamID a streamID returned by the play() function 449 */ setPriority(int streamID, int priority)450 public native final void setPriority(int streamID, int priority); 451 452 /** 453 * Set loop mode. 454 * 455 * Change the loop mode. A loop value of -1 means loop forever, 456 * a value of 0 means don't loop, other values indicate the 457 * number of repeats, e.g. a value of 1 plays the audio twice. 458 * If the stream does not exist, it will have no effect. 459 * 460 * @param streamID a streamID returned by the play() function 461 * @param loop loop mode (0 = no loop, -1 = loop forever) 462 */ setLoop(int streamID, int loop)463 public native final void setLoop(int streamID, int loop); 464 465 /** 466 * Change playback rate. 467 * 468 * The playback rate allows the application to vary the playback 469 * rate (pitch) of the sound. A value of 1.0 means playback at 470 * the original frequency. A value of 2.0 means playback twice 471 * as fast, and a value of 0.5 means playback at half speed. 472 * If the stream does not exist, it will have no effect. 473 * 474 * @param streamID a streamID returned by the play() function 475 * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0) 476 */ setRate(int streamID, float rate)477 public native final void setRate(int streamID, float rate); 478 479 public interface OnLoadCompleteListener { 480 /** 481 * Called when a sound has completed loading. 482 * 483 * @param soundPool SoundPool object from the load() method 484 * @param sampleId the sample ID of the sound loaded. 485 * @param status the status of the load operation (0 = success) 486 */ onLoadComplete(SoundPool soundPool, int sampleId, int status)487 public void onLoadComplete(SoundPool soundPool, int sampleId, int status); 488 } 489 490 /** 491 * Sets the callback hook for the OnLoadCompleteListener. 492 */ setOnLoadCompleteListener(OnLoadCompleteListener listener)493 public void setOnLoadCompleteListener(OnLoadCompleteListener listener) { 494 if (listener == null) { 495 mEventHandler.set(null); 496 return; 497 } 498 499 Looper looper; 500 if ((looper = Looper.myLooper()) != null) { 501 mEventHandler.set(new EventHandler(looper, listener)); 502 } else if ((looper = Looper.getMainLooper()) != null) { 503 mEventHandler.set(new EventHandler(looper, listener)); 504 } else { 505 mEventHandler.set(null); 506 } 507 } 508 _load(FileDescriptor fd, long offset, long length, int priority)509 private native final int _load(FileDescriptor fd, long offset, long length, int priority); 510 native_setup(int maxStreams, @NonNull Object attributes, @NonNull String opPackageName)511 private native int native_setup(int maxStreams, 512 @NonNull Object/*AudioAttributes*/ attributes, @NonNull String opPackageName); 513 _play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)514 private native final int _play(int soundID, float leftVolume, float rightVolume, 515 int priority, int loop, float rate); 516 _setVolume(int streamID, float leftVolume, float rightVolume)517 private native final void _setVolume(int streamID, float leftVolume, float rightVolume); 518 _mute(boolean muting)519 private native final void _mute(boolean muting); 520 521 // post event from native code to message handler 522 @SuppressWarnings("unchecked") postEventFromNative(int msg, int arg1, int arg2, Object obj)523 private void postEventFromNative(int msg, int arg1, int arg2, Object obj) { 524 Handler eventHandler = mEventHandler.get(); 525 if (eventHandler == null) { 526 return; 527 } 528 Message message = eventHandler.obtainMessage(msg, arg1, arg2, obj); 529 eventHandler.sendMessage(message); 530 } 531 532 private final class EventHandler extends Handler { 533 private final OnLoadCompleteListener mOnLoadCompleteListener; 534 EventHandler(Looper looper, @NonNull OnLoadCompleteListener onLoadCompleteListener)535 EventHandler(Looper looper, @NonNull OnLoadCompleteListener onLoadCompleteListener) { 536 super(looper); 537 mOnLoadCompleteListener = onLoadCompleteListener; 538 } 539 540 @Override handleMessage(Message msg)541 public void handleMessage(Message msg) { 542 if (msg.what != SAMPLE_LOADED) { 543 Log.e(TAG, "Unknown message type " + msg.what); 544 return; 545 } 546 547 if (DEBUG) Log.d(TAG, "Sample " + msg.arg1 + " loaded"); 548 mOnLoadCompleteListener.onLoadComplete(SoundPool.this, msg.arg1, msg.arg2); 549 } 550 } 551 552 /** 553 * Builder class for {@link SoundPool} objects. 554 */ 555 public static class Builder { 556 private int mMaxStreams = 1; 557 private AudioAttributes mAudioAttributes; 558 559 /** 560 * Constructs a new Builder with the defaults format values. 561 * If not provided, the maximum number of streams is 1 (see {@link #setMaxStreams(int)} to 562 * change it), and the audio attributes have a usage value of 563 * {@link AudioAttributes#USAGE_MEDIA} (see {@link #setAudioAttributes(AudioAttributes)} to 564 * change them). 565 */ Builder()566 public Builder() { 567 } 568 569 /** 570 * Sets the maximum of number of simultaneous streams that can be played simultaneously. 571 * @param maxStreams a value equal to 1 or greater. 572 * @return the same Builder instance 573 * @throws IllegalArgumentException 574 */ setMaxStreams(int maxStreams)575 public Builder setMaxStreams(int maxStreams) throws IllegalArgumentException { 576 if (maxStreams <= 0) { 577 throw new IllegalArgumentException( 578 "Strictly positive value required for the maximum number of streams"); 579 } 580 mMaxStreams = maxStreams; 581 return this; 582 } 583 584 /** 585 * Sets the {@link AudioAttributes}. For examples, game applications will use attributes 586 * built with usage information set to {@link AudioAttributes#USAGE_GAME}. 587 * @param attributes a non-null 588 * @return 589 */ setAudioAttributes(AudioAttributes attributes)590 public Builder setAudioAttributes(AudioAttributes attributes) 591 throws IllegalArgumentException { 592 if (attributes == null) { 593 throw new IllegalArgumentException("Invalid null AudioAttributes"); 594 } 595 mAudioAttributes = attributes; 596 return this; 597 } 598 build()599 public SoundPool build() { 600 if (mAudioAttributes == null) { 601 mAudioAttributes = new AudioAttributes.Builder() 602 .setUsage(AudioAttributes.USAGE_MEDIA).build(); 603 } 604 return new SoundPool(mMaxStreams, mAudioAttributes); 605 } 606 } 607 } 608