Android Question mediaplayer.setvolume not working

Startup

Active Member
Licensed User
Longtime User
I am trying to set the volume using the code below to max in an alarm however the "mp.SetVolume(1,1)" line does not work (does not change the volume to max)
I added the same code to the "SimpleAlarm" that SuSu wrote and it doesn't work there either.
I've attached a zip of SuSu's "SimpleAlarm" with the additional SetVolume line (to show the complete code)/
What am I doing wrong?

B4X:
Sub PlayAlarm
    mp.Load(File.DirAssets, "sound.wav")
    mp.SetVolume(1,1)
    mp.Play
End Sub
 

Attachments

  • alarmExport.zip
    236.5 KB · Views: 156

JohnC

Expert
Licensed User
Longtime User
The way I understand it, the MP volume is a "slave" to the devices "media" volume. So, if the devices media volume is low, no matter how high you set the MP.Setvolume it will be limited by what the user set the "Media" volume to.

To get the media to play the loudest, you will also need to adjust the devices "Media" channel volume.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are setting the volume for the MediaPlayer object not the device.

If you want to set the volume for the device it is something like:

B4X:
    Dim Ph As Phone
    Ph.SetVolume(Ph.VOLUME_MUSIC,Ph.GetMaxVolume(Ph.VOLUME_MUSIC),True)

Dependant on which channel you are playing the sound on.
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
You are setting the volume for the MediaPlayer object not the device.

If you want to set the volume for the device it is something like:

B4X:
    Dim Ph As Phone
    Ph.SetVolume(Ph.VOLUME_MUSIC,Ph.GetMaxVolume(Ph.VOLUME_MUSIC),True)

Dependant on which channel you are playing the sound on.

Thanks Steve! That works just using the code you wrote. Not sure what you mean by "Dependant on which channel you are playing the sound on" as it works with just what you wrote?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Please note that I am pretty sure that before you do the .Setvolume, you should do a .GetVolume and store the current user's value.

Then, when you are done playing the sound, return the volume back to the user's setting.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Try setting the volume as a float. I have a vague recollection of needing to do this for some reason.

B4X:
mp.SetVolume(1.0, 1.0)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Dependant on which channel you are playing the sound on

There are several possible channels that you can control the volume on, MediaPlayer defaults to the Music channel, but there are others:

1584034298907.png
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
Thanks, Erel! I see that is necessary. However, when I set the phone to "do not disturb" even tho the phone does not ring it does not print the Msg ("Reset Mode to hear alarm") with the code I've set. Is an Error not being thrown or is my code incorrect to catch it? (In Debug Mode the program skips over (does not run) the Catch section ).

B4X:
Sub PlayAlarm
    mp.Load(File.DirAssets, "xylophone.mp3")
    Dim Ph As Phone
    Try
        Ph.SetVolume(Ph.VOLUME_MUSIC,Ph.GetMaxVolume(Ph.VOLUME_MUSIC),True)
    Catch
        MsgboxAsync("Phone is set to 'Do Not Disturb Mode'", "Reset Mode to hear alarm")
    End Try
    mp.Play
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub PlayAlarm
    mp.Load(File.DirAssets, "xylophone.mp3")
    Dim Ph As Phone
    Try
        Ph.SetVolume(Ph.VOLUME_MUSIC,Ph.GetMaxVolume(Ph.VOLUME_MUSIC),True)
       Log("Worked")
    Catch
         Log("Didn't work")
        MsgboxAsync("Phone is set to 'Do Not Disturb Mode'", "Reset Mode to hear alarm")
    End Try
    mp.Play
End Sub
What is the output in DnD mode?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I did some testing and mp should play in every DnD mode except "Total Silence", which is by design.

What DnD mode is the device set to?

The below code can tell you:
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
Thanks, JohnC but the problem is not with the DnD mode which is working fine and has stopped the sound but the Try/Catch is not alerting the user to turn off the DnD mode so the alarm sounds and the user hears it.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
But what I'm saying is that maybe even when DnD is set to Total Silence, there may be no guarantee that it will throw an error.

So, I was just curious if you set the DnD to Alarm or Priority will the sound play?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I recently did a LOT of research into this, and I understand that when DnD is off it will play.

But as you can see below, there are many different DnD modes:
B4X:
   Select Ret
        Case 0
            Log("DnD: Off")
        Case 1
            Log("DnD: Priority Only")
        Case 2
            Log("DnD: Total Silence")
        Case 3
            Log("DnD: Alarms Only")
        Case -1
            Log("DnD: SettingNotFoundException occured ?")
    End Select

So, if your phone is in DnD mode "Total Silence", MP may not throw an error - it just wont play.

My device allows me to set which mode DnD goes into when I turn DnD on. I'll bet your device is set to go into "Total Silence" when in DnD mode and that's why it wont play.

Simply change your DnD mode to "Alarms Only" or "Priority Only" and MP should play when DnD is on.

Then have you app check the DnD mode instead of relying on an error to detect if MP will play or not.
 
Last edited:
Upvote 0

Startup

Active Member
Licensed User
Longtime User
I'm not concerned about my device and getting it to play the alarm. I'm concerned with getting the user's device to play the alarm if he has set the sound to DnD and the alarm is not sounding. The code is meant to send a msg to the user to turn off DnD. However, since it's not throwing an exception it doesn't send the message. The question is what code do I use to alert the user or overcome his setting the sound to DnD?
 
Upvote 0
Top