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
Hi,

Adding that line causes the app to do a "Force close"

Am I missing something else?


You have to tell Android not to kill the service like this:

B4X:
Sub Service_Start

    Service.StartForeground(1, ntfTheNotification) 

.....
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Move these lines to Service_Create

B4X:
ntfTheNotification.Initialize
ntfTheNotification.Icon = "icon"    
ntfTheNotification.Sound = False

If it doesn't work, upload your project in a ZIP and I'll take a look.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Looks like that statement requires that a notification needs to be displayed first before using it so I did this:

B4X:
Sub Service_Create

   ntfTheNotification.Initialize
   ntfTheNotification.Icon = "icon"    
   ntfTheNotification.Sound = False
   ntfTheNotification.SetInfo("Chimes", "Chimes are now active.", Main)
   ntfTheNotification.Notify(1) 
End Sub

Sub Service_Start (StartingIntent As Intent)

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

   Service.StartForeground(1, ntfTheNotification) 

   ' 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

Hopefully the chiming will not fall asleep. I'll let you know later.

Thanks for the help.


Move these lines to Service_Create

B4X:
ntfTheNotification.Initialize
ntfTheNotification.Icon = "icon"    
ntfTheNotification.Sound = False

If it doesn't work, upload your project in a ZIP and I'll take a look.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi NJDude,

It still stops after an hour so I will try to move the StartForeground line to where the media player starts to see it that will make a diference. If not I will prepare a zip of the project.

This should work because I know other apps will run media files without stopping while the screen is turned off.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I decided to place Log statements in many places in the service code to see where the problem is.

Instead of showing gaps in the timer ticks, the media player played when it should. Huh???? Why does it work when the Log statements are placed in the code?

Anyway, I will start removing the Log statements to see if just 1 Log statement will keeps things going.

I noticed that the Log writes entries into to Log tab of the IDE so if I disable the B4A bridge will the Log be written somewhere on the Android device? Will this eventually use up the storage of the Android device?

Is there a statement I can add to my code that wakes up the Android device just like the Log statement does?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Perhaps is the placement of some statements, this is how I have setup a service in one of my apps and works fine.

B4X:
Sub Service_Create

    CheckSizeTimer.Initialize("CheckSizeTimer", 500)    
    
    Notification1.Initialize
    Notification1.Icon = "icon" 
    Notification1.Vibrate = False

End Sub

Sub Service_Start

    Notification1.SetInfo("Downloading " & MP3, "Progress: 0%", Null)
    Notification1.Sound = False
      
    Service.StartForeground(1, Notification1) 

    CheckSizeTimer.Enabled = True

End Sub

Then, in a routine when the process is done I have:

B4X:
Service.StopForeground(1)

Check the location of my Timer initialization, I have it in Service_Create, also, I just use KeepAlive and not PartialLock, I don't know if that matters.

Regarding the Log, I've use it just a couple of times, however there are free apps on the Market that allow you to read log files.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for the code sample.

I will try that after I see what happens if I use only one Log statement which is called every second.

I also have 4 different notifications in the code. Does the number 1 in the StartForground have any importance to any particular notification that gets locked?

Also before adding the Log statements the app does not work after 1 hour of use which included using the StartForground statement.

Will leaving the Log statement in the code eventually use up storage on the Android device?

Perhaps is the placement of some statements, this is how I have setup a service in one of my apps and works fine.

B4X:
Sub Service_Create

    CheckSizeTimer.Initialize("CheckSizeTimer", 500)    
    
    Notification1.Initialize
    Notification1.Icon = "icon" 
    Notification1.Vibrate = False

End Sub




Sub Service_Start

    Notification1.SetInfo("Downloading " & MP3, "Progress: 0%", Null)
    Notification1.Sound = False
      
    Service.StartForeground(1, Notification1) 

    CheckSizeTimer.Enabled = True

End Sub

Check the location of my Timer initialization, I have it in Service_Create,

Regarding the Log, I rarely use it, however there are free apps on the Market that allow you to read log files.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
When I disabled the B4A Bridge, the code to play the media player also stopped if there is a Log statement in the code.

This leads me to believe the issue is this:
If the B4A Bridge is not connected my code as it is now will not work.

Can you show me the coding for a bare bones minimum working service that has a timer that ticks once each 1 second since you know how to do it?

I will use that to rebuild my media player code based on that.

