Android Question how to stop app from playing music when incoming calls ?

Ehsan Tamjidi

Member
Licensed User
Longtime User
hi there
I have created an app that plays music I need to stop music on incoming calls or outgoing calls

thanks for your answers
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Ehsan Tamjidi

Member
Licensed User
Longtime User
Hi Erel
Thanks a lot for the help

I used your Code Like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject
   jo.InitializeContext
   jo = jo.RunMethod("getSystemService", Array("audio"))
   Dim event As Object = jo.CreateEventFromUI("android.media.AudioManager.OnAudioFocusChangeListener", _
     "audio", Null)
   Dim p As Phone
   Log(jo.RunMethod("requestAudioFocus", Array (event, p.VOLUME_MUSIC, 2)))
End Sub

Sub audio_Event (MethodName As String, Args() As Object) As Object
   Log("Method: "&MethodName)

    If MethodName="onAudioFocusChange" Then
        Log("Args(0): "&Args(0))
        If Args(0)<0 Then
            Phone_Idle = False
            player1.Pause()

        Else
            Phone_Idle = True
            Reset_Radio(RadioChannel)
        End If
    End If
    Log("Phone Idle: "&Phone_Idle)
   Return Null
End Sub

but I receive an error:
Ignoring event: audio_event

I think it is because I dont play through service, and I dont know how to play in a service


thanks for your time - cheers
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I know this thread is a year old but I read it yesterday and looked at what I was doing in my Music App Service (which is working just fine)

I handle the PhoneEvents and if the phone goes off hook I set a flag and pause the music
When it goes back to Idle I restart the music if it was playing

NOW what I am doing is working fine.

But should I be doing what Erel suggested above?


B4X:
Public  Sub BBP_PhoneStatus_PhoneStateChanged(State As String, IncomingNumber As String, Intent As Intent)
   
           Log("BlackBoxPlayer::PhoneStateChanged  State:" &State &"  Incoming Number:" &IncomingNumber)
           
           '------------------------------------------------------------------------
           '  A phone call has come in or we are making a phone call
           '------------------------------------------------------------------------
           If  State.EqualsIgnoreCase("OFFHOOK") Then
               '--------------------------------------------------------------------
               '  If we weren't already on the phone then check if we should stop
               '--------------------------------------------------------------------
                 If  BBP_PhoneOffHook = False Then
                     BBP_PhoneOffHook           = True
                     BBP_PhoneCallStoppedPlaying = BBP_IsPlaying
                   BBP_WasPlaying               = BBP_IsPlaying
                   
                   If  BBP_IsPlaying Then
                       OnPause
                   End If
                 End If
     
              Return
           End If
   
   
           '------------------------------------------------------------------------
           '  Phone has been hung up
           '------------------------------------------------------------------------           
           If  State.EqualsIgnoreCase("IDLE") Then
                 If  BBP_PhoneOffHook Then
                     BBP_PhoneOffHook = False

                   '----------------------------------------------------------------
                   '  Check if we need to restore playing
                   '----------------------------------------------------------------         
                     If  BBP_PhoneCallStoppedPlaying Then
                       BBP_PhoneCallStoppedPlaying = False
                       
                       If  BBP_WasPlaying Then
                           BBP_WasPlaying = False
                           
                           OnPause
                       End If
                     End If
                 End If
     
                 Return
           End If
End Sub
#end Region

BobVal
 
Upvote 0
Top