Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Saving music state (beginner)
    primarykey
    data
    text
    <p>I am having an issue with the music playback of my app. I set up the media player within a Service class which plays music throughout the application fine. However, the issue I'm having is when changing screens from the Options page to the Home page again. </p> <p>In the option class, I have a single ToggleButton which turns the music on and off when it's clicked, and I have used a boolean to determine whether the ToggleButton is auto on and off, depending on whether the music is playing. </p> <p>However, when the user clicks the back button or the save button (which both send them back to the home page) the music starts playing again, even if it's checked as "off". </p> <p>Does anyone have a suggestion for me? </p> <p><strong>Service Class:</strong></p> <pre><code>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) { return null; } @Override public void onDestroy() { super.onDestroy(); if(mp!=null) { mp.stop(); mp.release(); } mp=null; } </code></pre> <p>}</p> <p><strong>Options Class:</strong></p> <pre><code>public class OptionsActivity extends Activity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK ) { Intent i = new Intent(OptionsActivity.this, MainActivity.class); startActivity(i); return true; } return super.onKeyDown(keyCode, event); } private boolean isMyServiceRunning(String serviceCanonicalClassName) { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceCanonicalClassName.equals(service.service.getClassName())) { return true; } } return false; } Intent i; // Handles MyMusicService.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.options); final TextView tSound = (TextView) findViewById(R.id.textView2); final Button saveBtn = (Button) findViewById(R.id.optSaveBtn); final Button tblBtn = (Button) findViewById(R.id.tableBtn); i=new Intent(this, MyMusicService.class); final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref); boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName()); soundOption.setChecked(musicPlays); if(musicPlays==true){ tSound.setText("On"); } if(musicPlays==false) { tSound.setText("Off"); } soundOption.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on clicks to control sound being on and off. if(soundOption.isChecked()) { Toast.makeText(OptionsActivity.this, "Music on.", Toast.LENGTH_SHORT).show(); startService(i); Intent i = new Intent(OptionsActivity.this, OptionsActivity.class); startActivity(i); } else { if(stopService(i)==true){ soundOption.setChecked(false); stopService(i); Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show(); Intent i = new Intent(OptionsActivity.this, OptionsActivity.class); startActivity(i); } } } }); tblBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent tblView = new Intent(OptionsActivity.this, SQLView.class); startActivity(tblView); } }); saveBtn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { Intent homePage = new Intent(OptionsActivity.this, MainActivity.class); switch (v.getId()){ case R.id.optSaveBtn: //Determine what will happen when the user presses the "Submit button". boolean optionsWork = true; try{ String sound = tSound.getText().toString(); optionsDB entry = new optionsDB(OptionsActivity.this); //Creating a new instance of MasterMind game entry.open(); entry.createOptionEntry(sound); //Passing both strings entry.close(); }catch (Exception e){ //Creating an error message if for some reason the app cannot transfer data to the Database. Toast.makeText(OptionsActivity.this, "Error", Toast.LENGTH_SHORT).show(); } finally { //Creating an AlertDialog box when the user presses the Submit button. if (optionsWork){ Toast.makeText(OptionsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show(); } } break; } startActivity(homePage); } }); } } </code></pre> <p><strong>And finally, the home class:</strong></p> <pre><code>public class MainActivity extends Activity { Intent i; private boolean isMyServiceRunning(String serviceCanonicalClassName) { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceCanonicalClassName.equals(service.service.getClassName())) { return true; } } return false; } public void checkSound(){ boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName()); if(musicPlays==true){ //Do nothing } else { stopService(i); } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //setting the layout //Start Networking //Intent net = new Intent(this, Networking.class); //startService(net); //Auto starts playing music on app launch. i = new Intent(this,MyMusicService.class); startService(i); checkSound(); final ImageView findGame = (ImageView) findViewById(R.id.btnFindGame); final ImageView profile = (ImageView) findViewById(R.id.btnProfile); final ImageView instructions = (ImageView) findViewById(R.id.btnInstructions); final ImageView options = (ImageView) findViewById(R.id.btnOptions); findGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DrawActivity.class); startActivity(intent); } }); profile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, WaitingActivity.class); startActivity(intent); } }); instructions.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, InstructionsActivity.class); startActivity(intent); } }); options.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, OptionsActivity.class); startActivity(intent); } }); } //On app close, music stops playing. @Override protected void onDestroy() { if (this.isFinishing()){ super.onDestroy(); stopService(i); //finish(); } } </code></pre> <p>}</p> <p>Any help would be very much appreciated, thank you. </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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