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