Media player stops working after 1 hour

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you look at my code and tell me why the Media Player stops working after 1 hour?

While the screen is turned off it stops working after 1 hour has passed. If I turn the screen back on everything is normal.

The Media Player is used in a Service Module which should be running but stops after 1 hour after it is running.

It will work ok until after an hour has passed if I re-install the app.

I also noticed after 1 hour has passed the notifications also are not being updated.

Here is the Service Module I'm using. Sorry for such a large module:

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   ' Media Player.
   '--------------
   Dim mpMediaPlayer As MediaPlayer

   ' Timers.
   '--------
   Dim tmrChimeTimer As Timer
   
   ' Phone.
   '-------
   Dim pwsPhone As PhoneWakeState

   ' Notifications.
   '---------------
   Dim ntfTheNotification As Notification
   
   ntfTheNotification.Initialize
   ntfTheNotification.Icon = "icon"
   ntfTheNotification.Sound = False
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

   ToastMessageShow("Chimes is now active.", False)

   ' This will be used to play the chimes.
   '--------------------------------------
   mpMediaPlayer.Initialize2("MediaPlayer")
   mpMediaPlayer.SetVolume(1, 1)

   ' Start the Timer.
   '-----------------
   tmrChimeTimer.Initialize("tmrChimeTimer", 1000)
   tmrChimeTimer.Enabled = True

End Sub

Sub Service_Destroy

End Sub

Sub tmrChimeTimer_Tick
    
   Dim intHour As Int
   Dim intMinute As Int
   Dim intSeconds As Int
   Dim intTheCurrentDateTime As Long
   Dim blnOkToChime As Boolean

    intTheCurrentDateTime = DateTime.Now
   intHour = DateTime.GetHour(intTheCurrentDateTime)
    intMinute = DateTime.GetMinute(intTheCurrentDateTime)
    intSeconds = DateTime.GetSecond(intTheCurrentDateTime)
   
'   ToastMessageShow(intHour & " Hours.   " & intMinute & " Minutes.   " & intSeconds & " Seconds.", False)
   
   blnOkToChime = False
   
   If mpMediaPlayer.IsPlaying = False AND intSeconds < 10 Then

      Select intMinute
         Case 15
            
            ToastMessageShow("15 past", True)
            
            If main.blnToggleButton15MinuteChimeIsOn = True Then
               mpMediaPlayer.Load(File.DirAssets, "quarter.mp3")
               ntfTheNotification.SetInfo("Chimes", "It's now 15 past the hour.", Main) 

               PlayTheChimes
            End If
      
         Case 30

            ToastMessageShow("30 past", True)
            
            If main.blnToggleButton30MinuteChimeIsOn = True Then
               mpMediaPlayer.Load(File.DirAssets, "half.mp3")
               ntfTheNotification.SetInfo("Chimes", "It's now 30 past the hour.", Main) 

               PlayTheChimes
            End If
      
         Case 45

            ToastMessageShow("45 past", True)
            
            If main.blnToggleButton45MinuteChimeIsOn = True Then
               mpMediaPlayer.Load(File.DirAssets, "quarter_before.mp3")
               ntfTheNotification.SetInfo("Chimes", "It's now 45 past the hour.", Main) 

               PlayTheChimes
            End If
            
         Case 0
         
            ToastMessageShow("on the hour", True)
            
            If main.blnToggleButtonOnTheHourChimeIsOn = True Then
               
               Select inthour
                  Case 1
                     mpMediaPlayer.Load(File.DirAssets, "one_chime.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 1 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 2
                     mpMediaPlayer.Load(File.DirAssets, "two_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 2 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 3
                     mpMediaPlayer.Load(File.DirAssets, "three_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 3 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 4
                     mpMediaPlayer.Load(File.DirAssets, "four_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 4 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 5
                     mpMediaPlayer.Load(File.DirAssets, "five_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 5 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 6
                     mpMediaPlayer.Load(File.DirAssets, "six_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 6 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 7
                     mpMediaPlayer.Load(File.DirAssets, "seven_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 7 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 8
                     mpMediaPlayer.Load(File.DirAssets, "eight_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 8 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 9
                     mpMediaPlayer.Load(File.DirAssets, "nine_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 9 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 10
                     mpMediaPlayer.Load(File.DirAssets, "ten_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 10 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 11
                     mpMediaPlayer.Load(File.DirAssets, "eleven_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 11 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 12
                     mpMediaPlayer.Load(File.DirAssets, "twelve_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 12 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 13
                     mpMediaPlayer.Load(File.DirAssets, "one_chime.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 1 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 14
                     mpMediaPlayer.Load(File.DirAssets, "two_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 2 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 15
                     mpMediaPlayer.Load(File.DirAssets, "three_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 3 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 16
                     mpMediaPlayer.Load(File.DirAssets, "four_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 4 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 17
                     mpMediaPlayer.Load(File.DirAssets, "five_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 5 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 18
                     mpMediaPlayer.Load(File.DirAssets, "six_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 6 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 19
                     mpMediaPlayer.Load(File.DirAssets, "seven_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 7 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 20
                     mpMediaPlayer.Load(File.DirAssets, "eight_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 8 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 21
                     mpMediaPlayer.Load(File.DirAssets, "nine_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 9 O'clock.", Main) 

                     PlayTheChimes

                  Case 22
                     mpMediaPlayer.Load(File.DirAssets, "ten_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 10 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 23
                     mpMediaPlayer.Load(File.DirAssets, "eleven_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 11 O'clock.", Main) 

                     PlayTheChimes
               
                  Case 24
                     mpMediaPlayer.Load(File.DirAssets, "twelve_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 12 O'clock.", Main) 

                     PlayTheChimes

                  Case 0
                     mpMediaPlayer.Load(File.DirAssets, "twelve_chimes.mp3")
                     ntfTheNotification.SetInfo("Chimes", "It's now 12 O'clock.", Main) 

                     PlayTheChimes
               End Select ' inthour
            End If ' main.blnToggleButtonOnTheHourIsOn = True
      End Select
   End If ' mpMediaPlayer.IsPlaying = False AND intSeconds < 10
End Sub

' Media Player
'-------------

Sub PlayTheChimes

   ntfTheNotification.Notify(1) 

   pwsPhone.KeepAlive(False)
   pwsPhone.PartialLock

   ntfTheNotification.SetInfo("Chimes", "Starting the chimes.", Main) 
   ntfTheNotification.Notify(2) 

   mpMediaPlayer.SetVolume(Main.fltVolume, Main.fltVolume)
   mpMediaPlayer.Play
End Sub

Sub MediaPlayer_Complete
   
   ntfTheNotification.SetInfo("Chimes", "Chiming complete.", Main) 
   ntfTheNotification.Notify(3) 

   pwsPhone.ReleaseKeepAlive
   pwsPhone.ReleasePartialLock
End Sub
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
We tried that and the sound plays as if the sounds is turned on and off at random intervals. It's ok when the phone is plugged in to is turned on.

That's why we need to schedule alarms like you can do in Java even though I don't know much about Java. That's why we are using B4A.

Use StartServiceAt. This is exactly what it does.
 
Upvote 0
Top