how to get the sound fade in functionality in alarm application using media player in android

前端 未结 2 818
旧巷少年郎
旧巷少年郎 2021-01-26 10:41

Hi i am new to android. I am developing a application with alarm functionality. Here i need to provide the sound fade in functionality. I am using media player to invoke the rin

2条回答
  •  伪装坚强ぢ
    2021-01-26 10:53

    you can use this method:

    setVolume(float leftVolume, float rightVolume);
    

    to create "fade in" effect you'll just start at zero and keep setting it a little bit higher at regular intervals. Some thing like this will increast the volume by 5% every half of a second

                final Handler h = new Handler();
                float leftVol = 0f;
                float rightVol = 0f;
                Runnable increaseVol = new Runnable(){
                    public void run(){
                        mp.setVolume(leftVol, rightVol);
                        if(leftVol < 1.0f){
                            leftVol += .05f;
                            rightVol += .05f;
                            h.postDelayed(increaseVol, 500);
                        }
                    }
                };
    
    
                h.post(increaseVol);
    

    you can control how much it gets raised each tick by changing what you add to leftVol and rightVol inside the if statement. And you can change how long each tick takes by changing the 500 parameter in the postDelayed() method.

提交回复
热议问题