3

I am new to android and I'm trying to develop an app where I take two music file. When the app starts the first music plays nd when the user clicks a button the second music plays and the first music has to fade out slowly.

I have tried it but i ended up with playing two music files one after the other. I am not able to get how to bring the fade out effect.

Here is my sample code

package com.Audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class DemoAudio extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MediaPlayer mp=MediaPlayer.create(getBaseContext(), R.raw.fruit_dance);
        mp.start();
        mp.setVolume(1.0f, 1.0f);

        Button btn=(Button) findViewById(R.id.xBtn);
        btn.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.futurebells_full);
                    mp1.start();
                    mp1.setVolume(5.0f, 5.0f);
                }
                return true;
            }
        });
     }

}

Any help will be appreciated. Thanks a lot.

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
Lavanya
  • 3,903
  • 6
  • 31
  • 57

3 Answers3

2

I got the answer for it. I have created two separate objects for MediaPlayer and by using some methods i have adjusted their volumes the first music to lower the pitch and the second one to higher the pitch and this is what i wanted.

This can be achieved well by using threads, it seems to work more better than by setting volumes directly.

Lavanya
  • 3,903
  • 6
  • 31
  • 57
1

see it fade-in-out I think it similar to your issue .

Community
  • 1
  • 1
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1

Two files can be played simultaneously if Java Threads are used. Start your MediaPlayer objects in two different threads. Always use threads for non-UI related stuff.

Shumon Saha
  • 1,415
  • 6
  • 16
  • 33