hello all,
I am working on android project. i have started background music in one of my activity named"myMainScreen.java"
now on that activity i have put one button.
while clicking on button i will redirected to another activity called"page_play.java" through intent.
Music will get stopped on the second activity(page_play).

but i want music in continuation, what changes i have to do in my current code.
my current code is given below.

public class myMainScreen extends Activity {

    MediaPlayer mp;
    AssetFileDescriptor asd;
    Button b1,b2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        b1 = (Button)findViewById(R.id.btn_play);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(myMainScreen.this,page_play.class);
                startActivity(i);
            }
        });

   try {
            asd = getAssets().openFd("Background_Music.mp3");
            mp = new MediaPlayer();
            mp.setDataSource(asd.getFileDescriptor(), asd.getStartOffset(),asd.getLength());
            mp.setLooping(true);
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void onPause()
    {
        super.onPause();
        mp.pause();
    }
}

Recommended Answers

All 2 Replies

Thanks peter.It is very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.