Custom Click Sounds with MediaPlayer?

GHosaPhat

Member
I have a series of ImageView elements in my application that should act as "buttons". I want the user to hear a "click" sound when they tap the ImageView, but no sound currently plays. Through some of my testing, I figured that I might want to play some custom sounds for each of the ImageView objects' click events.

I found some .wav files for what I wanted to hear, so I added the files to the project and added the following code elements:
B4X:
Sub Globals
   Dim wavWinner As MediaPlayer
   Dim wavButton As MediaPlayer
   Dim wavError As MediaPlayer
   Dim wavEndGame As MediaPlayer
...
End Sub

Sub Activity_Create(FirstTime As Boolean)
   wavButton.Initialize
   wavButton.Looping = False
   wavButton.Load(File.DirAssets, "button.wav")
   
   wavWinner.Initialize
   wavWinner.Looping = False
   wavWinner.Load(File.DirAssets, "winner.wav")
   
   wavError.Initialize
   wavError.Looping = False
   wavError.Load(File.DirAssets, "error.wav")
   
   wavEndGame.Initialize
   wavEndGame.Looping = False
   wavEndGame.Load(File.DirAssets, "endgame.wav")
...
End Sub

Sub Button1_Click
   wavButton.Play
...
End Sub

Sub Button2_Click
   wavButton.Play
...
End Sub

Sub Button3_Click
   wavButton.Play
...
End Sub
And similar code for other buttons. I compiled it to my device, but I'm not getting any of the sounds to play. I'm not sure if I'm missing something, so any help would be great. :)
 

stevel05

Expert
Licensed User
Longtime User
The code looks OK, you could make sure the volume is up using SetVolume method.

If you post your project or a small test project that does the same (including your files), we can test it on different devices.

For what you are trying to achieve SoundPool may be better and would be more flexible. To save some space you could also look at converting the wav's into ogg.
 
Upvote 0

GHosaPhat

Member
Well, I added the wavButton.SetVolume(1,1) after the Initialize method, and still no go. I assume SoundPool is an additional library, and at this point I'm still working with the trial version, so I can't use that yet (I do plan on purchasing when I can, but that's not quite an option just yet).

I put together a simple calculator that does the same things as the real app to see if I could make it work there, but it's the same result. I was trying to attach the app, but my ZIP file is apparently just a bit too large.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
SoundPool is part of the Audio Library, I'm not sure if it's an extra or part of the main system.

You could upload the project to a file sharing site like dropbox or similar if you like, or try compressing the wav's to ogg. That should do it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It's working fine on my HTC, it sounds just like a typewriter.

Silly question, but do you have the phone volume up. It should be the media volume.
 
Upvote 0

GHosaPhat

Member
LOL, no problem. Silly questions are the basis of the diagnostic process. :)

As I was testing, I thought I had checked all of the volume settings. Of course, when I went back to check again, it seems that the media volume had, in fact, been turned down all the way. I turned it back up, and sure enough, it worked as expected. Dangit! I HATE it when I miss the basics and spend so much time troubleshooting the obvious. :BangHead:

AHH! I just realized my epic fail... I had been testing on both the emultor and the device itself. I checked all of the settings on the emulator (which doesn't seem to pass the sounds back to the PC's speakers for whatever reason, but I don't really care at this point), but NOT the settings on the device. Thanks for the "silly question". I love it when I can make myself look like a total moron in front of other developers. :p
 
Last edited:
Upvote 0
Top