Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop the Media Player when the home button is hit
    primarykey
    data
    text
    <p>In my app in my main activity I have a media player variable that is set to loop. When I hit the home button the music continues to play. What I would rather have happen is the music shut off when the user hits the home button. From what I've gathered the home button can't be overridden, and I tried onPause() but when I would click any of my image buttons the music media player would stop. Is there a work around to this? Thanks, and let me know if you need my code.</p> <p>MainActivity:</p> <pre><code>package com.crazycastles; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ static MediaPlayer mainSound; @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.main); mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen); mainSound.setLooping(true); mainSound.start(); //CREATE BUTTON 1 &amp; SOUND final MediaPlayer buttonSound = MediaPlayer.create( MainActivity.this, R.raw.swords); ImageButton button1 = (ImageButton) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { buttonSound.start(); startActivity(new Intent(MainActivity.this, button1Activity.class)); } }); ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton); multiplayerbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { buttonSound.start(); startActivity(new Intent(MainActivity.this, multiplayerbuttonActivity.class)); } }); ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton); optionsbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { buttonSound.start(); startActivity(new Intent(MainActivity.this, optionsActivity.class)); } }); ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton); exitbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { buttonSound.start(); finish(); mainSound.stop(); System.exit(0); } }); //END OF BUTTON1 &amp; SOUND } public void onClick(View v) { // TODO Auto-generated method stub } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { mainSound.pause(); //stop media player return true; } return super.onKeyDown(keyCode, event); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload