Frage

I have an Alarm class which plays the default alarm tone on time. I want to make the alarm tone play, volume from minimum to maximum within a time. How can i increase the volume gradually from minimum to maximum. Is there any links for sample codes or projects. please help me

War es hilfreich?

Lösung

You can do it by yourself. Nothing complicated in it.

Once your PendingIntent is triggered, make use of handlers to do the increasing.

  1. Create class that will take the AudioManager or Context of the app as argument.

      public class VolumeRunnable implements Runnable{
    
    private AudioManager mAudioManager;
    private Handler mHandlerThatWillIncreaseVolume;
    private final static int DELAY_UNTILL_NEXT_INCREASE = 30*1000;//30 seconds between each increment
    VolumeRunnable(AudioManager audioManager, Handler handler){
       this.mAudioManager = audioManager;
       this.mHandlerThatWillIncreaseVolume = handler;
    }
    
    @Override
    public void run(){
       int currentAlarmVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
       if(currentAlarmVolume != mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM)){ //if we havent reached the max
    
       //here increase the volume of the alarm stream by adding currentAlarmVolume+someNewFactor
            mHandlerThatWillIncreaseVolume.postDelayed(this,DELAY_UNTILL_NEXT_INCREASE); //"recursively call this runnable again with some delay between each increment of the volume, untill the condition above is satisfied. 
       }
    
    }
    

    }

  2. Call this Runnable once the PendingIntent is triggered:

someHandler.post(new VolumeRunnable(mAudioManager, someHandler));

I hope this gave you idea. Forgive any mistakes or typos.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top