public class Form1 extends ActionBarActivity { private MediaPlayer mp; Context context; ... setContentView(R.layout.activity_form1); if (mp==null) { context = this; mp = MediaPlayer.create(context, R.drawable.mainmenus); mp.setLooping(true); mp.start(); }; .... public void startbook(View view) { mp.pause(); mp.stop(); mp.reset(); mp.release(); mp = null; Intent intent = new Intent(Form1.this, Form2.class); startActivity(intent); } 

When you press the startbook button, the sound does not stop. And if the launch of the sound hang on something like:

 public void startmuz(View view) { mp = MediaPlayer.create(context, R.drawable.mainmenus); mp.setLooping(true); mp.start(); } 

The sound starts and stops normally. But! It is necessary that the sound starts playing when the activity is opened automatically. That with the above code does not work well, no way.

    2 answers 2

    Carefully check that mp not local in onCreate() and is not overridden anywhere else. In startbook() it should be the same as initialized in onCreate() . It is better to check the debugger, or output the value to the console via Log.v.

    In addition, it is better to start the player in onStart() and be sure to stop at onStop() (if it has not been stopped yet), otherwise there is a risk of losing it running. It may be better to bring the MediaPlayer to the service.

    And never do that:

     context = this; 
    • Helped mp announce as:. public static MediaPlayer mp = null; - Pavel Gribov

    Helped mp announce as:. public static MediaPlayer mp = null;