Android:逐渐增加通知声音的音量

时间:2016-05-05 23:45:01

标签: android

我有一个通知,我想逐渐增加声音,直到它被解雇/回答。我怎么能这样做?谢谢大家的帮助!

1 个答案:

答案 0 :(得分:-1)

这就是我执行此任务的方式。首先,我捕获原始设置。毕业后,一旦用户驳回通知,我就会调用另一个函数来返回设置。这是毕业声音的代码:

 public static void graduateVolumeLevel(Context context) {
    getVolumeLevel(context);
    final AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    final int DELAY_UNTIL_NEXT_INCREASE = 6 * 1000;

    LaunchService.startThread();

    mHandlerIncreaseVolume = new Handler(appService.thread.getLooper()); 
   /*you may not need this part.
    I use it b/c i run my stuff on another thread not to 
   interfere with the main thread. 
   Creating a new handler on main thread may be sufficient for your needs.*/

   if(runnable==null) {
       runnable = new Runnable() {
           @Override
           public void run() {
               int currentSystemVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
               int currentRingVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);

               if (currentSystemVolume != mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)||
                       currentRingVolume != mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING)) { //if not max
                   //increase the volume of the alarm stream 
                   mAudioManager.setStreamVolume(AudioManager.STREAM_RING,currentRingVolume++,0);
                   mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, currentRingVolume++, 0);

                   if(mHandlerIncreaseVolume!=null)
                   mHandlerIncreaseVolume.postDelayed(this, DELAY_UNTIL_NEXT_INCREASE);
               }
           }
       };
   }

    mHandlerIncreaseVolume.post(runnable);
}

我发现了一个类似的帖子但是再也找不到它了。这是我用来创建我的代码,我稍作修改,因为这个例子没有完全正常工作。一旦我返回原始设置,我从处理程序中删除回调并将其设置为null。