Greetings,
I made an app that plays songs based on a user set song start and ending period. That part works perfectly as long as the app is displayed on the phone screen. The app also runs quite well in the background and is not easily killed by Android because it's using the technique used in this app tutorial. The problem I'm having is when the phone screen is off or the app is not in focus the song is not played during the user set time period. When the app doesn't have focus, the log shows the schedule starts but then puts a message to a queue and never plays the song. The song is played by using CallSubDelayed. As soon as I get the app in focus be having it open on screen the message in the queue is released and the song plays.
How do I get the app not to block up the queue?
I made an app that plays songs based on a user set song start and ending period. That part works perfectly as long as the app is displayed on the phone screen. The app also runs quite well in the background and is not easily killed by Android because it's using the technique used in this app tutorial. The problem I'm having is when the phone screen is off or the app is not in focus the song is not played during the user set time period. When the app doesn't have focus, the log shows the schedule starts but then puts a message to a queue and never plays the song. The song is played by using CallSubDelayed. As soon as I get the app in focus be having it open on screen the message in the queue is released and the song plays.
How do I get the app not to block up the queue?
B4X:
** Activity (main) Create, isFirst = false **
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
This is when the app is running with the phone screen off.
Schedule For Song 1 is running.
sending message to waiting queue (CallSubDelayed - ImageViewPlayCurrentSong_Click)
This is when I load up the app on screen.
running waiting messages (2)
** Activity (main) Resume **
** Receiver (servicewidget) OnReceive **
** Service (servicewidget) Start **
Service started in foreground mode.
This code works only if the app is on screen.:
Sub tmrForSchedules_Tick
' Loop through all songs.
' We will exit the loop if the time now is within any scheduled song period to play a song.
' If any is found regardless of wether or not the user is listening to a song, these will be
' played.
'-------------------------------------------------------------------------------------------
For intSongNumber = 1 To 3
' Check if the song has a schedule that's ok to test (light bulb is on).
'-----------------------------------------------------------------------
If kvs.Get("Song" & intSongNumber & "ScheduleIsActivated") Then ' The light bulb is on.
' Get the ticks value for the songs starting period.
'----------------------------------------------------
Dim lngScheduleFrom As Long = _
DateTime.DateTimeParse(DateTime.Date(DateTime.Now), _
kvs.Get("Song" & intSongNumber & "ScheduleActiveFrom") & ":00")
' Check if the ending period is set for the next day.
'--------------------------------------------------
If kvs.Get("Song" & intSongNumber & "ScheduleIsOvernight") Then
' Get the ticks value for the ending period for tomorrow.
'--------------------------------------------------------
Dim lngScheduleUntil As Long = _
DateTime.Add(DateTime.DateTimeParse(DateTime.Date(DateTime.Now), _
kvs.Get("Song" & intSongNumber & "ScheduleActiveUntil") & ":00"),0,0,1)
Else
' Get the ticks value for the ending today.
'------------------------------------------
Dim lngScheduleUntil As Long = DateTime.DateTimeParse(DateTime.Date(DateTime.Now), _
kvs.Get("Song" & intSongNumber & "ScheduleActiveUntil") & ":00")
End If
Select intSongNumber
Case 1
' Go here only if a song is not playing for schedule 2 or 3.
'-----------------------------------------------------------
If blnSchedule2IsPlayingSong = False And _
blnSchedule3IsPlayingSong = False Then
' Go here if the time now is inside the schedul period for song 1.
'-----------------------------------------------------------------
If DateTime.Now >= lngScheduleFrom And _
DateTime.Now <= lngScheduleUntil Then
' Go here if the schedule is not running. This ensures the song is only
' played the first time the time now is in the schedule period.
'----------------------------------------------------------------------
If blnSchedule1IsPlayingSong = False Then
blnSchedule1IsPlayingSong = True
blnScheduleIsPlayingSong = True ' Flag used in the Main module.
Main.blnAsongIsPlaying = False ' Toggle to make the song play.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " is running.")
End If
Else
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the time now is outside of the schedule period.
'-----------------------------------------------------------------------
If blnSchedule1IsPlayingSong = True Then
blnSchedule1IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
End If
End If
Case 2
' Go here if the time now is inside the schedul period for song 2.
'-----------------------------------------------------------------
If blnSchedule1IsPlayingSong = False And _
blnSchedule3IsPlayingSong = False Then
' Go here only once if the time now is inside the schedul period for song 2.
'---------------------------------------------------------------------------
If DateTime.Now >= lngScheduleFrom And _
DateTime.Now <= lngScheduleUntil Then
' Go here if the schedule is not running. This ensures the song is only
' played the first time the time now is in the schedule period.
'----------------------------------------------------------------------
If blnSchedule2IsPlayingSong = False Then
blnSchedule2IsPlayingSong = True
blnScheduleIsPlayingSong = True ' Flag used in the Main module.
Main.blnAsongIsPlaying = False ' Toggle to make the song play.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " is running.")
End If
Else
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the time now is outside of the schedule period.
'-----------------------------------------------------------------------
If blnSchedule2IsPlayingSong = True Then
blnSchedule2IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
End If
End If
Case 3
' Go here if the time now is inside the schedul period for song 3.
'-----------------------------------------------------------------
If blnSchedule1IsPlayingSong = False And _
blnSchedule2IsPlayingSong = False Then
' Go here only once if the time now is inside the schedul period for song 3.
'---------------------------------------------------------------------------
If DateTime.Now >= lngScheduleFrom And _
DateTime.Now <= lngScheduleUntil Then
' Go here if the schedule is not running. This ensures the song is only
' played the first time the time now is in the schedule period.
'----------------------------------------------------------------------
If blnSchedule3IsPlayingSong = False Then
blnSchedule3IsPlayingSong = True
blnScheduleIsPlayingSong = True ' Flag used in the Main module.
Main.blnAsongIsPlaying = False ' Toggle to make the song play.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " is running.")
End If
Else
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the time now is outside of the schedule period.
'-----------------------------------------------------------------------
If blnSchedule3IsPlayingSong = True Then
blnSchedule3IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
End If
End If
End Select
Else ' The light bulb is off.
Select intSongNumber
Case 1
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the turns off the light bulb for the schedule.
'----------------------------------------------------------------------
If blnSchedule1IsPlayingSong = True Then
blnSchedule1IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
Case 2
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the turns off the light bulb for the schedule.
'----------------------------------------------------------------------
If blnSchedule2IsPlayingSong = True Then
blnSchedule2IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
Case 3
' Go here if the schedule is running. This ensures the song is only
' stopped the first time the turns off the light bulb for the schedule.
'----------------------------------------------------------------------
If blnSchedule3IsPlayingSong = True Then
blnSchedule3IsPlayingSong = False
blnScheduleIsPlayingSong = False ' Flag used in the Main module.
Main.blnAsongIsPlaying = True ' Toggle to make the song stop.
CallSubDelayed(Main, "ImageViewPlayCurrentSong_Click")
Log("Schedule For Song " & intSongNumber & " has ended.")
End If
End Select
End If
Next
End Sub