Android Question Sound Pool - audio file not playing

Sanxion

Active Member
Licensed User
Longtime User
Hi all

I have encountered a problem when attempting to play a sound file immediately after loading it - the sound appears not to be played. However, if I introduce a "break" between the two lines, e.g. a message box, the sound is played. It also plays during debug.

This is the Sub:

B4X:
Sub ivSound_Click
   
    Dim intSoundID As Int
    Dim TaskID As String
   
    Try
   
    ' Find out which panel is visible and get the task number
    For Each v As View In Activity
        If v Is Panel Then  
            Dim PanelTag As String
            PanelTag = v.Tag
            'Dim VisiblePanelID As String
            If PanelTag.StartsWith("Task") Then
                If v.Visible = True Then
                    TaskID = PanelTag
                End If
            End If
        End If
    Next
   
    ' Load the sound file
    intSoundID = SP.Load(File.DirAssets, TaskID & ".mp3")
    'Play the sound file
        'Msgbox(intSoundID,"")
    SP.Play(intSoundID, 1, 1, 1, 0, 1)
   
    Catch
   
        Msgbox(LastException.Message,"")
   
    End Try
   
End Sub

Any help would be much appreciated.
 

JordiCP

Expert
Licensed User
Longtime User
1) In general, any "Play" command that is launched before the file is completely loaded will not work.
2) mp3 files can take a bit longer to be loaded than other formats.

Possible workarounds can be to load the different sounds in advance (if there are not too many and/or they are not too big) and play the chosen one when needed.
Or just add a small delay (but the time required to load and decode can be device dependant)
Also, if you can convert the format from .mp3 to .ogg, it will be easier for SoundPool
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Faster!! :D

16-homer-simpson.gif
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
The correct way to work with SoundPool is to load all the files when the program starts and then call play whenever needed.
That is what I have done before. But I have over 350 files in this situation. Should I load them all in Activity_Create?
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
1) In general, any "Play" command that is launched before the file is completely loaded will not work.
2) mp3 files can take a bit longer to be loaded than other formats.

Possible workarounds can be to load the different sounds in advance (if there are not too many and/or they are not too big) and play the chosen one when needed.
Or just add a small delay (but the time required to load and decode can be device dependant)
Also, if you can convert the format from .mp3 to .ogg, it will be easier for SoundPool
Thank-you for the response.
 
Upvote 0
Top