Android Question Playing default system sound

rleiman

Well-Known Member
Licensed User
Longtime User
I would like to play a default system sound that exists on every Android device.

If you go to the settings on a phone, there is an option that allows the user to set a new system volume. When the user moves the seekbar thumb a sound is played so the user knows what the new volume happens to be.

I would like to do the same in my app.

My app is already set up so it will detect when the seekbar value changes.

Since there are many versions of Android out there, the default system sound could be in several different places on each phone.

Can you show me the needed coding to locate and play the system sound just like when the user uses the seekbar in the Android "Volumes" dialog box? I see many apps on Google Play do this but I don't know how they did it.

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I didn't find a way to play this sound. You can always capture it and then distribute it as part of your app.

You can play some standard sound effects with this code:
B4X:
Sub Activity_Click
   Dim am As JavaObject = GetContext.RunMethod("getSystemService", Array("audio"))
   am.RunMethod("playSoundEffect", Array(8))
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

The available constants are listed here: http://developer.android.com/reference/android/media/AudioManager.html#FX_KEYPRESS_STANDARD
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for the code. It works great.

Can you explain what the coding does?

I would love to learn about it.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…