Android Question Audiofocus

Discussion in 'Android Questions' started by Wal, Apr 28, 2015.

  1. Wal

    Wal Member Licensed User

    Can I tell if the app lost the Audiofocus ?
     
  2. Wal

    Wal Member Licensed User

    I found this
    http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html

    Code:
    OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
        
    public void onAudioFocusChange(int focusChange) {
            
    if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
                // Pause playback
            
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Resume playback
            } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
                am.abandonAudioFocus(afChangeListener);
                // Stop playback
            }
        }
    };
    Code:
    OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
        
    public void onAudioFocusChange(int focusChange) {
            
    if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                // Lower the volume
            
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Raise it back to normal
            }
        }
    };
    Can I translate that into B4A ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Try this code:
    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Dim jo As JavaObject
       jo.InitializeContext
       jo = jo.RunMethod(
    "getSystemService"Array("audio"))
       
    Dim event As Object = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", _
         
    "audio"Null)
       
    Dim p As Phone
       
    Log(jo.RunMethod("requestAudioFocus"Array (event, p.VOLUME_MUSIC, 2)))
    End Sub


    Sub audio_Event (MethodName As String, Args() As Object) As Object
       
    Log(MethodName)
       
    Return Null
    End Sub
    MethodName should be onAudioFocusChange when the relevant event fires.
     
    Wal likes this.
  4. Wal

    Wal Member Licensed User

    The Code work's great, but in a service in debug mode not.

    Code:
    Sub Service_Start (StartingIntent As Intent)
        
    Dim jo As JavaObject
        jo.InitializeContext   
    ' -->>>"java.lang.NoSuchFieldException: context"
           jo = jo.RunMethod("getSystemService"Array("audio"))
           
    Dim event As Object = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", _
         
    "audio"Null)
           
    Dim p As Phone
           jo.RunMethod(
    "requestAudioFocus"Array (event, p.VOLUME_MUSIC, 2))
        frequenz = 
    7
        Volume = 
    10
        OpenRadio
    End Sub
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full error message from the logs? What is your JavaObject library version?
     
  6. Wal

    Wal Member Licensed User

    Thank Erel,
    for your feedback.
    Debug(legacy) - works
    Release(obfuscated) - works
    Release - works
    Debug(rapid) - don't work

    Core 4.01
    JavaObject 2.01
    Reflection 2.40

    Code:
    Sub Service_Start (StartingIntent As Intent)
        
    Dim jo As JavaObject
        jo.InitializeContext  
    ' --->>> line: 25  (serial)
           jo = jo.RunMethod("getSystemService"Array("audio"))
           
    Dim event As Object = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", _
         
    "audio"Null)
           
    Dim p As Phone
           jo.RunMethod(
    "requestAudioFocus"Array (event, p.VOLUME_MUSIC, 2))
        OpenRadio
    End Sub
    Code:
    Installing file.
    PackageAdded: package:b4a.example1
    ** 
    Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    ** 
    Service (serial) Create **
    ** 
    Service (serial) Start **
    Error occurred on line: 
    25 (serial)
    java.lang.NoSuchFieldException: context
        at java.lang.Class.getDeclaredField(Class.java:
    890)
        at anywheresoftware.b4j.object.JavaObject.InitializeContext(
    JavaObject.java:64)
        at b4a.example1.serial._service_start(
    serial.java:1209)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    372)
        at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:636)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:302)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:238)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    372)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    121)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    171)
        at b4a.example1.serial.handleStart(
    serial.java:71)
        at b4a.example1.serial.onStartCommand(
    serial.java:55)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:
    2894)
        at android.app.ActivityThread.access$
    2100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:
    1401)
        at android.os.Handler.dispatchMessage(Handler.java:
    102)
        at android.os.Looper.loop(Looper.java:
    135)
        at android.app.ActivityThread.main(ActivityThread.java:
    5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    698)
    ** 
    Service (serial) Start **
    java.lang.RuntimeException: Unable 
    to start service b4a.example1.serial@352afceb with Intent { cmp=b4a.example1/.serial }: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.reflect.Method.getName()' on a null object reference
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
        at android.app.ActivityThread.access$2100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.reflect.Method.getName()' on a null object reference
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:148)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
        at b4a.example1.serial.handleStart(serial.java:71)
        at b4a.example1.serial.onStartCommand(serial.java:55)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
        ... 9 more
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.reflect.Method.getName()' on a null object reference
        at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:406)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:248)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
        ... 13 more
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Try it with the attached jar file (v2.02). Make sure to copy it to the internal libraries folder.
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice