SoundPool.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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
17package android.media;
18
19import android.util.Log;
20import java.io.File;
21import java.io.FileDescriptor;
22import android.os.ParcelFileDescriptor;
23import java.lang.ref.WeakReference;
24import android.content.Context;
25import android.content.res.AssetFileDescriptor;
26import java.io.IOException;
27
28/*
29 * The SoundPool class manages and plays audio resources for applications.
30 */
31public class SoundPool
32{
33    static { System.loadLibrary("soundpool"); }
34
35    private final static String TAG = "SoundPool";
36
37    private int mNativeContext; // accessed by native methods
38
39    public SoundPool(int maxStreams, int streamType, int srcQuality) {
40        native_setup(new WeakReference<SoundPool>(this), maxStreams, streamType, srcQuality);
41    }
42
43    public int load(String path, int priority)
44    {
45        // pass network streams to player
46        if (path.startsWith("http:"))
47            return _load(path, priority);
48
49        // try local path
50        int id = 0;
51        try {
52            File f = new File(path);
53            if (f != null) {
54                ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
55                if (fd != null) {
56                    id = _load(fd.getFileDescriptor(), 0, f.length(), priority);
57                    //Log.v(TAG, "close fd");
58                    fd.close();
59                }
60            }
61        } catch (java.io.IOException e) {}
62        return id;
63    }
64
65    public int load(Context context, int resId, int priority) {
66        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
67        int id = 0;
68        if (afd != null) {
69            id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority);
70            try {
71                //Log.v(TAG, "close fd");
72                afd.close();
73            } catch (java.io.IOException ex) {
74                //Log.d(TAG, "close failed:", ex);
75            }
76        }
77        return id;
78    }
79
80    private native final int _load(String uri, int priority);
81
82    private native final int _load(FileDescriptor fd, long offset, long length, int priority);
83
84    public native final boolean unload(int soundID);
85
86    public native final int play(int soundID, float leftVolume, float rightVolume,
87            int priority, int loop, float rate);
88
89    public native final void pause(int streamID);
90
91    public native final void resume(int streamID);
92
93    public native final void stop(int streamID);
94
95    public native final void setVolume(int streamID,
96            float leftVolume, float rightVolume);
97
98    public native final void setPriority(int streamID, int priority);
99
100    public native final void setLoop(int streamID, int loop);
101
102    public native final void setRate(int streamID, float rate);
103
104    public native final void release();
105
106    private native final void native_setup(Object mediaplayer_this,
107            int maxStreams, int streamType, int srcQuality);
108
109    protected void finalize() { release(); }
110}
111
112