Pregunta

I manage to add background music to my apps, but when i close it, it doesn't stop. When I click on HOME, or BACK button, it still plays music. Does anyone knows how to solve that problem?

MediaPlayer bsound;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    MediaPlayer bsound = MediaPlayer.create(this, R.raw.sample);
    bsound.setLooping(true); // Set looping
    bsound.setVolume(100,100);
    bsound.start();

    TextView txt = (TextView) findViewById(R.id.textView1);  
    Typeface font = Typeface.createFromAsset(this.getAssets(), "Schalk.ttf");  
    txt.setTypeface(font); 

    TextView txt1 = (TextView) findViewById(R.id.button1);  
    Typeface font1 = Typeface.createFromAsset(this.getAssets(), "Schalk.ttf");  
    txt1.setTypeface(font1);            
}

public void list(View view) {
    Intent intent = new Intent(this, Menus.class);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}
¿Fue útil?

Solución

Try to pause and resume MediaPlayer:

MediaPlayer bsound;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    bsound = MediaPlayer.create(this, R.raw.sample);
    bsound.setLooping(true); // Set looping
    bsound.setVolume(100, 100);
    bsound.start();

    TextView txt = (TextView) findViewById(R.id.textView1);
    Typeface font = Typeface.createFromAsset(this.getAssets(), "Schalk.ttf");
    txt.setTypeface(font);

    TextView txt1 = (TextView) findViewById(R.id.button1);
    Typeface font1 = Typeface.createFromAsset(this.getAssets(), "Schalk.ttf");
    txt1.setTypeface(font1);
}

@Override
protected void onPause() {
    if (bsound.isPlaying()) {
        bsound.pause();
    }
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    bsound.start();
}

And yes, you didn't initialize that MediaPlayer bsound field. Change this line:

MediaPlayer bsound = MediaPlayer.create(this, R.raw.sample);

to this:

bsound = MediaPlayer.create(this, R.raw.sample);

Otros consejos

You should implement onStop() for your activity and stop your music playing in this method. This will stop it whenever your activity closes. Look here for a bit more info

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

Call pause() method in onPause() in activity and start in onStart() method.

Copy below code and paste AndroidManifest.xml file in under First Activity Tag.

<activity                        
            android:name="com.FirstActivity"
            android:clearTaskOnLaunch="true" 
            android:launchMode="singleTask"
            android:excludeFromRecents="true">              

        </activity>     

Also Add this below code in all under Activity Tag in AndroidManifest.xml file

 android:finishOnTaskLaunch="true"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top