Android Question Playing a .wav sound

Peekay

Active Member
Licensed User
Longtime User
I have a .wav file in my 'Files' folder which is a camera click.

I have this sub from another link on this site:

PlaySound:
Sub PlayRecording
    Dim ret As Int 'Used in call to Pool.Play. ret = 0 if error occurs
    Dim PoolID As Int 'Identifies the Pool number when a sound file is loaded
    Dim SourceDir As String = File.DirInternal 'or the Directory where your file is located
    Dim SourceFileName As String = "yourfilename.wav" 'The file you wish to use
    Dim VolumeLeft As Double = 1 ' = Value between 0 and 1 for Left Channel
    Dim VolumeRight As Double = 1 ' = Value between 0 and 1 for Right Channel
    Dim Pitch As Double = 1 '= Value between 0 and 2: <1 is low pitch, 1 is normal, 2 is highest pitch
    Dim Priority As Int = 1 'SoundPool Documentation says this value is irrelevant, is for future use
    Dim Loop As Int = 0 'Number of times to repeat the sound, -1 to repeat indefinitely
    'Load the sound
    PoolID = Pool.Load(SourceDir,SourceFileName) 'Loads the file and gets it's number in the pool.
    Sleep(100) 'Some delay required between pool loading and playback - I'm unsure why or how much?
    'Play the sound
    ret = Pool.Play(PoolID,VolumeLeft,VolumeRight,Priority,Loop,Pitch)
End Sub

1. How do I save the file in Dir.Internal?
2. Is this sub appropriate or is there some other simpler routine which I can use?

Thanks
PK
 

klaus

Expert
Licensed User
Longtime User
1. Instead of
Dim SourceDir As String = File.DirInternal
use
Dim SourceDir As String = File.DirAssets
File.DirAssets contains the files from the projects Files folder.

Or use
File.Copy(File.DirAssets, "yourfilename.wav", File.DirInternal, "yourfilename.wav")
to copy the *wav file from the projects Files folder to File.DirInternal.

2. Maybe you could use the MediaPlayer library.
B4X:
Public MP As MediaPlayer

MP.Initialize2("MP")

MP.Load(File.DirAssets, "yourfilename.wav")

MP.Play
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Soundpool will introduce less latency (time between button press and the sound playing) than Mediaplayer, the reason that you need to wait is that the file has to be loaded, but you can pre load the sound and don't need to load it every time you play the sound. You can also use the OnLoadCompleteListener. There is an old example here: https://www.b4x.com/android/forum/threads/multitrigger-soundpool-example.42038/post-253582.

As I say it's an old example, let me know if there are any problems with it.
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Thanks stevel05,
I have studied the example, but the fruit is hanging too high for me.
I do get it to work on the sub quoted above, but it makes the sound quite a few times in stead of once.
I have followed your good advice to load the sound file once in the Activity_Create.

PK
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
makes the sound quite a few times in stead of once.

If you want help with that, post an example project showing how you are calling the play sub (Export as Zip from the file Menu)
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Fixed. The problem was in the camex_preview which repeated ten times.
 
Last edited:
Upvote 0

Christan

New Member
I think you should use the MediaPlayer library. Or use File.Copy (File.DirAssets, "yourfilename.wav", File.DirInternal, "yourfilename.wav") to copy the * wav file from the Projects Files folder to File.DirInternal.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…