Android Question how to detect phone state by using PhoneStateChanged

Dear specialists, I want to detect if phone state has changed to The ringing state. If this event will occur, I want to release Exoplayer so this library will stop live stream playback. Please, how to write a short code which will react on The PhoneStateChanged on The state ringing. Because if The PhoneStateChanged status is RINGING, it means, that somebody is calling on my number. So I want to stop The Internet radio playback.
Thank you for The code. Pause, release functions work even on Android 4.1, so Exoplayer library is compatible even with so old version. There is An deep problem between click and playback. But on Android 6.0 and newer, everything work much more smoother.
 

epiCode

Active Member
Licensed User
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
You can also try:
Library can handle audio focus for you. Minimum tested SDK is 21, but maybe you can set it to lower number.
 
Upvote 0
this worked for me:

try it out
Very well done and thank you for your fast reply. I have also found may be that it was your code which work as A service. I must only construct my code so it will release The exoplayer but code can not open it again. So I will have to define variable which will be boolean according to The fact, if phone will ring or not.
 
Upvote 0

epiCode

Active Member
Licensed User
Very well done and thank you for your fast reply. I have also found may be that it was your code which work as A service. I must only construct my code so it will release The exoplayer but code can not open it again. So I will have to define variable which will be boolean according to The fact, if phone will ring or not.
I am presuming you mean something like this in your service:

B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    If State = lastState Then Return
   
    Select State
        Case "IDLE"
            If B4XPages.MainPage.playing Then 'I have playing as a boolean in mainpage set to public
                ' Code to resume ( you can call sub in your main code here too)
                Log("Resumed")
            End If
        Case "OFHOOK", "RINGING"
            If B4XPages.MainPage.playing Then  'I have playing as a boolean in mainpage set to public
                ' pause the playing
               Log("paused")
            End If
    End Select
    lastState = State
End Sub
 
Upvote 0
Top