Android Media播放器无法使用复选框?

时间:2013-07-17 01:00:03

标签: android checkbox onclick sharedpreferences android-mediaplayer

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

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();
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定两项活动,但在使用单一活动时可能会产生偏差。这里的主要想法 - 不仅存储状态播放/暂停,而且还存储当前播放位置和简历 - 只是寻求它。

注意:如果您想要背景播放,那么您的方式是完全错误的,您需要使用"Using a Service with 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();
    }
}