SoundPool.java revision 6cb9900e6f884adb6c9aa0243f2bf88985f671f8
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.media;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.AndroidRuntimeException;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.File;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.ParcelFileDescriptor;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.ref.WeakReference;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.AssetFileDescriptor;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks/**
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The SoundPool class manages and plays audio resources for applications.
31cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
32cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>A SoundPool is a collection of samples that can be loaded into memory
33cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * from a resource inside the APK or from a file in the file system. The
34cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * SoundPool library uses the MediaPlayer service to decode the audio
35cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * into a raw 16-bit PCM mono or stereo stream. This allows applications
36cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * to ship with compressed streams without having to suffer the CPU load
37cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * and latency of decompressing during playback.</p>
38cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
39cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>In addition to low-latency playback, SoundPool can also manage the number
40cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * of audio streams being rendered at once. When the SoundPool object is
41cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * constructed, the maxStreams parameter sets the maximum number of streams
42cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * that can be played at a time from this single SoundPool. SoundPool tracks
43cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * the number of active streams. If the maximum number of streams is exceeded,
44cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * SoundPool will automatically stop a previously playing stream based first
45cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * on priority and then by age within that priority. Limiting the maximum
46cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * number of streams helps to cap CPU loading and reducing the likelihood that
47cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * audio mixing will impact visuals or UI performance.</p>
48cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
496cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * <p>Sounds can be looped by setting a non-zero loop value. A value of -1
506cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * causes the sound to loop forever. In this case, the application must
516cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * explicitly call the stop() function to stop the sound. Any other non-zero
526cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * value will cause the sound to repeat the specified number of times, e.g.
536cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * a value of 3 causes the sound to play a total of 4 times.</p>
546cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks *
556cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * <p>The playback rate can also be changed. A playback rate of 1.0 causes
566cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * the sound to play at its original frequency (resampled, if necessary,
576cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * to the hardware output frequency). A playback rate of 2.0 causes the
586cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * sound to play at twice its original frequency, and a playback rate of
596cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * 0.5 causes it to play at half its original frequency. The playback
606cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * rate range is 0.5 to 2.0.</p>
616cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks *
62cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>Priority runs low to high, i.e. higher numbers are higher priority.
63cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * Priority is used when a call to play() would cause the number of active
64cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * streams to exceed the value established by the maxStreams parameter when
65cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * the SoundPool was created. In this case, the stream allocator will stop
66cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * the lowest priority stream. If there are multiple streams with the same
67cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * low priority, it will choose the oldest stream to stop. In the case
68cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * where the priority of the new stream is lower than all the active
69cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * streams, the new sound will not play and the play() function will return
70cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * a streamID of zero.</p>
71cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
72cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>Let's examine a typical use case: A game consists of several levels of
73cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * play. For each level, there is a set of unique sounds that are used only
74cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * by that level. In this case, the game logic should create a new SoundPool
75cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * object when the first level is loaded. The level data itself might contain
76cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * the list of sounds to be used by this level. The loading logic iterates
77cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * through the list of sounds calling the appropriate SoundPool.load()
78cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * function. This should typically be done early in the process to allow time
79cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * for decompressing the audio to raw PCM format before they are needed for
80cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * playback.</p>
81cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
82cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>Once the sounds are loaded and play has started, the application can
83cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * trigger sounds by calling SoundPool.play(). Playing streams can be
84cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * paused or resumed, and the application can also alter the pitch by
85cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * adjusting the playback rate in real-time for doppler or synthesis
86cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * effects.</p>
87cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks *
886cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * <p>Note that since streams can be stopped due to resource constraints, the
896cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * streamID is a reference to a particular instance of a stream. If the stream
906cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * is stopped to allow a higher priority stream to play, the stream is no
916cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * longer be valid. However, the application is allowed to call methods on
926cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * the streamID without error. This may help simplify program logic since
936cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks * the application need not concern itself with the stream lifecycle.</p>
946cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks *
95cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * <p>In our example, when the player has completed the level, the game
96cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * logic should call SoundPool.release() to release all the native resources
97cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * in use and then set the SoundPool reference to null. If the player starts
98cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * another level, a new SoundPool is created, sounds are loaded, and play
99cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks * resumes.</p>
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class SoundPool
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static { System.loadLibrary("soundpool"); }
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final static String TAG = "SoundPool";
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNativeContext; // accessed by native methods
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
109cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
110cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * Constructor. Constructs a SoundPool object with the following
111cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * characteristics:
112cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
113cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param maxStreams the maximum number of simultaneous streams for this
114cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *                   SoundPool object
115cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamType the audio stream type as described in AudioManager
116cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *                   For example, game applications will normally use
117cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *                   {@link AudioManager#STREAM_MUSIC}.
118cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param srcQuality the sample-rate converter quality. Currently has no
119cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *                   effect. Use 0 for the default.
120cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return a SoundPool object, or null if creation failed
121cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public SoundPool(int maxStreams, int streamType, int srcQuality) {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        native_setup(new WeakReference<SoundPool>(this), maxStreams, streamType, srcQuality);
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
126cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
1276cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Load the sound from the specified path.
1286cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *
129cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param path the path to the audio file
1306cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param priority the priority of the sound. Currently has no effect. Use
1316cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *                 a value of 1 for future compatibility.
132cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return a sound ID. This value can be used to play or unload the sound.
133cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int load(String path, int priority)
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // pass network streams to player
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (path.startsWith("http:"))
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return _load(path, priority);
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // try local path
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int id = 0;
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            File f = new File(path);
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (f != null) {
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (fd != null) {
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    id = _load(fd.getFileDescriptor(), 0, f.length(), priority);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    //Log.v(TAG, "close fd");
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    fd.close();
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (java.io.IOException e) {}
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return id;
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
156cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
1576cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Load the sound from the specified APK resource.
158cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
1596cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Note that the extension is dropped. For example, if you want to load
160cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * a sound from the raw resource file "explosion.mp3", you would specify
161cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * "R.raw.explosion" as the resource ID. Note that this means you cannot
162cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * have both an "explosion.wav" and an "explosion.mp3" in the res/raw
1636cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * directory.
164cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
165cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param context the application context
166cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param resId the resource ID
1676cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param priority the priority of the sound. Currently has no effect. Use
1686cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *                 a value of 1 for future compatibility.
169cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return a sound ID. This value can be used to play or unload the sound.
170cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int load(Context context, int resId, int priority) {
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int id = 0;
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (afd != null) {
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority);
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                //Log.v(TAG, "close fd");
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                afd.close();
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (java.io.IOException ex) {
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                //Log.d(TAG, "close failed:", ex);
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return id;
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
186cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
1876cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Load the sound from an asset file descriptor.
188cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
189cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param afd an asset file descriptor
1906cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param priority the priority of the sound. Currently has no effect. Use
1916cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *                 a value of 1 for future compatibility.
192cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return a sound ID. This value can be used to play or unload the sound.
193cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int load(AssetFileDescriptor afd, int priority) {
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (afd != null) {
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            long len = afd.getLength();
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (len < 0) {
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                throw new AndroidRuntimeException("no length for fd");
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return _load(afd.getFileDescriptor(), afd.getStartOffset(), len, priority);
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
206cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2076cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Load the sound from a FileDescriptor.
208cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2096cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * This version is useful if you store multiple sounds in a single
210cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * binary. The offset specifies the offset from the start of the file
2116cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * and the length specifies the length of the sound within the file.
212cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
213cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param fd a FileDescriptor object
214cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param offset offset to the start of the sound
215cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param length length of the sound
2166cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param priority the priority of the sound. Currently has no effect. Use
2176cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *                 a value of 1 for future compatibility.
218cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return a sound ID. This value can be used to play or unload the sound.
219cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int load(FileDescriptor fd, long offset, long length, int priority) {
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return _load(fd, offset, length, priority);
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int _load(String uri, int priority);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int _load(FileDescriptor fd, long offset, long length, int priority);
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
228cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2296cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Unload a sound from a sound ID.
230cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2316cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Unloads the sound specified by the soundID. This is the value
232cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * returned by the load() function. Returns true if the sound is
2336cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * successfully unloaded, false if the sound was already unloaded.
234cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
235cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param soundID a soundID returned by the load() function
236cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return true if just unloaded, false if previously unloaded
237cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final boolean unload(int soundID);
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
240cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2416cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Play a sound from a sound ID.
242cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2436cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Play the sound specified by the soundID. This is the value
244cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * returned by the load() function. Returns a non-zero streamID
245cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * if successful, zero if it fails. The streamID can be used to
246cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * further control playback. Note that calling play() may cause
247cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * another sound to stop playing if the maximum number of active
2486cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * streams is exceeded. A loop value of -1 means loop forever,
2496cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * a value of 0 means don't loop, other values indicate the
2506cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * number of repeats, e.g. a value of 1 plays the audio twice.
2516cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * The playback rate allows the application to vary the playback
2526cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * rate (pitch) of the sound. A value of 1.0 means play back at
2536cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * the original frequency. A value of 2.0 means play back twice
2546cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * as fast, and a value of 0.5 means playback at half speed.
255cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
256cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param soundID a soundID returned by the load() function
2576cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param leftVolume left volume value (range = 0.0 to 1.0)
2586cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param rightVolume right volume value (range = 0.0 to 1.0)
2596cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param priority stream priority (0 = lowest priority)
2606cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param loop loop mode (0 = no loop, -1 = loop forever)
2616cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
262cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @return non-zero streamID if successful, zero if failed
263cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final int play(int soundID, float leftVolume, float rightVolume,
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int priority, int loop, float rate);
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
267cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2686cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Pause a playback stream.
269cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2706cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Pause the stream specified by the streamID. This is the
271cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * value returned by the play() function. If the stream is
272cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * playing, it will be paused. If the stream is not playing
273cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * (e.g. is stopped or was previously paused), calling this
2746cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * function will have no effect.
275cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
276cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
277cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void pause(int streamID);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
280cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2816cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Resume a playback stream.
282cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2836cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Resume the stream specified by the streamID. This
284cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * is the value returned by the play() function. If the stream
285cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * is paused, this will resume playback. If the stream was not
2866cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * previously paused, calling this function will have no effect.
287cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
288cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
289cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void resume(int streamID);
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
292cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
2936cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Stop a playback stream.
294cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
2956cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Stop the stream specified by the streamID. This
296cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * is the value returned by the play() function. If the stream
297cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * is playing, it will be stopped. It also releases any native
298cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * resources associated with this stream. If the stream is not
2996cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * playing, it will have no effect.
300cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
301cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
302cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void stop(int streamID);
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
305cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
3066cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Set stream volume.
307cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
3086cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Sets the volume on the stream specified by the streamID.
309cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * This is the value returned by the play() function. The
310cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * value must be in the range of 0.0 to 1.0. If the stream does
3116cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * not exist, it will have no effect.
312cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
313cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
314cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param leftVolume left volume value (range = 0.0 to 1.0)
315cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param rightVolume right volume value (range = 0.0 to 1.0)
316cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void setVolume(int streamID,
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            float leftVolume, float rightVolume);
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
320cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
3216cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Change stream priority.
322cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
3236cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Change the priority of the stream specified by the streamID.
324cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * This is the value returned by the play() function. Affects the
3256cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * order in which streams are re-used to play new sounds. If the
3266cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * stream does not exist, it will have no effect.
327cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
328cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
329cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void setPriority(int streamID, int priority);
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
332cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks    /**
3336cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Set loop mode.
334cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
3356cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Change the loop mode. A loop value of -1 means loop forever,
3366cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * a value of 0 means don't loop, other values indicate the
3376cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * number of repeats, e.g. a value of 1 plays the audio twice.
3386cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * If the stream does not exist, it will have no effect.
339cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     *
340cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     * @param streamID a streamID returned by the play() function
3416cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param loop loop mode (0 = no loop, -1 = loop forever)
342cef302d0950a02fdc6920475d0c357d3949e85c3Dave Sparks     */
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void setLoop(int streamID, int loop);
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3456cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks    /**
3466cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Change playback rate.
3476cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *
3486cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * The playback rate allows the application to vary the playback
3496cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * rate (pitch) of the sound. A value of 1.0 means playback at
3506cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * the original frequency. A value of 2.0 means playback twice
3516cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * as fast, and a value of 0.5 means playback at half speed.
3526cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * If the stream does not exist, it will have no effect.
3536cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *
3546cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param streamID a streamID returned by the play() function
3556cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
3566cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     */
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void setRate(int streamID, float rate);
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3596cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks    /**
3606cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Release the SoundPool resources.
3616cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     *
3626cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * Release all memory and native resources used by the SoundPool
3636cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * object. The SoundPool can no longer be used and the reference
3646cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     * should be set to null.
3656cb9900e6f884adb6c9aa0243f2bf88985f671f8Dave Sparks     */
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public native final void release();
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_setup(Object mediaplayer_this,
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int maxStreams, int streamType, int srcQuality);
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void finalize() { release(); }
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
373