Android Question Set Volume Programatica[ly

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I am trying to set the volume a Beep is played at and then restore the volume. I thought this would be simple but the compiler doesn't like my code.
It looks straight forward but I'm missing something.
Any help much appreciated.

The relevant part of code.

Adjust volume:
VolMus = Beep1.VOLUME_MUSIC        'Save current volume setting in integer '
Beep1.VOLUME_MUSIC = 5                  'Set volume for Beep1'
Beep1.Beep
Sleep(21)
Beep1.VOLUME_MUSIC = VolMus       'Restore setting'

The Compiler shows the following:
Compiling generated Java code. Error
B4A line: 5974
Beep1.VOLUME_MUSIC = 5
src\b4a\configulator_calculator\main.java:9066: error: cannot assign a value to final variable VOLUME_MUSIC
parent.mostCurrent._beep1.VOLUME_MUSIC = (int) (5);

I've searched the Forum but failed.
Thanks in advance

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
VOLUME_XXX are constants that are passed to Beeper.Initialize2. They set the voice channel.

You can se the volume with Phone.SetVolume. Make sure to wrap it in a Try / Catch block as there are cases where it will throw an exception.
Thanks Erel,

I will give it a try.
It seems an odd way to go when there is "XXXX.VOLUME_MUSIC".
I don't think Beep comes through the Voice Channel, but through the Music channel.

Regards Roger
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Beep generates a SINE or PULSE wave internally according to frequency and play it with a certain amplitude, so @Erel can add a new parameter as volume for example between 0 to 1 and multiply it to the wave samples generated to control the Beep volume. It is the tiniest thing for Erel to make but it will change the Audio library API.
 
Upvote 0
Top