Android Question Phone volume buttons doesn't work in all activities

Discussion in 'Android Questions' started by serjio, Apr 5, 2017.

  1. serjio

    serjio Member Licensed User

    Hi, I'm working on App with some activities and a background music that start from MediaPlayer in Main Activity_Create.
    The problem is that the music volume can't be set in main activity by volume phone buttons while it works in all other activitie.
    Furthermore phone volume buttons doesn't work in an activity that contains webview and where I declared another MediaPlayer with a different audio.
    I've read some posts but nothing.

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    Dim p As Phone
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod(
    "setVolumeControlStream"Array(p.VOLUME_MUSIC))
     
    Dave O likes this.
  3. serjio

    serjio Member Licensed User

    Thanks for interest Erel.
    Nothing has changed neither in Main and webview activity.
    I've declared variables first in globals, then process globals just to try all.
    The problem exist in emulator and my phisical phone.

    This is my code for play audio in Activity_create

    Code:
    Public audioBg As MediaPlayer  'process globals
    audioBg.Initialize2("audioBg")
    audioBg.Load(
    File.DirAssets, "app-bg.mp3")
    audioBg.Looping = 
    True
    audioBg.SetVolume(
    0.2 , 0.2)
     
    Last edited: Apr 5, 2017
  4. serjio

    serjio Member Licensed User

    I've noticed that volume buttons doesn't work in the activity where I declared and initialized the MediaPlayer type to load a bg sound. Same in the Activity that contains only a webview
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Are you calling this code after you initialize MediaPlayer?
     
  6. serjio

    serjio Member Licensed User

    Yes, I've tried all without results.
    Because the problem is only in two activities I've compared with more attention both codes and finded that the problem is caused by following code:
    Code:
    Sub Activity_KeyPress (KeyCode As Int) As Boolean
            
    If KeyCode = KeyCodes.KEYCODE_BACK Then
                audioBg.Stop
                
    Activity.Finish
            
    End If
            
    Return True
    End Sub
    Disabling this audio volume buttons works properly.
    It's the first time that I program software from scratch, maybe I wrote this not in the right way.
    There's a way to prevent this in Activity_KeyPress?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can handle the volume buttons with Activity_KeyUp.
     
    serjio likes this.
  8. serjio

    serjio Member Licensed User

    It works! Thank you Erel and thanks for B4A :)
     
    Erel likes this.
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