Android Question Audio Routing

Discussion in 'Android Questions' started by Martin Beukes, Jun 17, 2019.

  1. Martin Beukes

    Martin Beukes Member Licensed 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:

    Code:
    #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:

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

    Any help would be appreciated.

    Martin
     
  2. nobbi59

    nobbi59 Active Member Licensed User

    Try it like this:

    Code:
    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 = NullThen
            
    Return ret
        
    End If
        
    Return False
    End Sub
     
  3. Martin Beukes

    Martin Beukes Member Licensed 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.
     
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