kobihudson
kobihudson

Reputation: 165

Hardware volume buttons are not working when my app is running

I have been working on an app that plays some audio. I am currently playing the audio as a STREAM_MUSIC type of stream and this works just fine. I want to be able to control the volume with the hardware volume controls on the device. When I am in the app the hardware buttons don't do anything, the volume doesn't change and the toast doesn't pop up. I then press the home button so the app is running in the background the hardware volume buttons work. They only work when my app is running in the background.

I have tried to use the code this.setVolumeControlStream(AudioManager.STREAM_MUSIC); in my onCreate() method and this did not change the apps behavior and I am still facing the same problems.

I also have been testing this on a DROID 2, DROID RAZR, Samsung Galaxy s3, Samsung Galaxy s4, Lenovo tablet, and a rooted DROID 2 but they all behave the same.

 public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    setVolumeControlStream(audio.STREAM_MUSIC); //this line should set up the hardware
    //buttons to control the volume 
    if (QtApplication.m_delegateObject != null && QtApplication.onCreate != null) {
        QtApplication.invokeDelegateMethod(QtApplication.onCreate, savedInstanceState);
        return;
    }

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    try {
        m_activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        finish();
        return;
    }

    if (null == getLastNonConfigurationInstance()) {
        // if splash screen is defined, then show it
        if (m_activityInfo.metaData.containsKey("android.app.splash_screen") )
            setContentView(m_activityInfo.metaData.getInt("android.app.splash_screen"));
        startApp(true);
    }
}

Upvotes: 1

Views: 2049

Answers (1)

Dyna
Dyna

Reputation: 2305

You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge. Android provides the handy setVolumeControlStream() method to direct volume key presses to the audio stream you specify.

Take a look at this, I think it might be helpful:
Use Hardware Volume Keys to Control Your App’s Audio Volume

EDIT:

on your oncreate method you have to do:

 this.setVolumeControlStream(AudioManager.STREAM_MUSIC); //if you use AudioManager.STREAM_MUSIC to load the sound
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId,
          int status) {
        loaded = true;
      }
    });
   soundPool.load(this, R.raw.sound1, 1); // your sound

Upvotes: 2

Related Questions