0

I want to make a button onclick to fade out the music and call the media player to stop?

I want this to be over 5 sec

I have tried to make a timer like code, but i dont get how to make it work - can you guys help me?

float volume = 1;
float speed = 0.05f;

    public void FadeOut(float deltaTime); 
{ 
mpMain.setVolume(1, 1); 
volume -= speed* deltaTime; } 
public void FadeIn(float deltaTime1); 
{ 
mpMain.setVolume(0, 0); 
volume += speed* deltaTime1; } 


}}
ZimZux
  • 29
  • 5
  • Show the code that you've tried and why it didn't work please – Cruncher Sep 27 '13 at 20:28
  • ive tried a bunch of codes, and delete it when it didnt work and startet over... the latest is .... public void FadeOut(float deltaTime); { mpMain.setVolume(1, 1); volume -= speed* deltaTime; } public void FadeIn(float deltaTime1); { mpMain.setVolume(0, 0); volume += speed* deltaTime1; } Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FadeOut. }} – ZimZux Sep 27 '13 at 20:29
  • http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float) setVolume with a timer should probably be pretty good I think – Cruncher Sep 27 '13 at 20:30
  • `public void FadeOut(float deltaTime);` what's this? Why is there a semi-colon after the method declaration? Can you post the code formatted in the question please – Cruncher Sep 27 '13 at 20:33
  • @ZimZux, please edit your question to contain your code instead of posting the code in the comments. – Bryan Herbst Sep 27 '13 at 20:35
  • http://stackoverflow.com/questions/6884590/android-how-to-create-fade-in-fade-out-sound-effects-for-any-music-file-that-my ... was the latest page i used to try figuring this out – ZimZux Sep 27 '13 at 20:35
  • Again, post that in the question. It's relevant. With the code that you did to try to implement the solution. – Cruncher Sep 27 '13 at 20:37

1 Answers1

1

The difference between

Android: How to create fade-in/fade-out sound effects for any music file that my app plays?

and the code that you posted, is that you set the volume directly to 1, and ignore the volume variable that you're changing. Instead use the volume variable exactly as they do in the solution to the other question. This also needs to be called several times via a timer.

deltatime should be the amount of time that passed since the last time the method was called. This is to keep the fade consistent if the phone lags or something.

Community
  • 1
  • 1
Cruncher
  • 7,241
  • 1
  • 26
  • 62