Android Question requestAudioFocus sdk 26+

Yngveh58

Member
Licensed User
Longtime User
Hello
Has tried to create a version's secure handling of 'Audio focus' media has stuck.
Can anyone help ?.


B4X:
Sub requestFocus(request As Boolean) As Boolean
    Try
        jo.InitializeContext
        jo = jo.RunMethod("getSystemService", Array("audio"))
        event = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", "audio", Null)
                                    
        Dim p As Phone
    
        If p.SdkVersion <= 25  Then
            If request = True Then
                If jo.RunMethod("requestAudioFocus", Array (event, p.VOLUME_MUSIC, 2)) = 1 Then Return True
            Else
                jo.RunMethod("abandonAudioFocus", Array (event, p.VOLUME_MUSIC, 2))
            End If
        Else
            If request = True Then
                If jo.RunMethod("requestAudioFocus", Array (event, p.VOLUME_MUSIC, 2)) = 1 Then Return True
            Else
                jo.RunMethod("abandonAudioFocusRequest", Array(event, p.VOLUME_MUSIC, 2))
            End If
        End If
        Return False
    Catch
        Log("Sub requestFocus " & LastException)
        Return False
    End Try
    
End Sub


Sub audio_Event (MethodName As String, Args() As Object) As Object
    Log("Methodname = " & MethodName)
    
    If MethodName = "onAudioFocusChange" Then
        mpStop ' mp.stop ( MediaPlayerStream)
    End If
    
    Return Null
End Sub
 

Yngveh58

Member
Licensed User
Longtime User
Hello
With sdkversion 29 I get the following error:
Sub requestFocus (RuntimeException) java.lang.RuntimeException: Method: abandonAudioFocusRequest not matched.
 
Upvote 0

Yngveh58

Member
Licensed User
Longtime User
Excuse my flawed description.
I can't get 'abandonAudioFocusRequest' to work on sdk > = 26.
'requestAudioFocus' works.

'requestAudioFocus' and 'abandonAudioFocus' work on sdk <= 25.

error = Sub requestFocus (RuntimeException) java.lang.RuntimeException: Method: abandonAudioFocusRequest not matched.

B4X:
Sub Process_Globals
    Dim mp As MediaPlayerStream
    Dim jo As JavaObject
    Dim event As Object
End Sub


Sub requestFocus(request As Boolean) As Boolean
    Try
        jo.InitializeContext
        jo = jo.RunMethod("getSystemService", Array("audio"))
        event = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", "audio", Null)
                                                
        Dim p As Phone
    
        If p.SdkVersion <= 25  Then
            If request = True Then
                If jo.RunMethod("requestAudioFocus", Array (event, p.VOLUME_MUSIC, 2)) = 1 Then Return True
            Else
                If jo.RunMethod("abandonAudioFocus", Array(event)) = 1 Then Return True
            End If
        Else
            If request = True Then
                If jo.RunMethod("requestAudioFocus", Array (event, p.VOLUME_MUSIC, 2)) = 1 Then Return True
            Else
                If jo.RunMethod("abandonAudioFocusRequest", Array(event)) = 1 Then Return True
            End If
        End If
        Return False
    Catch
        Log("Sub requestFocus " & LastException)
        txt=LastException
        Return False
    End Try
    
End Sub
 
Upvote 0

Yngveh58

Member
Licensed User
Longtime User
Hello
Can you give an example how in B4A
Have found this:
B4X:
public boolean requestAudioFocus (OnAudioFocusChangeListener focusChangeListener,
                                 int streamType, int audioFocusGain) {
    int r;
    if (Build.VERSION.SDK_INT> = Build.VERSION_CODES.O) {
        r = mAudioManager.requestAudioFocus (
                new AudioFocusRequest.Builder (audioFocusGain)
                        .setAudioAttributes (
                                new AudioAttributes.Builder ()
                                        .setLegacyStreamType (stream type)
                                        .build ())
                        .setOnAudioFocusChangeListener (Chang focus listener)
                        .build ());
    } else {
        // noinspection deprecation
        r = mAudioManager.requestAudioFocus (focusChangeListener, streamType, audioFocusGain);
    }

    return r == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}

public PlayerService() {
    musicPlayer = new MusicPlayer();
    musicPlayer.addListener(new MusicPlayer.Listener() {
        @Override
        public void onCurrentTrackChanged() {
            updateScrobbling();
        }

        @Override
        public void onStateChanged() {
            sentStateBroadcast();
            if(musicPlayer.getState() != PlayerState.Preparing)
                updateNotification();
        }
    });
    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            handleAudioFocusChange(focusChange);
        }
    };
}

@override
public void onStateChanged () {
After the broadcast state ();
if (musicPlayer.getState ()! = PlayerState.Preparing)
updateNotification ();
}
});
audioFocusListener = new OnAudioFocusChangeListener () {
@override
public void onAudioFocusChange (int focusChange) {
handle audio focus change (focus change);
}
};
}
 
Upvote 0

Yngveh58

Member
Licensed User
Longtime User
Sorry for the wrong
B4X:
public void abandonAudioFocus(OnAudioFocusChangeListener focusChangeListener) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mAudioManager.abandonAudioFocusRequest(
                new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                        .setOnAudioFocusChangeListener(focusChangeListener)
                        .build());
    } else {
        //noinspection deprecation
        mAudioManager.abandonAudioFocus(focusChangeListener);
    }
}
Code
 
Upvote 0
Top