Android Question automatic launch

Tata Mapassa

Member
Licensed User
Longtime User
Hello,

How to make an audio file run (mediaplayer1.play) at a specific time and when checkbox1 is true?
I tried the following code but it does not work:

DateTime.DateFormat = "hh: mm"

If CheckBox1.Checked = True And DateTime.Time (DateTime.Now) = "08:00" Then
mediaplayer1.play
End If

thank you
 

udg

Expert
Licensed User
Longtime User
Are you using a loop to continously check if it's play time (08:00 in your example)?
Use a service instead. Start it at the desidered time, hold a lock and play the audio. Then release the lock and wait for next "wake up" time.
Or if it's for a running app, just use a timer ticking each minute to check if it's time to play the audio.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Something like:
B4X:
Dim Timer1 As Timer ' in Process_Globals
..
'Activity_Create
Timer1.Initialize("Timer1",1000 * 60) 'check each minute
Timer1.Enabled = True 'start timer
...
Sub Timer1_Tick
If DateTime.Time (DateTime.Now) = "08:00" AND Not(mp.IsPlaying) Then
      Timer1.Enabled = False
      mp.Play
End If
End Sub

This is just to give you a rough idea.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
File/export as zip then publish your code here so others could check about possible errors in it.
BTW, "it does not work !" is not that helpful as a message.. :)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
"08:00" : AM, PM or both?
@GOMBE already defined (post #1) its time format as:
DateTime.DateFormat = "hh: mm"
so I expect him to know what he needs.

You must use StartServiceAt or StartServiceAtExact for this.
Fully agree. It's what I suggested in post #2.
Then it came to mind the probably it's a one-shot for a running app where a timer could serve as well.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Using "HH:mm" it differenziates between 08:00 and 20.00; it depends on what the OP needs.
 
Upvote 0
Top