Problems with MusicPlayer on Android (Beginner)

I have a problem with an app I am building.

Basically, I am trying to play music in the background of my application, what can I do and it works great. However, when the user navigates to another screen, the music restarts, rather than just continuing as usual.

Here is all the code I have:

public class MainActivity extends Activity {    


MediaPlayer mp;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //setting the layout

    mp = MediaPlayer.create(this, R.raw.song);
    mp.setLooping(true);
    mp.getDuration();
    mp.start();

      

Then I get this method at the end of this class to stop the music when the app is closed:

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();


    mp.release();
    finish();

}

      

I'm not sure what to include in my other classes to keep the music playing. If anyone could show me I would be very grateful.

Thank.

+3


source to share


2 answers


You have made your Mediaplayer instance dependent on Activity. So when the activity is restarted, onCreate () is called again and the music restarts. If you want to continue playing music, take mp out of any Activity instance and place it inside the Service. Then start the Service from your MainActivity and let it play music if you don't stop the service.

  public class MainActivity extends ListActivity {




    /** Called when the activity is first created. */

Intent i; 
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.splash);
       i=new Intent(this, MyMusicService.class);
       final ToggleButton togglebutton =(ToggleButton)findViewById(R.id.toggleButton1);
      togglebutton.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {   
              // Perform action on clicks    
              if (togglebutton.isChecked()) {   
                  Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();  
                  startService(i);  
              } else {   
                  stopService(i);
                  Toast.makeText(MainActivity.this, "Not checked", Toast.LENGTH_SHORT).show(); 
                      }    }});





}
@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
stopService(i);
}
}

      



The service is as follows:

   public class MyMusicService extends Service {
    MediaPlayer mp;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     mp = MediaPlayer.create(this, R.raw.song);


        mp.start();
mp.setLooping(true);

    return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(mp!=null)
        mp.release();
    mp=null;
}
}

      

+2


source


Place mp.release () method in onDestroy method not in onPause () method. Caz, every time a new activity transitions to the interface, the existing activity goes into pause state.



0


source







All Articles