Android Question Audio Routing

Martin Beukes

Member
Licensed User
Longtime User
Greetings Friends,

I have a weird problem. I have a Pine64 running android. When you reboot, it doesn't detect that the audio jack is plugged in. I want to be able to set it to on in order to test. Bellow is my test app code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim myMusic As MediaPlayer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim l As Label
    l.Initialize("l")
    l.Color = Colors.Black
    l.Textcolor    = Colors.White
    l.TextSize = Activity.Height/10
    Activity.AddView(l,0,0,Activity.Width,Activity.Height)
    
    l.Text = "wired: " & isWiredHeadsetOn & _
    CRLF & _
    "Speaker: " & isSpeakerphoneOn
    
    If FirstTime Then
        myMusic.Initialize2("myMusic")
    End If
    
    playaudio
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub isWiredHeadsetOn As Boolean
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Return r.RunMethod("isWiredHeadsetOn")
End Sub

Sub setWiredHeadsetOn As Boolean
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Return r.RunMethod2("setWiredHeadsetOn", True, "java.lang.boolean")
End Sub

Sub isSpeakerphoneOn As Boolean
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Return r.RunMethod("isSpeakerphoneOn")
End Sub

Sub playaudio
    ToastMessageShow("Play audio", True)
    myMusic.Load(File.DirAssets, "boom.mp3")
    myMusic.Play
End Sub

Sub myMusic_Complete
    ToastMessageShow("Retry", True)
    setWiredHeadsetOn
    myMusic.Play
End Sub

I get this error:

java.lang.RuntimeException: Cannot parse: null as boolean

This is coming from the "setWiredHeadsetOn" line. I am lost

Any help would be appreciated.

Martin
 

nobbi59

Active Member
Licensed User
Longtime User
Try it like this:

B4X:
Sub setWiredHeadsetOn As Boolean
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Dim ret As Object = r.RunMethod2("setWiredHeadsetOn", True, "java.lang.boolean")
    If Not(ret = Null) Then
        Return ret
    End If
    Return False
End Sub
 
Upvote 0

Martin Beukes

Member
Licensed User
Longtime User
Thanks for that. I helped to see that the deprecated function will not work. I have someone wrapping the Audio Manager atm. Once I have working code I will post it back here for future reference.
 
Upvote 0
Top