Phone Music Volume

jpvniekerk

Active Member
Licensed User
Longtime User
I am trying to intercept the phone volume setting (for music), set it to the max to play a warning through mediaplayer, then reset the phone volume to it's previous level. But It's not working as I think it should.

In Sub Process_Globals I have
B4X:
Dim MP1 As MediaPlayer
Dim Ph As Phone
Code for setting the volume and playing the warning is:
B4X:
Sub PlayWarningSound
   Dim PhVol,MaxVol As Float
   PhVol = Ph.GetVolume(Ph.VOLUME_MUSIC)             'get current music channel volume
   MaxVol = Ph.GetMaxVolume(Ph.VOLUME_MUSIC)      'get max music channel volume
   Ph.SetVolume(Ph.VOLUME_MUSIC,MaxVol,False)      'set phone music channel to max volume
   DoEvents
   MP1.SetVolume(DDVol/100,DDVol/100)
   MP1.Play
   Ph.SetVolume(Ph.VOLUME_MUSIC,PhVol,False)        'set phone music channel volume back to original
   DoEvents
End Sub
DDVol is a value set by the user between 0 and 100.

When I run sub PlayWarningSound, the volume is still dependent on the phone's music channel volume (set manually before the sub) - i.e. if the music channel is set to lowest, I don't hear anything, but if the phone volume is at say 50%, I only hear the warning at 50% - even if DDVol is 100.

It seems that "Ph.SetVolume(Ph.VOLUME_MUSIC,MaxVol,False)" does not have any effect here. I've added the "DoEvents", but still no luck.

Am I doing something wrong?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should remove the DoEvents calls. They will not help.

MediaPlayer.Play is an asynchronous call. It doesn't block until the playback is completed.

You should set the volume to the maximum level and return it to the previous level in MediaPlayer_Complete event. Note that you should call MediaPlayer.Initialize2 to intercept this event (see the documentation).
 
Upvote 0
Top