|
| 1 | +package extension.androidtools.media; |
| 2 | + |
| 3 | +import haxe.macro.Tools.TTypedExprTools; |
| 4 | +#if (!android && !native) |
| 5 | +#error 'extension-androidtools is not supported on your current platform' |
| 6 | +#end |
| 7 | +import extension.androidtools.jni.JNICache; |
| 8 | +import lime.app.Event; |
| 9 | +import lime.system.JNI; |
| 10 | + |
| 11 | +/** |
| 12 | + * Utility class for managing volume and ringer mode control. |
| 13 | + */ |
| 14 | +class AudioManager |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Constant representing the stream type that can be used for changing volume of that stream: voice call. |
| 18 | + */ |
| 19 | + public static final STREAM_VOICE_CALL:Int = 0; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constant representing the stream type that can be used for changing volume of that stream: system sounds. |
| 23 | + */ |
| 24 | + public static final STREAM_SYSTEM:Int = 1; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constant representing the stream type that can be used for changing volume of that stream: phone ring. |
| 28 | + */ |
| 29 | + public static final STREAM_RING:Int = 2; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constant representing the stream type that can be used for changing volume of that stream: music playback. |
| 33 | + */ |
| 34 | + public static final STREAM_MUSIC:Int = 3; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constant representing the stream type that can be used for changing volume of that stream: alarm. |
| 38 | + */ |
| 39 | + public static final STREAM_ALARM:Int = 4; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constant representing the stream type that can be used for changing volume of that stream: notification. |
| 43 | + */ |
| 44 | + public static final STREAM_NOTIFICATION:Int = 5; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constant representing the stream type that can be used for changing volume of that stream: phone calls when connected to bluetooth. |
| 48 | + */ |
| 49 | + public static final STREAM_BLUETOOTH_SCO:Int = 6; |
| 50 | + |
| 51 | + /** |
| 52 | + * Constant representing the stream type that can be used for changing volume of that stream: enforced system sounds. |
| 53 | + */ |
| 54 | + public static final STREAM_SYSTEM_ENFORCED:Int = 7; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constant representing the stream type that can be used for changing volume of that stream: DTMF Tones. |
| 58 | + */ |
| 59 | + public static final STREAM_DTMF:Int = 8; |
| 60 | + |
| 61 | + /** |
| 62 | + * Constant representing the stream type that can be used for changing volume of that stream: tts transmitted through the speaker. |
| 63 | + */ |
| 64 | + public static final STREAM_TTS:Int = 9; |
| 65 | + |
| 66 | + /** |
| 67 | + * Constant representing the stream type that can be used for changing volume of that stream: accessibility prompts. |
| 68 | + */ |
| 69 | + public static final STREAM_ACCESSIBILITY:Int = 10; |
| 70 | + |
| 71 | + /** |
| 72 | + * Constant representing the stream type that can be used for changing volume of that stream: virtual assistant. |
| 73 | + */ |
| 74 | + public static final STREAM_ASSISTANT:Int = 11; |
| 75 | + |
| 76 | + /** |
| 77 | + * Constant representing the direction of volume adjustment: raise the volume. |
| 78 | + */ |
| 79 | + public static final ADJUST_RAISE:Int = 1; |
| 80 | + |
| 81 | + /** |
| 82 | + * Constant representing the direction of volume adjustment: lower the volume. |
| 83 | + */ |
| 84 | + public static final ADJUST_LOWER:Int = -1; |
| 85 | + |
| 86 | + /** |
| 87 | + * Constant representing the direction of volume adjustment: same volume. |
| 88 | + */ |
| 89 | + public static final ADJUST_SAME:Int = 0; |
| 90 | + |
| 91 | + /** |
| 92 | + * Constant representing the direction of volume adjustment: mute the volume. |
| 93 | + */ |
| 94 | + public static final ADJUST_MUTE:Int = -100; |
| 95 | + |
| 96 | + /** |
| 97 | + * Constant representing the direction of volume adjustment: unmute the volume. |
| 98 | + */ |
| 99 | + public static final ADJUST_UNMUTE:Int = 100; |
| 100 | + |
| 101 | + /** |
| 102 | + * Constant representing the direction of volume adjustment: toggle mute state. |
| 103 | + */ |
| 104 | + public static final ADJUST_TOGGLE_MUTE:Int = 101; |
| 105 | + |
| 106 | + /** |
| 107 | + * Constant representing the flags for volume adjustment: show ui. |
| 108 | + */ |
| 109 | + public static final FLAG_SHOW_UI:Int = 1 << 0; |
| 110 | + |
| 111 | + /** |
| 112 | + * Constant representing the flags for volume adjustment: include ringer modes. |
| 113 | + */ |
| 114 | + public static final FLAG_ALLOW_RINGER_MODES:Int = 1 << 1; |
| 115 | + |
| 116 | + /** |
| 117 | + * Constant representing the flags for volume adjustment: play sound when changing volume. |
| 118 | + */ |
| 119 | + public static final FLAG_PLAY_SOUND:Int = 1 << 2; |
| 120 | + |
| 121 | + /** |
| 122 | + * Constant representing the flags for volume adjustment: remove any sound and vibrate. |
| 123 | + */ |
| 124 | + public static final FLAG_REMOVE_SOUND_AND_VIBRATE:Int = 1 << 3; |
| 125 | + |
| 126 | + /** |
| 127 | + * Constant representing the flags for volume adjustment: vibrate if going into the vibrate ringer mode. |
| 128 | + */ |
| 129 | + public static final FLAG_VIBRATE:Int = 1 << 4; |
| 130 | + |
| 131 | + /** |
| 132 | + * Constant representing the flags for audio focus status: nothing changed. |
| 133 | + */ |
| 134 | + public static final AUDIOFOCUS_NONE:Int = 0; |
| 135 | + |
| 136 | + /** |
| 137 | + * Constant representing the flags for audio focus status: gain of audio focus. |
| 138 | + */ |
| 139 | + public static final AUDIOFOCUS_GAIN:Int = 1; |
| 140 | + |
| 141 | + /** |
| 142 | + * Constant representing the flags for audio focus status: gain of temporary audio focus. |
| 143 | + */ |
| 144 | + public static final AUDIOFOCUS_GAIN_TRANSIENT:Int = 2; |
| 145 | + |
| 146 | + /** |
| 147 | + * Constant representing the flags for audio focus status: gain of temporary audio focus with ducking another app. |
| 148 | + */ |
| 149 | + public static final AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:Int = 3; |
| 150 | + |
| 151 | + /** |
| 152 | + * Constant representing the flags for audio focus status: gain of exclusive temporary audio focus. |
| 153 | + */ |
| 154 | + public static final AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:Int = 4; |
| 155 | + |
| 156 | + /** |
| 157 | + * Constant representing the flags for audio focus status: loss of audio focus. |
| 158 | + */ |
| 159 | + public static final AUDIOFOCUS_LOSS:Int = -1 * AUDIOFOCUS_GAIN; |
| 160 | + |
| 161 | + /** |
| 162 | + * Constant representing the flags for audio focus status: loss of temporary audio focus. |
| 163 | + */ |
| 164 | + public static final AUDIOFOCUS_LOSS_TRANSIENT:Int = -1 * AUDIOFOCUS_GAIN_TRANSIENT; |
| 165 | + |
| 166 | + /** |
| 167 | + * Constant representing the flags for audio focus status: loss of temporary audio focus with ducking another app. |
| 168 | + */ |
| 169 | + public static final AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:Int = -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK; |
| 170 | + |
| 171 | + /** |
| 172 | + * Constant representing the flags for audio focus change result: unable to change focus. |
| 173 | + */ |
| 174 | + public static final AUDIOFOCUS_REQUEST_FAILED:Int = 0; |
| 175 | + |
| 176 | + /** |
| 177 | + * Constant representing the flags for audio focus change result: focus was changed successfully. |
| 178 | + */ |
| 179 | + public static final AUDIOFOCUS_REQUEST_GRANTED:Int = 1; |
| 180 | + |
| 181 | + /** |
| 182 | + * Constant representing the flags for audio focus change result: focus was changed successfully but delayed. |
| 183 | + */ |
| 184 | + public static final AUDIOFOCUS_REQUEST_DELAYED:Int = 2; |
| 185 | + |
| 186 | + /** |
| 187 | + * Event that is triggered when the audio focus changes. |
| 188 | + * |
| 189 | + * Listeners can subscribe to this event to be notified when the audio focus state changes. |
| 190 | + * |
| 191 | + * The event handler receives a dynamic parameter containing information about the focus change. |
| 192 | + */ |
| 193 | + public static var onFocusChangeEvent(default, null):Event<Int->Void> = new Event<Int->Void>(); |
| 194 | + |
| 195 | + /** |
| 196 | + * Adjusts the volume of a specified audio stream. |
| 197 | + * |
| 198 | + * @param streamType The type of audio stream to adjust (e.g., STREAM_MUSIC). |
| 199 | + * @param direction The direction to adjust the volume (e.g., ADJUST_RAISE, ADJUST_LOWER). |
| 200 | + * @param flags Additional operation flags (e.g., FLAG_SHOW_UI). |
| 201 | + */ |
| 202 | + public static function adjustStreamVolume(streamType:Int, direction:Int, flags:Int):Void |
| 203 | + { |
| 204 | + final adjustStreamVolumeJNI:Null<Dynamic> = JNICache.createStaticMethod('org/haxe/extension/Tools', 'adjustStreamVolume', '(III)V'); |
| 205 | + |
| 206 | + if (adjustStreamVolumeJNI != null) |
| 207 | + adjustStreamVolumeJNI(streamType, direction, flags); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Retrieves the current volume index for a specified audio stream. |
| 212 | + * |
| 213 | + * @param streamType The type of audio stream (e.g., STREAM_MUSIC). |
| 214 | + * @return The current volume index for the specified stream, or 0 if an error occurs. |
| 215 | + */ |
| 216 | + public static function getStreamVolume(streamType:Int):Int |
| 217 | + { |
| 218 | + final getStreamVolumeJNI:Null<Dynamic> = JNICache.createStaticMethod('org/haxe/extension/Tools', 'getStreamVolume', '(I)I'); |
| 219 | + |
| 220 | + if (getStreamVolumeJNI != null) |
| 221 | + return getStreamVolumeJNI(); |
| 222 | + |
| 223 | + return 0; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Requests audio focus for a given stream and duration, and sets up a callback for focus changes. |
| 228 | + * |
| 229 | + * @param streamType The type of audio stream for which focus is requested. |
| 230 | + * @param durationHint The duration of the audio focus request (e.g., AUDIOFOCUS_GAIN). |
| 231 | + * @return The result of the audio focus request (e.g., AUDIOFOCUS_REQUEST_GRANTED or AUDIOFOCUS_REQUEST_FAILED). |
| 232 | + */ |
| 233 | + public static function requestAudioFocus(?focusChange:Int->Void, streamType:Int, durationHint:Int):Int |
| 234 | + { |
| 235 | + final requestAudioFocusJNI:Null<Dynamic> = JNICache.createStaticMethod('org/haxe/extension/Tools', 'requestAudioFocus', |
| 236 | + '(Lorg/haxe/lime/HaxeObject;II)I'); |
| 237 | + |
| 238 | + if (requestAudioFocusJNI != null) |
| 239 | + return requestAudioFocusJNI(new OnAudioFocusChangeListener(), streamType, durationHint); |
| 240 | + |
| 241 | + return AUDIOFOCUS_REQUEST_FAILED; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Abandons audio focus for the given callback. |
| 246 | + * |
| 247 | + * @return The result of the abandon audio focus request (e.g., AUDIOFOCUS_REQUEST_GRANTED or AUDIOFOCUS_REQUEST_FAILED). |
| 248 | + */ |
| 249 | + public static function abandonAudioFocus():Int |
| 250 | + { |
| 251 | + final abandonAudioFocusJNI:Null<Dynamic> = JNICache.createStaticMethod('org/haxe/extension/Tools', 'abandonAudioFocus', |
| 252 | + '(Lorg/haxe/lime/HaxeObject;)I'); |
| 253 | + |
| 254 | + if (abandonAudioFocusJNI != null) |
| 255 | + return abandonAudioFocusJNI(new OnAudioFocusChangeListener()); |
| 256 | + |
| 257 | + return AUDIOFOCUS_REQUEST_FAILED; |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +/** |
| 262 | + * Listener class for handling audio focus changes. |
| 263 | + */ |
| 264 | +@:noCompletion |
| 265 | +private class OnAudioFocusChangeListener #if (lime >= "8.0.0") implements JNISafety #end |
| 266 | +{ |
| 267 | + public function new():Void {} |
| 268 | + |
| 269 | + @:keep |
| 270 | + #if (lime >= "8.0.0") |
| 271 | + @:runOnMainThread |
| 272 | + #end |
| 273 | + public function onAudioFocusChange(focusChange:Int):Void |
| 274 | + { |
| 275 | + if (AudioManager.onFocusChangeEvent != null) |
| 276 | + AudioManager.onFocusChangeEvent.dispatch(focusChange); |
| 277 | + } |
| 278 | +} |
0 commit comments