Loop doesn't work correctly on fast CPU?

susu

Well-Known Member
Licensed User
Longtime User
I wrote a talking clock 6 months ago. Because TTS didn't support my language so I combine some sound files to create a phrase.
For example: ItIs.mp3 + Ten.mp3 + Oclock.mp3 = "It is ten o'clock" (of course in my language).

I used a Sub to play each sound file:
B4X:
Dim mp As MediaPlayer

Sub Talk(what As String)
   Do While mp.IsPlaying
   DoEvents
   Loop
   mp.Load(File.DirAssets, what & ".wav")
   mp.Play
End Sub

Talk("ItIs")
Talk("Ten")
Talk("Oclock")

My app worked great on my old phone (800Mhz CPU). I can hear each sound file played one by one. I just changed to new phone (1.5Ghz Dual Core CPU), now it seems all sound files play at the same time. I think the CPU is too fast to handle the loop. Is it right? How can I make the sound files play one by one again?

Thank you for your help.
 

susu

Well-Known Member
Licensed User
Longtime User
No.
A much better solution is to use the Complete event and start the next song.

Thanks Erel. Did MediaPlayer have Complete event?

Oh, I found it. (mp.Initialize2). Let's try new code
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Need help! I tried Complete event and Timer too but can't make it work.
I'm so lazy to rewrite all the code. I wish there're a way to pause between 2 sound files. Something like:

Talk("ItIs")
Pause
Talk("five")
Pause
Talk("twenty")
Pause
Talk("two")
Pause
Talk("minute")
Pause
Talk("pm")
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
It seems like there should be a clean solution using mediaplayer, but in case you don't find one here is a delay sub I created.. not sure it's a completely valid way to do things but it works. I've tested it to about 4 seconds on a DroidX at which point the OS complained. Should be fine for short delays.
B4X:
' I don't recommend long delays here, because OS might take a dump on it.
' delay value is in miliseconds
Sub delay(msdly)
   Dim dlytime, dlytime2 As Long
   dlytime=DateTime.Now
   dlytime2=dlytime
   Do While dlytime2<dlytime+msdly
      dlytime2=DateTime.Now
   Loop
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use something like:
B4X:
Sub Process_Globals
   Dim Songs As List
   Dim CurrentSong As Int
   Dim Mp As MediaPlayer
End Sub
Sub Globals
   
End Sub
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Mp.Initialize2("mp")
      Songs.Initialize
   End If
   
   'To play a list of songs:
   Songs.Clear
   Songs.Add("song1.mp3")
   Songs.Add("song2.mp3")
   Songs.Add("song3.mp3")
   CurrentSong = 0
   Mp_Complete 'Call Mp_Complete to start playing
   
End Sub

Sub Mp_Complete
   If CurrentSong < Songs.Size Then
      Mp.Load(File.DirInternal, Songs.Get(CurrentSong)) 'Change folder location as needed
      Mp.Play
      CurrentSong = CurrentSong + 1
   End If
End Sub
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Erel, you're my angel Base on your code I fixed my app.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…