3
我有一個鬧鐘類,可以按時播放默認的鬧鐘鈴聲。我想在一段時間內播放鬧鐘鈴聲,音量從最小到最大。我怎樣才能從最小值到最大值逐漸增加音量。樣本代碼或項目是否有鏈接?請幫我Android鬧鐘音量逐漸增加
我有一個鬧鐘類,可以按時播放默認的鬧鐘鈴聲。我想在一段時間內播放鬧鐘鈴聲,音量從最小到最大。我怎樣才能從最小值到最大值逐漸增加音量。樣本代碼或項目是否有鏈接?請幫我Android鬧鐘音量逐漸增加
你可以自己做。沒有什麼複雜的。
一旦你的PendingIntent
被觸發,請利用處理程序來增加。
創建類,將採取AudioManager
或應用作爲參數的Context
。
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.
}
}
}
調用此Runnable
一旦PendingIntent
被觸發:
someHandler.post(新VolumeRunnable(mAudioManager,someHandler));
我希望這給你的想法。原諒任何錯誤或錯別字。
您錯過了: Handler someHandler = new Handler(); 之前: someHandler.post(新VolumeRunnable(mAudioManager,someHandler)); 只是爲他人評論。 – jclova