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):

publicclassMyActivityextendsActivityimplementsCompoundButton.OnCheckedChangeListener {
    /** To play music */privateMediaPlayermPlayer=null;
    /** To start / pause music */privateCheckBoxmSwitch=null;
    /** To store paused state */privatebooleanmIsPlayerPaused=false;

    /** Key to store state of checkbox */privatestaticfinalStringCHECKBOX_KEY="com.example.TestApp.CHECKBOX_KEY";
    /** Key to store current play position */privatestaticfinalStringPLAY_POSITION_KEY="com.example.TestApp.PLAY_POSITION_KEY";

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

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

    @OverrideprotectedvoidonPause() {
        super.onPause();

        saveState();

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

        mPlayer.release();
        mPlayer = null;

        mSwitch.setOnCheckedChangeListener(null);
    }

    @OverrideprotectedvoidonResume() {
        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.
     */privatevoidloadPrefs() {
        finalSharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
        // get played positionfinalintposition= sp.getInt(PLAY_POSITION_KEY, 0);

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

        finalbooleancbValue= sp.getBoolean(CHECKBOX_KEY, false);

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

    @OverridepublicvoidonCheckedChanged(final CompoundButton buttonView, finalboolean isChecked) {
        if (isChecked) {
            if (!mPlayer.isPlaying()) {
                mPlayer.start();
            }
        } else {
            mPlayer.pause();
            mIsPlayerPaused = true;
        }
    }

    /**
     * Saves current playback state.
     */privatevoidsaveState() {
        finalSharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editoredit= 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?"