Android Question reduce volume of mp3 player in the background

MMB1979

Member
Licensed User
Longtime User
Hello everybody,

I am looking for a solutions since days here in the forum, and I have tried already a lot of code snippets, but none of them worked.

I am currently playing around with a Sports App, which is a talking feature. While the App is talking it shall reduce the volume of the MP3 Player in the background. I can handle the volumen of the phone and the media player of my app, but I cannot handle the volumen of other apps in the background.

Is there someone who has an idea?

Thank you in advance!
Markus
 

MMB1979

Member
Licensed User
Longtime User
Hallo Erel,

I just know 6 of them:

B4X:
Phone.SetVolume(Phone.VOLUME_MUSIC, Phone.GetMaxVolume / 10, False)
Phone.SetVolume(Phone.VOLUME_ALARM, Phone.GetMaxVolume / 10, False)
Phone.SetVolume(Phone.VOLUME_NOTIFICATION, Phone.GetMaxVolume / 10, False)
Phone.SetVolume(Phone.VOLUME_RING, Phone.GetMaxVolume / 10, False)
Phone.SetVolume(Phone.VOLUME_SYSTEM, Phone.GetMaxVolume / 10, False)
Phone.SetVolume(Phone.VOLUME_VOICE_CALL, Phone.GetMaxVolume / 10, False)

But even if I reduce the volume of all and set the Media Player to MAX - MediaPlayer.SetVolume(1, 1), that makes no difference. Due to the phone volume also the App Volume is too low. And if I increase the System or Music volume, then the App has again the same volume as the MP3 Player in the background.
 
Last edited:
Upvote 0

MMB1979

Member
Licensed User
Longtime User
thx for the Link! I tried to use following code - but still the whole "music" is at 1/10 of the volume...

B4X:
Dim mp As MediaPlayer
    mp.Initialize2("MediaPlayer")
    mp.looping=False

'this should reduce the volume of the mp3 player in the background
Dim ph As Phone
    ph.SetVolume(ph.VOLUME_MUSIC, ph.GetMaxVolume(ph.VOLUME_MUSIC) / 10, False)

'this should play my mp3 file at max volume
Dim rf As Reflector
    rf.Target = mp
    rf.Target = rf.GetField("mp")
    rf.RunMethod2("setAudioStreamType", rf.GetStaticField("android.media.AudioManager", "STREAM_MUSIC"), "java.lang.int")
    mp.Load(File.DirAssets, "hello.mp3")
    mp.SetVolume(1, 1)
    mp.play
 
Upvote 0

MMB1979

Member
Licensed User
Longtime User
THX Erel!!! It works!

But you should better not be in vibration ringer mode - otherwise you will not hear anything. You could use the Alarm stream instead, but that would mean that you would not hear it through the headphone but the phone speaker!

However, if you change the ringer whilst playing your sounds it works. WOuld be cool if I could prevent from vibrating, if I restore the ringer mode to vibration.

Here is the code:

B4X:
Sub Button2_Click

    handleVolume

Dim rf As Reflector
    rf.Target = mp
    rf.Target = rf.GetField("mp")
    rf.RunMethod2("setAudioStreamType", rf.GetStaticField("android.media.AudioManager", "STREAM_SYSTEM"), "java.lang.int")
    mp.Load(File.DirAssets, "5.mp3")
    mp.setVolume(1, 1)
    mp.play

End Sub

Sub mp_Complete
    RestoreVolume
   
End Sub

Sub BackupVolume
    phvol1 = ph.getVolume(ph.VOLUME_MUSIC)
    phvol2 = ph.getVolume(ph.VOLUME_SYSTEM)

End Sub

Sub RestoreVolume
    ph.setVolume(ph.VOLUME_MUSIC, phvol1, False)
    ph.setVolume(ph.VOLUME_SYSTEM, phvol2, False)
   
End Sub

Sub handleVolume
    ph.setVolume(ph.VOLUME_MUSIC, ph.GetMaxVolume(ph.VOLUME_MUSIC) / 10, False)
    ph.setVolume(ph.VOLUME_SYSTEM, ph.GetMaxVolume(ph.VOLUME_SYSTEM), False)

End Sub

Sub BackupRinger
    phmod = ph.GetRingerMode
    ph.SetRingerMode(ph.RINGER_NORMAL)
   
End Sub

Sub RestoreRinger
    ph.SetRingerMode(phmod)
   
End Sub
 
Upvote 0
Top