-1

I got the switch and new layout to work, thank you everyone. One last question though. I would like the music to stop playing if the user presses the back or home button. This is the code, I do not know what I am missing.

package com.androidsleepmachine.gamble;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ocean extends Activity implements View.OnClickListener {
 public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button1;
public Spinner spinner1;

// Initialize the activity
@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.ocean);

    button1 = (Button) findViewById(R.id.btn1);
    button1.setOnClickListener(this);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,  
android.R.layout.simple_spinner_item, TIME_IN_MINUTES);          
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);          
    spinner1.setAdapter(adapter); 
}

// Play the sound and start the timer
private void playSound(int resourceId) {
    // Cleanup any previous sound files
    cleanup();
    // Create a new media player instance and start it
    mediaPlayer = MediaPlayer.create(this, resourceId);
    mediaPlayer.start();
    // Create the timer to stop the sound after x number of milliseconds
    int selectedTime = TIME_IN_MINUTES[spinner1.getSelectedItemPosition()];
    handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn1:
            playSound(R.raw.ocean_birds);
            break;
    }
}

// Stop the sound and cleanup the media player
public void cleanup() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }
    // Cancel any previously running tasks
    handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
    public void run() {
        cleanup();
    }
};
}

Logcat

09-18 00:28:59.000: E/AndroidRuntime(1627): android.app.SuperNotCalledException:  
Activity {com.androidsleepmachine.gamble/com.androidsleepmachine.gamble.Ship} did not 
call through to super.onStop()
09-18 00:28:59.000: E/AndroidRuntime(1627):     at 
android.app.Activity.performStop(Activity.java:5148)
09-18 00:28:59.000: E/AndroidRuntime(1627):     at   
android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3232)
09-18 00:28:59.000: E/AndroidRuntime(1627):     at   
android.os.Handler.dispatchMessage(Handler.java:99)
09-18 00:28:59.000: E/AndroidRuntime(1627):     at 
android.os.Looper.loop(Looper.java:137)
09-18 00:28:59.000: E/AndroidRuntime(1627):     at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 00:28:59.000: E/AndroidRuntime(1627):     at     
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
user2727048
  • 115
  • 2
  • 12

2 Answers2

2

You need to check is user close the App or just switching to another Activity. You can check my Answers for that

Answer1

Answer2

Hope it helps!!

EDIT

Check this both method in your ship class.

 @Override
    protected void onStop() {
        Log.d(TAG, "App stopped");

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "App destoryed");

        super.onDestroy();
    }
Community
  • 1
  • 1
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
-2

use on key up

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            stop sound!
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
effi
  • 64
  • 6