0

我有一首歌在后台播放,下一页是一个复选框。当它被选中时,我希望音乐保持打开,当它未选中时,我希望它保持关闭。它工作正常,除了一种方式。当我在播放时取消选中音乐时,它会停止播放音乐。现在,如果我重新启动应用程序,音乐不会打开,这也很好,但是当我去检查音乐时,它不会重新打开,直到我回到上一页。我想在重新检查后开始。如果音乐正在播放并且我取消选中它,它会暂停,如果我选中它,它会开始备份,所以我很困惑。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    @Override
    public void onDestroy() {
        mPlayer.stop();
        super.onDestroy();
    }

    private void loadPrefs() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        boolean cbValue = sp.getBoolean("CHECKBOX",true);

        if(cbValue) {
            mPlayer.start();
        } else {
            // nothing to do
        }
    }
}

//------------------------------------------------------

public class next extends Activity {
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.border);
        checkBox = (CheckBox) findViewById(R.id.checkBox1); 
        loadPrefs();
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePrefs("CHECKBOX",checkBox.isChecked());
                loadPrefs();
            }
        }); 
    }

    private void loadPrefs() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        boolean cbValue = sp.getBoolean("CHECKBOX",true);

        if(cbValue) {
            checkBox.setChecked(true);
            MainActivity.mPlayer.start();
        } else {
            checkBox.setChecked(false);
            MainActivity.mPlayer.pause();
        }
    }

    private void savePrefs(String key, Boolean value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }
}
4

1 回答 1

0

我不确定两个活动,但可以在使用单个活动的情况下给出建议。这里的主要思想 - 不仅存储状态播放/暂停,还存储当前播放位置和恢复 - 只需寻找它。

注意:如果您想要后台播放,那么您的方式是完全错误的,您需要使用“使用 MediaPlayer 使用服务”中的建议来修改它(我不会在这里介绍它,因为它完全不同并且很好解释在文档中)。

我建议尝试以下代码(它对我来说很好):

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();
    }
}
于 2013-07-17T07:09:24.140 回答