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

RB Smissaert

Well-Known Member
Licensed User
Longtime User
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.
>>
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.

Not sure this would work.
This app works with a service (called Tracker) that passes satnav values (time, latitude, longitude and altitude) to the main app code, about every second or so.
This app has route points on an OSM map and if the distance between the provided latitude and longitude from Tracker is less than X meters (default 12 meters)
then it will trigger playing the relevant direction sound file. The principal of all this is very simple, but it gets seriously complicated if you code for situations where
the people using this take a wrong turn or just walk somehow away from the route or somehow a route point was missed. I think it is the most complicated code (even although it is not much code) I have ever dealt with. Trouble is to test it properly you will need to walk the route.
I think I have it nearly all fixed now though.

Will look at this suggestion though to use a queue with a list holding the sound files or sound files names.

RBS
 
Upvote 0
Top