Skip to content Skip to sidebar Skip to footer

Android Media Player Wont Work With Checkbox?

I have a song playing in the background, and on the next page is a check box. When it's checked I want the music to stay on and it when it's unchecked I want it to stay off. It wor

Solution 1:

I'm not sure about two activities, but could give siggestion in case of usage of single one. The main idea here - store not only state play / pause, but store current playing position also and on resume - just seek to it.

NOTE: In case if You want background play, then your way is completely wrong and You need to rework it using suggestions from "Using a Service with MediaPlayer" (I'm not going to cover it here, because it's completely different and well explained in the documentation).

I would suggest to try the following code (it works fine for me):

public class MyActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    /** To play music */
    private MediaPlayer mPlayer = null;
    /** To start / pause music */
    private CheckBox mSwitch = null;
    /** To store paused state */
    private boolean mIsPlayerPaused = false;

    /** Key to store state of checkbox */
    private static final String CHECKBOX_KEY = "com.example.TestApp.CHECKBOX_KEY";
    /** Key to store current play position */
    private static final String PLAY_POSITION_KEY = "com.example.TestApp.PLAY_POSITION_KEY";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Show the layout with the test view
        setContentView(R.layout.main);

        mSwitch = (CheckBox) findViewById(R.id.checkBox);
    }

    @Override
    protected void onPause() {
        super.onPause();

        saveState();

        if (mPlayer.isPlaying() || mIsPlayerPaused) {
            mPlayer.stop();
        }

        mPlayer.release();
        mPlayer = null;

        mSwitch.setOnCheckedChangeListener(null);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mPlayer = MediaPlayer.create(this, R.raw.roy);
        mPlayer.setLooping(true);

        loadPrefs();
        mSwitch.setOnCheckedChangeListener(this);
    }

    /**
     * Loads preferences. Should be called before set listener to CheckBox.
     */
    private void loadPrefs() {
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        // get played position
        final int position = sp.getInt(PLAY_POSITION_KEY, 0);

        if (position > 0) {
            mPlayer.seekTo(position);
        }

        final boolean cbValue = sp.getBoolean(CHECKBOX_KEY, false);

        if (cbValue) {
            mPlayer.start();
            mSwitch.setChecked(true);
        } else {
            mIsPlayerPaused = true;
        }
    }

    @Override
    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
        if (isChecked) {
            if (!mPlayer.isPlaying()) {
                mPlayer.start();
            }
        } else {
            mPlayer.pause();
            mIsPlayerPaused = true;
        }
    }

    /**
     * Saves current playback state.
     */
    private void saveState() {
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor edit = sp.edit();

        edit.putBoolean(CHECKBOX_KEY, mSwitch.isChecked());

        if (mPlayer != null && (mPlayer.isPlaying() || mIsPlayerPaused)) {
            edit.putInt(PLAY_POSITION_KEY, mPlayer.getCurrentPosition());
        } else {
            edit.putInt(PLAY_POSITION_KEY, 0);
        }

        edit.commit();
    }
}

Post a Comment for "Android Media Player Wont Work With Checkbox?"