Thanks.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Actually, the sample I posted is the working code, is not really much you need to do, my question is, are you starting the service on your MAIN routine?, sorry for the obvious question but you never know :D
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Yes.

B4X:
   ' Start up the services if needed.
   '---------------------------------
   If IsPaused(ChimeService) Then
      StartServiceAt(ChimeService, DateTime.Now, True)
   End If
   
   If IsPaused(WidgetService) Then
      StartServiceAt(WidgetService, DateTime.Now, True)
   End If

Could you strip down your working code and post it up since it's using a timer?



Actually, the sample I posted is the working code, is not really much you need to do, my question is, are you starting the service on your MAIN routine?, sorry for the obvious question but you never know :D
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
This is a bare-bones service sample, just a counter that will last 1 minute.

I don't use StartServiceAt on any of my apps, just KeepAlive, so far, I haven't had any issues.
 

Attachments

  • GenericSample.zip
    9.7 KB · Views: 221
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
It works fine until I turn off my screen and disable B4A Bridge so doing both of those things make things not work.

Looks like Erel needs to check into why this is happening.

If the screen is turn on it will work.
If the screen is turned off but the B4A Bridge is on then it also works.
If they are both turned off then it does not work.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
No, just listening for the sound of the notification with my ear.

I did make a slight change in the code to make the notification make a sound every 20 seconds to make sure it's constantly running.

If you turn off your screen and disconnect the B4A Bridge do you get the same results as I am?

By the way, I'm using an Asus Transformer tablet to test this with.

Now I'm intrigued.

Are you including any Debug Information or Attach Debugger?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I don't experience that problem, I have a cheap clone tablet, a Galaxy tab and an EVO 4G, no problem at all.

What are the steps you follow so I can do the same here and see if it happens.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I also have a 7" capacitive screen Chinese Herotab C8 tablet I also want to test it on.

Here are the steps:
Connect to B4A Bridge.
Change the app to constantly do the notification by turning the notification sound off until the timer is up.
Change the counter limit to 20.
Compile and install the app to your tablet.
Disconnect the B4A Bridge.
Run the app.
While the app is running turn off the tablet screen.
Listen to see if the notification make a sound each time the counter reaches 20.

I don't experience that problem, I have a cheap clone tablet, a Galaxy tab and an EVO 4G, no problem at all.

What are the steps so I can do the same here and see if it happens.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I just tried it on my Chinese tablet and the same thing happens.

The notifications do eventually make a sound but not every 20 seconds. The Chinese tablet does handle it better than my Asus Transformer which practically stops.

Here is the modified code:

B4X:
'Service module

Sub Process_Globals
   
   Dim Counter As Int 'A Simple counter to use in the example
   Dim Flag As Boolean 'This flag will be used like this: Flag = FALSE means counted reached 100 Flag = TRUE service stopped/aborted.
   Dim CheckSizeTimer As Timer
    Dim Notification1 As Notification

End Sub

Sub Service_Create

   CheckSizeTimer.Initialize("CheckSizeTimer", 1000)

      Notification1.Initialize
      Notification1.Icon = "icon"
      Notification1.Vibrate = False

End Sub

Sub Service_Start

   Counter = 0
         
   Notification1.SetInfo("Counting", "Progress: 0", Null)
      Notification1.Sound = False
   
   Service.StartForeground(1, Notification1) 
            
   CheckSizeTimer.Enabled = True
            
End Sub

Sub Finish

'   Service.StopForeground(1)

'    CheckSizeTimer.Enabled = False
                     
   Notification1.Sound = True
    Notification1.SetInfo("Finished", "Counting complete", Null)
   Notification1.AutoCancel = True
    Notification1.Notify(1)
   Notification1.Sound = False

'   StopService("")

End Sub

Sub Service_Destroy

   If Flag = True Then
      Flag = False
         
       Service.StopForeground(1) 'Return the service to the "background" (also removes the ongoing notification)

          CheckSizeTimer.Enabled = False
                     
         Notification1.Sound = True
    
       Notification1.SetInfo("Cancelled", "Counting Cancelled", Null)
       Notification1.AutoCancel = True
          Notification1.Notify(1)
   End If
End Sub

Sub CheckSizeTimer_Tick

   Notification1.SetInfo("Counting", "Progress: " & Counter, Null)
   Notification1.Notify(1)   
            
   Counter = Counter + 1
            
   If Counter > 20 Then
      Counter = 0
'         Flag = False

      Finish
   End If
End Sub
 
Upvote 0
Top