Play two audio files in android

I am new to android and I am trying to develop an application in which I take two music files. When the application starts the first music, when the user presses the button, the second music is played, and the first music slowly disappears.

I tried this, but in the end I played two music files one by one. I cannot figure out how to deduce the effect of fading out.

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 would be appreciated. Many thanks.

+3
source share
3 answers

I got an answer. I created two separate objects for MediaPlayer and, using some methods, I adjusted their volumes with the first music to lower the pitch, and the second higher, and this is what I wanted.

, , , .

+2
+1

Two files can play simultaneously if Java streams are used. Start MediaPlayer objects in two different threads. Always use streams for non-UI materials.

+1
source

All Articles