11package extension .androidtools .media ;
22
33import extension .androidtools .jni .JNICache ;
4- import cpp .RawPointer ;
54import lime .app .Event ;
65import lime .system .JNI ;
76
@@ -188,10 +187,7 @@ class AudioManager
188187 public static final AUDIOFOCUS_REQUEST_DELAYED : Int = 2 ;
189188
190189 @:noCompletion
191- private var constructor : Dynamic ;
192-
193- @:noCompletion
194- private var focusListener : Dynamic ;
190+ private var constructor : Null <Dynamic >;
195191
196192 private var onFocusChangeListener : OnAudioFocusChangeListener ;
197193
@@ -200,26 +196,48 @@ class AudioManager
200196 */
201197 public function new (): Void
202198 {
203- constructor = JNICache .createStaticMethod (' org/haxe/extension/Tools' , ' getAudioManager' , ' ()Landroid/media/AudioManager;' )();
204-
205- focusListener = JNICache .createStaticMethod (' org/haxe/extension/Tools' , ' createAudioFocusCallback' ,
206- " (Lorg/haxe/lime/HaxeObject;)Ljava/lang/Object;" )(onFocusChangeListener = new OnAudioFocusChangeListener ());
199+ final constructorJNI : Null <Dynamic > = JNICache .createStaticMethod (' org/haxe/extension/Tools' , ' getAudioManager' , ' ()Landroid/media/AudioManager;' );
200+
201+ if (constructorJNI != null )
202+ {
203+ constructor = constructorJNI ();
204+ }
205+
206+ onFocusChangeListener = new OnAudioFocusChangeListener ();
207207 }
208208
209209 /**
210210 * Adjust the volume of a stream.
211211 */
212212 public function adjustStreamVolume (streamType : Int , direction : Int , flags : Int ): Void
213213 {
214- JNI .callMember (JNICache .createMemberMethod (' android/media/AudioManager' , ' adjustStreamVolume' , ' (III)V' ), constructor , [streamType , direction , flags ]);
214+ if (constructor == null )
215+ return ;
216+
217+ final adjustStreamVolumeJNI : Null <Dynamic > = JNICache .createMemberMethod (' android/media/AudioManager' , ' adjustStreamVolume' , ' (III)V' );
218+
219+ if (adjustStreamVolumeJNI != null )
220+ JNI .callMember (adjustStreamVolumeJNI , constructor , [streamType , direction , flags ]);
215221 }
216222
217223 /**
218224 * Get the volume of a stream.
219225 */
220226 public function getStreamVolume (streamType : Int ): Int
221227 {
222- return JNI .callMember (JNICache .createMemberMethod (' android/media/AudioManager' , ' getStreamVolume' , ' (I)I' ), constructor , [streamType ]);
228+ if (constructor == null )
229+ return 0 ;
230+
231+ final getStreamVolumeJNI : Null <Dynamic > = JNICache .createMemberMethod (' android/media/AudioManager' , ' getStreamVolume' , ' (I)I' );
232+
233+ if (getStreamVolumeJNI != null )
234+ {
235+ return JNI .callMember (getStreamVolumeJNI , constructor , [streamType ]);
236+ }
237+ else
238+ {
239+ return 0 ;
240+ }
223241 }
224242
225243 /**
@@ -235,21 +253,45 @@ class AudioManager
235253 */
236254 public function requestAudioFocus (? callback : Int -> Void , streamType : Int , durationHint : Int ): Int
237255 {
256+ if (constructor == null && onFocusChangeListener .focusListenerJNI == null )
257+ return 0 ;
258+
238259 if (callback != null )
239260 onFocusChangeListener .addCallback (callback );
240261
241- return JNI .callMember (JNICache .createMemberMethod (' android/media/AudioManager' , ' requestAudioFocus' , " (Landroid/media/AudioManager$OnAudioFocusChangeListener;II)I" ), constructor , [focusListener , streamType , durationHint ]);
262+ final requestAudioFocusJNI : Null <Dynamic > = JNICache .createMemberMethod (' android/media/AudioManager' , ' requestAudioFocus' , " (Landroid/media/AudioManager$OnAudioFocusChangeListener;II)I" );
263+
264+ if (requestAudioFocusJNI != null )
265+ {
266+ return JNI .callMember (requestAudioFocusJNI , constructor , [onFocusChangeListener .focusListenerJNI , streamType , durationHint ]);
267+ }
268+ else
269+ {
270+ return 0 ;
271+ }
242272 }
243273
244274 /**
245275 * Abandon audio focus.
246276 */
247277 public function abandonAudioFocus (? callback : Int -> Void ): Int
248278 {
279+ if (constructor == null && onFocusChangeListener .focusListenerJNI == null )
280+ return 0 ;
281+
249282 if (callback != null )
250283 onFocusChangeListener .addCallback (callback );
251284
252- return JNI .callMember (JNICache .createMemberMethod (' android/media/AudioManager' , ' abandonAudioFocus' , " (Landroid/media/AudioManager$OnAudioFocusChangeListener;)I" ), constructor , [focusListener ]);
285+ final abandonAudioFocusJNI : Null <Dynamic > = JNICache .createMemberMethod (' android/media/AudioManager' , ' abandonAudioFocus' , " (Landroid/media/AudioManager$OnAudioFocusChangeListener;)I" );
286+
287+ if (abandonAudioFocusJNI != null )
288+ {
289+ return JNI .callMember (JNICache .createMemberMethod (' android/media/AudioManager' , ' abandonAudioFocus' , " (Landroid/media/AudioManager$OnAudioFocusChangeListener;)I" ), constructor , [onFocusChangeListener .focusListenerJNI ]);
290+ }
291+ else
292+ {
293+ return 0 ;
294+ }
253295 }
254296}
255297
@@ -261,6 +303,9 @@ private class OnAudioFocusChangeListener #if (lime >= "8.0.0") implements JNISaf
261303{
262304 private var onFocusChangeEvent : Event <Int -> Void > = new Event <Int -> Void >();
263305
306+ @:noCompletion
307+ public var focusListenerJNI (default , null ): Null <Dynamic >;
308+
264309 /**
265310 * Creates a new audio focus listener with a specified callback function.
266311 *
@@ -270,6 +315,14 @@ private class OnAudioFocusChangeListener #if (lime >= "8.0.0") implements JNISaf
270315 {
271316 if (focusChange != null )
272317 onFocusChangeEvent .add (focusChange );
318+
319+ final createAudioFocusCallbackJNI : Null <Dynamic > = JNICache .createStaticMethod (' org/haxe/extension/Tools' , ' createAudioFocusCallback' ,
320+ " (Lorg/haxe/lime/HaxeObject;)Ljava/lang/Object;" );
321+
322+ if (createAudioFocusCallbackJNI != null )
323+ {
324+ focusListenerJNI = createAudioFocusCallbackJNI (this );
325+ }
273326 }
274327
275328 public function addCallback (focusChange : Int -> Void )
0 commit comments