0

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 ringtone for alarm. here is my code for playing alarm sound

 try {
                        if(mp==null){
                System.out.println("-------mp is null now------------");
                Uri myUri = Uri.parse("android.resource://com.android.crazy/raw/airtel");
           mp=MediaPlayer.create(Alarm.this, myUri);
            }
mp.setDataSource(songName);
            mp.prepareAsync();
            mp.setLooping(true);

        } catch (Exception e) {
            e.printStackTrace();
        } 
      mp.start();

Here i need to give the sound fadein functionality. please advise me how to put the sound fade in for a particular amount of time like 5 mins

Thanks in advance

raghumudem
  • 627
  • 1
  • 9
  • 17
  • 1
    Kindly refer this link. http://stackoverflow.com/questions/6884590/android-how-to-create-fade-in-fade-out-sound-effects-for-any-music-file-that-my –  Sep 02 '11 at 13:20

2 Answers2

0

I revised @FoamyGuy's answer to avoid compiler and runtime errors and for brevity and clarity:

// (At this point mp has been created and initialized)
public float mpVol = 0f;
mp.setVolume(mpVol, mpVol);
mp.start();

final Handler h = new Handler();
h.post(new Runnable() {
    public void run() {
        if (mp.isPlaying()) {
            if (mpVol < 1.0f) {
                mpVol += .05f;
                mp.setVolume(mpVol, mpVol);
                h.postDelayed(this, 500);
            }
        }
    }
});
CODE-REaD
  • 2,153
  • 3
  • 25
  • 54
0

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.

FoamyGuy
  • 45,328
  • 16
  • 118
  • 151
  • This caused compiler error: ***local variable rightVol is accessed from within inner class; needs to be declared final.*** I resolved this error by moving leftVol and rightVol within `Runnable increaseVol`, still had compiler error ***variable increaseVol might not have been initialized***. So I changed `Runnable increaseVol = new Runnable()` to `h.postDelayed(new Runnable()...}, 500);` and `h.postDelayed(increaseVol, 500)` to h.postDelayed(this, 500)` and got rid of `h.post(increaseVol)`. This compiles w/1.8.0_45 & runs w/Kitkat. See http://stackoverflow.com/a/20784353/5025060. – CODE-REaD Jun 09 '16 at 02:10