Android Question Pause code while sound file plays

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Working on a route direction app that uses sound files.
To avoid a sound file being cut off due to a second sound file starting I need to stop that second sound file starting while the first one still runs.
ResumableSub doesn't solve this problem.
As the sound files have the naming of the actual spoken words I have solved it near enough like this:

B4X:
    If strDirection.Length > 0 Then
        Dim rs As ResumableSub = PlaySoundFile(File.DirRootExternal & "/PhonePats/Map_Directions", strDirection & ".m4a")
        Wait For (rs) Complete (bDone As Boolean)
    End If
    
    'default for Enums.iSoundFileDelayFactor = 140
    Sleep(strDirection.Length * Enums.iSoundFileDelayFactor) 'to avoid cutting off the sound due to the next sound file

Just wondering if there might be a better way to do this.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Code with File.DirRootExternal = broken code.

Assuming that there is a call to Sleep inside PlaySoundFile then your solution looks good.
OK, thanks, forgot about not to use File.DirRootExternal and will fix that.
Are you saying the Sleep should be moved from where it is now to the PlaySoundFile ResumableSub?

RBS
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you saying the Sleep should be moved from where it is now to the PlaySoundFile ResumableSub?
It actually depends on your use case. If you always want to "pause" the code until the sound plays then it makes sense to move it to PlaySoundFile and call PlaySoundFile with Wait For (as you did).

Another possible approach which is more robust is to add the sounds you want to play to a queue (List) and make PlaySoundFile play all sounds in the queue, one by one. This way you will not need to arbitrary pause other parts of the program. I don't know whether it is relevant for your app or not.
 
Upvote 0
Top