Android Question SendKeyCode Issue

cooperlegend

Active Member
Licensed User
Longtime User
I have a strange issue when using SendKeyCode to Pause and Play Spotify.

My code
Code:
Si.SendKeyCode(KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE)

This works but ONLY when in debug mode and I pause on this line of code using the debugger and then continue.

If I run this line of code normally it does not work for me.

Any ideas before I pull out the last few hairs from my head?
 
It is almost as if the App needs focus for the call to work, very strange.

It works fine from a button (as in the demo code) but I need this to pause and play automatically whilst another announcement is made.

here is more of my code

Code:
Sub PlaySpotify
    Dim SpotIsPlaying1 As Boolean     'ditto
    Dim R As Reflector

    R.Target = R.GetContext
    R.Target = R.RunMethod2("getSystemService","audio","java.lang.String")
    SpotIsPlaying1 = R.RunMethod("isMusicActive")
    Log("Start Spotify : playing=" & SpotIsPlaying1)

    If SpotIsPlaying1 == True Then Return
    
    If bSpotify Then
        Si.SendKeyCode(KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE)
        Sleep(0)
    End If
End Sub

Sub StopSpotify
    Dim SpotIsPlaying1 As Boolean     'ditto
    Dim R As Reflector

    R.Target = R.GetContext
    R.Target = R.RunMethod2("getSystemService","audio","java.lang.String")
    SpotIsPlaying1 = R.RunMethod("isMusicActive")
    Log("Stop Spotify : playing=" & SpotIsPlaying1)

    If SpotIsPlaying1 == False Then Return
    
    If bSpotify Then
        Si.SendKeyCode(KeyCodes.KEYCODE_MEDIA_STOP)
        Sleep(0)
    End If
End Sub

Sub RunSpotify
    Dim i As Intent

    i.Initialize(i.ACTION_VIEW, "spotify:track:0")
    i.SetComponent("com.spotify.mobile.android.ui")

    StartActivity(i)
End Sub
 
Upvote 0
Back
Top