AudioSystem.java revision 843ef36f7b96cc19ea7d2996b7c8661b41ec3452
1/*
2 * Copyright (C) 2006 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
19
20/* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
21 * TO UPDATE THE CORRESPONDING NATIVE GLUE.  THANK YOU FOR YOUR COOPERATION
22 */
23
24/**
25 * @hide
26 */
27public class AudioSystem
28{
29    /* FIXME: Need to finalize this and correlate with native layer */
30    /*
31     * If these are modified, please also update Settings.System.VOLUME_SETTINGS
32     * and attrs.xml
33     */
34    /* The audio stream for phone calls */
35    public static final int STREAM_VOICE_CALL = 0;
36    /* The audio stream for system sounds */
37    public static final int STREAM_SYSTEM = 1;
38    /* The audio stream for the phone ring and message alerts */
39    public static final int STREAM_RING = 2;
40    /* The audio stream for music playback */
41    public static final int STREAM_MUSIC = 3;
42    /* The audio stream for alarms */
43    public static final int STREAM_ALARM = 4;
44    /* The audio stream for notifications */
45    public static final int STREAM_NOTIFICATION = 5;
46    /* @hide The audio stream for phone calls when connected on bluetooth */
47    public static final int STREAM_BLUETOOTH_SCO = 6;
48    /**
49     * @deprecated Use {@link #numStreamTypes() instead}
50     */
51    public static final int NUM_STREAMS = 5;
52
53    // Expose only the getter method publicly so we can change it in the future
54    private static final int NUM_STREAM_TYPES = 7;
55    public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; }
56
57    /* max and min volume levels */
58    /* Maximum volume setting, for use with setVolume(int,int) */
59    public static final int MAX_VOLUME = 100;
60    /* Minimum volume setting, for use with setVolume(int,int) */
61    public static final int MIN_VOLUME = 0;
62
63    /*
64     * Sets the volume of a specified audio stream.
65     *
66     * param type   the stream type to set the volume of (e.g. STREAM_MUSIC)
67     * param volume the volume level to set (0-100)
68     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
69     */
70    public static native int setVolume(int type, int volume);
71
72    /*
73     * Returns the volume of a specified audio stream.
74     *
75     * param type the stream type to get the volume of (e.g. STREAM_MUSIC)
76     * return the current volume (0-100)
77     */
78    public static native int getVolume(int type);
79
80    /*
81     * Sets the microphone mute on or off.
82     *
83     * param on set <var>true</var> to mute the microphone;
84     *           <var>false</var> to turn mute off
85     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
86     */
87    public static native int muteMicrophone(boolean on);
88
89    /*
90     * Checks whether the microphone mute is on or off.
91     *
92     * return true if microphone is muted, false if it's not
93     */
94    public static native boolean isMicrophoneMuted();
95
96    /*
97     * Sets the audio mode.
98     *
99     * param mode  the requested audio mode (NORMAL, RINGTONE, or IN_CALL).
100     *              Informs the HAL about the current audio state so that
101     *              it can route the audio appropriately.
102     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
103     */
104    public static native int setMode(int mode);
105
106    /*
107     * Returns the current audio mode.
108     *
109     * return      the current audio mode (NORMAL, RINGTONE, or IN_CALL).
110     *              Returns the current current audio state from the HAL.
111     */
112    public static native int getMode();
113
114    /* modes for setMode/getMode/setRoute/getRoute */
115    public static final int MODE_INVALID            = -2;
116    public static final int MODE_CURRENT            = -1;
117    public static final int MODE_NORMAL             = 0;
118    public static final int MODE_RINGTONE           = 1;
119    public static final int MODE_IN_CALL            = 2;
120    public static final int NUM_MODES               = 3;
121
122
123    /* Routing bits for setRouting/getRouting API */
124    public static final int ROUTE_EARPIECE          = (1 << 0);
125    public static final int ROUTE_SPEAKER           = (1 << 1);
126
127    /** @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
128    @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
129    public static final int ROUTE_BLUETOOTH_SCO     = (1 << 2);
130    public static final int ROUTE_HEADSET           = (1 << 3);
131    public static final int ROUTE_BLUETOOTH_A2DP    = (1 << 4);
132    public static final int ROUTE_ALL               = 0xFFFFFFFF;
133
134    /*
135     * Sets the audio routing for a specified mode
136     *
137     * param mode   audio mode to change route. E.g., MODE_RINGTONE.
138     * param routes bit vector of routes requested, created from one or
139     *               more of ROUTE_xxx types. Set bits indicate that route should be on
140     * param mask   bit vector of routes to change, created from one or more of
141     * ROUTE_xxx types. Unset bits indicate the route should be left unchanged
142     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
143     */
144    public static native int setRouting(int mode, int routes, int mask);
145
146    /*
147     * Returns the current audio routing bit vector for a specified mode.
148     *
149     * param mode audio mode to change route (e.g., MODE_RINGTONE)
150     * return an audio route bit vector that can be compared with ROUTE_xxx
151     * bits
152     */
153    public static native int getRouting(int mode);
154
155    /*
156     * Checks whether any music is active.
157     *
158     * return true if any music tracks are active.
159     */
160    public static native boolean isMusicActive();
161
162    /*
163     * Sets a generic audio configuration parameter. The use of these parameters
164     * are platform dependant, see libaudio
165     *
166     * ** Temporary interface - DO NOT USE
167     *
168     * TODO: Replace with a more generic key:value get/set mechanism
169     *
170     * param key   name of parameter to set. Must not be null.
171     * param value value of parameter. Must not be null.
172     */
173    public static native void setParameter(String key, String value);
174
175    /*
176    private final static String TAG = "audio";
177
178    private void log(String msg) {
179        Log.d(TAG, "[AudioSystem] " + msg);
180    }
181    */
182
183    // These match the enum in libs/android_runtime/android_media_AudioSystem.cpp
184    /* Command sucessful or Media server restarted. see ErrorCallback */
185    public static final int AUDIO_STATUS_OK = 0;
186    /* Command failed or unspecified audio error.  see ErrorCallback */
187    public static final int AUDIO_STATUS_ERROR = 1;
188    /* Media server died. see ErrorCallback */
189    public static final int AUDIO_STATUS_SERVER_DIED = 100;
190
191    private static ErrorCallback mErrorCallback;
192
193    /*
194     * Handles the audio error callback.
195     */
196    public interface ErrorCallback
197    {
198        /*
199         * Callback for audio server errors.
200         * param error   error code:
201         * - AUDIO_STATUS_OK
202         * - AUDIO_STATUS_SERVER_DIED
203         * - AUDIO_STATUS_ERROR
204         */
205        void onError(int error);
206    };
207
208    /*
209     * Registers a callback to be invoked when an error occurs.
210     * param cb the callback to run
211     */
212    public static void setErrorCallback(ErrorCallback cb)
213    {
214        mErrorCallback = cb;
215    }
216
217    private static void errorCallbackFromNative(int error)
218    {
219        if (mErrorCallback != null) {
220            mErrorCallback.onError(error);
221        }
222    }
223}
224