Android Question reduce volume of mp3 player in the background

Discussion in 'Android Questions' started by MMB1979, Feb 25, 2015.

  1. MMB1979

    MMB1979 Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The Phone library allows you to set the volume of the 7 different volume channels (Phone.SetVolume).
     
  3. MMB1979

    MMB1979 Member Licensed User

    Hallo Erel,

    I just know 6 of them:

    Code:
    Phone.SetVolume(Phone.VOLUME_MUSIC, Phone.GetMaxVolume / 10False)
    Phone.SetVolume(Phone.VOLUME_ALARM, Phone.GetMaxVolume / 10False)
    Phone.SetVolume(Phone.VOLUME_NOTIFICATION, Phone.GetMaxVolume / 10False)
    Phone.SetVolume(Phone.VOLUME_RING, Phone.GetMaxVolume / 10False)
    Phone.SetVolume(Phone.VOLUME_SYSTEM, Phone.GetMaxVolume / 10False)
    Phone.SetVolume(Phone.VOLUME_VOICE_CALL, Phone.GetMaxVolume / 10False)
    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: Feb 26, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. MMB1979

    MMB1979 Member Licensed User

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

    Code:
    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) / 
    10False)

    '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(
    11)
        mp.play
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You need to use a different channel. For example STREAM_SYSTEM.
     
  7. MMB1979

    MMB1979 Member Licensed 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:

    Code:
    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(
    11)
        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) / 
    10False)
        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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice