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:

NJDude

Expert
Licensed User
Longtime User
OH NOW I SEE!!!

Well, I have to say that I never tried it that way, and yes, the sample I sent you does the same on my device, if you unplug the phone and lock it the service goes to sleep. My apologies if I confused you.

I made some modifications to my original example and now it works as intended, but, after you unlock the device the service starts again, I've tried some other apps like a media player that came with my device and it does the same thing so I'm thinking that's how things work, I have to say that I've reached the limit of my knowledge, I hope someone else can shed some light.

Look at the attached project and tell me how it goes.

NOTE: Now I have questions of my own, like for example, how to continue a download when you lock the phone?, are there certain things that cannot be done while the phone is locked?, interesting topic.
 

Attachments

  • GenericSample1.zip
    9.8 KB · Views: 206
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Erel,

Can you work on making a service not go to sleep when the screen is off since this is happening to other developers?

I've already seen other apps that this does not happen. If this is possible, I would like to do the same with B4A.

Thanks.

OH NOW I SEE!!!

Well, I have to say that I never tried it that way, and yes, the sample I sent you does the same on my device, if you unplug the phone and lock it the service goes to sleep. My apologies if I confused you.

I made some modifications to my original example and now it works as intended, but, after you unlock the device the service starts again, I've tried some other apps like a media player that came with my device and it does the same thing so I'm thinking that's how things work, I have to say that I've reached the limit of my knowledge, I hope someone else can shed some light.

Look at the attached project and tell me how it goes.

NOTE: Now I have questions of my own, like for example, how to continue a download when you lock the phone?, are there certain things that cannot be done while the phone is locked?, interesting topic.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Yes. Here's the code:

B4X:
Sub PlayTheChimes

   ntfTheNotification.Notify(2) 

   pwsPhone.KeepAlive(False)
   pwsPhone.PartialLock

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

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

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

   pwsPhone.ReleaseKeepAlive
   pwsPhone.ReleasePartialLock
End Sub

Did you use a PartialLock to prevent the CPU from sleeping?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I have other apps on my device that play media while the screen is turned off which tells me that it can be done. I'm not sure how they do it though.

Also the alarm app will work and make sounds while the screen is also off.

Can an alarm app be made using B4A ? That will solve the media player issue.

This is all that can be done to prevent the device from going to sleep. There are no other available APIs to prevent it.

Seems like the manufacturer has decided to add its own limits.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
The alarm app is but this app is a 3rd party app.

https://market.android.com/details?...sMSwyLDEsImZyLmZ1bnNzb2Z0LmFwcHMuYW5kcm9pZCJd

It works while the screen is off and the device is locked. When it makes media player sounds the screen turns on for the duration of the sounds then turns back off when the sounds are finished.

This is a good example of what I wish to do with my clock chiming sounds. I they can do it I hope I can also do it.

Are they system applications?
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I tried StartServiceAt but no luck.

It works only if the B4A Bridge is turned on.

Is there a way to emulate having the B4A bridge being turned on inside my app? That will also solve the issue.


Can the service continuously call itself with StartServiceAt ?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I think I might know why that app works and ours don't: WIDGETS, the sample I wrote is a service so is your app, the app you mentioned has widgets, so, I guess that might be the answer.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I tried that app without the widgets. I wonder if using a widget does make it work and how they did it.

I think I might know why that app works and ours don't: WIDGETS, the sample I wrote is a service so is your app, the app you mentioned has widgets, so, I guess that might be the answer.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Erel might be right also when he mentioned that maybe the manufacturer added some limits, the app I have doesn't download when you lock the phone (HTC EVO 4G) however the same app on a Galaxy tab works without a problem even if it's locked and unplugged. I'm curious to know what's going on.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Most likely it's the cause but I wish I knew how fun77 was able to get their app to wake up the phone to play the media sounds.

Maybe Erel can figure out a way to do the same thing.

Erel might be right also when he mentioned that maybe the manufacturer added some limits, the app I have doesn't download when you lock the phone (HTC EVO 4G) however the same app on a Galaxy tab works without a problem even if it's locked and unplugged. I'm curious to know what's going on.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Maybe it's because B4A Bridge has code that keeps a network connection open?

I found that other people are having the same thing happen to them:
http://www.b4x.com/forum/basic4android-updates-questions/8106-keepalive.html

If someone has a small working project that they can upload that uses the media player in a service controlled by a timer when the screen is sleeping that will really be appreciated. ;)

There is nothing special in B4A Bridge. It is a regular application. I don't think that it is related. Note that the source code of it is available on the forum.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
That would explain why it works with the phone plugged in.

I guess I will need to go another way to play the media.

I think setting up and alarm service and a broadcast receiver would be the way to go.

How do I schedule an alarm and set up a broadcast receiver to respond to the alarm event in B4A? If any has done it please upload a project so we can learn how to do it.

Even if you just plug in your phone via USB the device will not go totally to sleep.
 
Upvote 0
Top