播放时增加媒体播放器的音量

时间:2018-12-15 19:45:28

标签: android android-mediaplayer volume

我想制作一个特殊的警报应用程序,并且用户应该有可能随着音乐变大而被唤醒,例如60秒。 我找不到解决办法,这就是为什么我需要您的帮助。

谢谢您的帮助,对不起我的英语不好

final AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    oldvolume=am.getStreamVolume(AudioManager.STREAM_MUSIC);
    am.setStreamVolume(AudioManager.STREAM_MUSIC,100,0);
    if (increase<=0)  mediaPlayer.setVolume(volume,volume);
    mediaPlayer.start();
    if (increase>0){
        mediaPlayer.setVolume(0,0);
        final double hohe=volume/increase;
        new CountDownTimer((increase*1000),1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mediaPlayer.pause();
                mediaPlayer.setVolume((float) hohe*millisUntilFinished,(float) hohe*(increase*1000-millisUntilFinished));
                mediaPlayer.start();
            }
            @Override
            public void onFinish() {
                mediaPlayer.pause();
                mediaPlayer.setVolume( volume,volume);
                mediaPlayer.start();
            }
        }.start();

那是我到目前为止所得到的。 增加=以秒为单位的增加时间 volume =最大音量警报应具有

1 个答案:

答案 0 :(得分:0)

解决方案,对我有用的是:

   private Handler mHandler = new Handler();
private Runnable mVolumeRunnable = new Runnable() {
    @Override
    public void run() {
        if (mediaPlayer != null&&currentvolume<endvolume) {
            currentvolume += volumeincrease;
            mediaPlayer.setVolume(currentvolume/100f, currentvolume/100f);
            Toast.makeText(AlertActivity.this,String.valueOf(currentvolume),Toast.LENGTH_SHORT).show();
            mHandler.postDelayed(mVolumeRunnable, 1000);
        }
        else mHandler.removeCallbacks(mVolumeRunnable);
    }
};