Android Question [SOLVED] - Listening for when audio from a video or sound file is played from another app

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm currently using this coding to detect when the phone state changes when the phone app is on a phone call. Is there something like it to detect when audio from another app starts playing? I want to use the same type of coding to mute the exoPlayer volume as soon as another app wether it's a video or other audio starts playing.

Process_Globals:
Private PE As PhoneEvents
Private PhoneId As PhoneId

B4X:
Sub InitializeObjects
    PE.InitializeWithPhoneState("PE",PhoneId)
End Sub

B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    
    If kvs.Get("SoundIsOn") = True Then
        If State = "IDLE" Then
            exoPlayer.Volume = kvs.Get("MasterVolume")
        Else
            exoPlayer.Volume = 0
        End If
    End If
End Sub
 

Peter Simpson

Expert
Licensed User
Longtime User
@rleiman why on earth are you making life so difficult for your self. You should share of my just using the built in media player library in the service to play the tick sound and the 15 (just swap the sound file just before 15 minutes) to play the 15 minutes chime. You already have the code in post three that works. This project should only take you less than an hour to complete as you are a well seasoned B4A veteran :)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Hi John,

I just looked at your code. My app is already set up with similar logic. I doesn't work for me because the coding in post #3 detects both internal and external media playing. Also the current coding will not play both the ticking and the chiming together. I need a way to test only for when the external media is playing. Sorry for the confusion.

I'm confused, if neither exoplayer is playing anything, then the IsMediaPlayer function should come back FALSE is no other app is playing anything. If it's coming back True then there can only be three causes that I can think of:

1) The IsMediaPlaying function is not working correctly
2) There is another app playing sound
3) You are using large sound files to play the ticks or chimes sounds. Are you are playing sound files that take more then 1000ms to fully play?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I'm confused, if neither exoplayer is playing anything, then the IsMediaPlayer function should come back FALSE is no other app is playing anything. If it's coming back True then there can only be three causes that I can think of:

1) The IsMediaPlaying function is not working correctly
2) There is another app playing sound
3) You are using large sound files to play either the ticks or the chimes - meaning you are playing sound files that last well over 1000ms - is this possible?

He only needs one player preferably the media player to play both sounds, that will make things a lot easier for himself.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I'm confused, if neither exoplayer is playing anything, then the IsMediaPlayer function should come back FALSE is no other app is playing anything. If it's coming back True then there can only be three causes that I can think of:

1) The IsMediaPlaying function is not working correctly
2) There is another app playing sound
3) You are using large sound files to play either the ticks or the chimes - meaning you are playing sound files that last well over 1000ms - is this possible?
Yes, the chiming sound file is large. It plays the Westminster chimes and the ticking sound file is small. Maybe 500ms.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
He only needs one player preferably the media player to play both sounds, that will make things a lot easier for himself.
IsMediaPlaying returns true when the chimes are playing preventing the ticking from being played by the other player.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, then some more thoughts:

1) In post #5 you say you " I want both to play at the same time" - do you literally mean that you want to play the tick sounds actually during the chimes sound playing? if that's not what you meant, then I agree with peter that you only need one exoplayer.

2) Ah, OK the chimes is causing the problem. Here's a solution:

B4X:
Sub tmr1000ms_Tick    'this happens every 1 second
    'we do the audio check BEFORE playing anything so there is minimal chance of the check falsely detecting our app's audio
    If IsMediaPlaying = False then  'if no external audio playing, then its ok to play tick/chime below

          If TimeToPlayQuarterlyChime then   'if its on the 15 min/quarterly of hour, then
              PlayChimeSound  'play quarterly chime sound instead of tick sound
              tmr1000ms.Enabled = False
              Sleep(xxx)  'xxx = the number of ms it takes to fully play the chimes sound file
              tmr1000ms.Enabled = True
          else
              PlayTickSound    'play the one second tick sound
          end if

    end if

End Sub
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
OK, then some more thoughts:

1) In post #5 you say you " I want both to play at the same time" - do you literally means you want to play the tick sound actually during then the chimes sound is playing? if that's not what you meant, then I agree with peter that you only need one exoplayer.

2) Ah, OK the chimes is causing the problem. Here's a solution:

B4X:
Sub tmr1000ms_Tick    'this happens every 1 second
    'we do the audio check BEFORE playing anything so there is minimal chance of the check falsely detecting our app's audio
    If IsMediaPlaying = False then  'if no external audio playing, then its ok to play tick/chime below

          If TimeToPlayQuarterlyChime then   'if its on the 15 min/quarterly of hour, then
              PlayChimeSound  'play quarterly chime sound instead of tick sound
              tmr1000ms.Enabled = False
              Sleep(xxxmx)  'xxx = the number of ms it takes to fully play the chimes sound file
              tmr1000ms.Enabled = True
          else
              PlayTickSound    'play the one second tick sound
          end if

    end if

End Sub
I'm looking to have exoPlayer1 play the chimes and exoPlayer2 also play the tick sound while the exoPlayer1 is still playing the chimes.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Wait....cancel my previous updated code above because my original code in post #12 should work fine as is and here's why..

Even if the chimes file takes more then 1000ms to play, it's OK that IsMediaPlaying returns TRUE while the chimes is playing because the code will detect that something is making audio and will immediately exit the routine (and let the chime continue to play). Then, when the chimes is finally done playing, IsMediaPlaying will return FALSE, and then the tick sounds will play each second (as long as there is no external audio) until the next quarterly hour, then the chimes will play again and so on.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, I think I understand the issue now....if the chimes are playing, then the ticks wont also play.

So, here is some new code:
B4X:
Sub Globals
    AlsoTicks As Boolean
End Sub

Sub tmr1000ms_Tick    'this happens every 1 second
 
    If IsMediaPlaying = False Or AlsoTicks = True Then

        If TimeToPlayQuarterlyChime And AlsoTicks = False Then
            PlayChimeSound
            AlsoTicks = True
        Else
            PlayTickSound
        End If

    End If

End Sub

Sub ChimesPlayer_Complete
    AlsoTicks = False
End Sub

What this code does is when the chimes are playing, it sets the "AlsoTicks" Boolean var to true so that the routine will continue to ALSO play the ticks every second.

And when the ChimesPlayer_Complete event happens (when the chimes is done playing), it clears this flag so that it will just play ticks.

The only potential problem is that if the user plays audio from another app while your (longer) chimes are playing, then your app will not know to shut up until the chimes are done playing.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
OK, I think I understand the issue now....if the chimes are playing, then the ticks wont also play.

So, here is some new code:
B4X:
Sub Globals
    AlsoTicks As Boolean
End Sub


Sub tmr1000ms_Tick    'this happens every 1 second
  
    If IsMediaPlaying = False Or AlsoTicks = True Then

        If TimeToPlayQuarterlyChime And AlsoTicks = False Then 
            PlayChimeSound
            AlsoTicks = True
        Else
            PlayTickSound 
        End If

    End If

End Sub

Sub ChimesPlayer_Complete
    AlsoTicks = False
End Sub

What this code does is when the chimes are playing, it sets the "AlsoTicks" Boolean var to true so that the routine will continue to ALSO play the ticks every second.

And when the ChimesPlayer_Complete event happens (when the chimes is done playing), it clears this flag so that it will just play ticks.

The only potential problem is that if the user plays audio form another app while the longer chimes is playing, then your app will not know to shut up until the chimes is done playing.
I love it. Looks like it will work. I will let you know tomorrow.

Thanks so much everyone for being patient with me.
 
Upvote 0
Top