Android的媒体播放器不会使用复选框工作? [英] Android Media player wont work with checkbox?

查看:183
本文介绍了Android的媒体播放器不会使用复选框工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后台播放歌曲,并在接下来的页面上一个复选框。当它选中我想要的音乐留在它时,它的选中我希望它留下来了。它的工作原理只是单向的罚款。当我取消了音乐在播放时停止音乐,因为它应该。现在如果我重新启动应用程序的音乐犯规开启这是很好的为好,但是当我去检查的音乐它不回头,直到我回到previous页面。我想一旦它重新测试开始。如果正在播放的音乐,我取消它,它会暂停,如果我检查启动丁字裤,所以我很困惑。

 公共类MainActivity延伸活动{
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        MPLAYER = MediaPlayer.create(MainActivity.this,R.raw.roy);
        mPlayer.setLooping(真);
        负荷preFS()
    }    @覆盖
    公共无效的onDestroy(){
        mPlayer.stop();
        super.onDestroy();
    }    私人无效负荷preFS(){
        共享preferences SP = preferenceManager.getDefaultShared preferences(本);
        布尔cbValue = sp.getBoolean(复选框,真正的);        如果(cbValue){
            mPlayer.start();
        }其他{
            // 没事做
        }
    }
}

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

 公共类下一个扩展活动{
    复选框复选框;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.border);
        复选框=(复选框)findViewById(R.id.checkBox1);
        负荷preFS();
        checkBox.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                节省preFS(复选框,checkBox.isChecked());
                负荷preFS();
            }
        });
    }    私人无效负荷preFS(){
        共享preferences SP = preferenceManager.getDefaultShared preferences(本);
        布尔cbValue = sp.getBoolean(复选框,真正的);        如果(cbValue){
            checkBox.setChecked(真);
            MainActivity.mPlayer.start();
        }其他{
            checkBox.setChecked(假);
            MainActivity.mPlayer.pause();
        }
    }    私人无效节省preFS(字符串键,布尔值){
        共享preferences SP = preferenceManager.getDefaultShared preferences(本);
        编辑编辑= sp.edit();
        edit.putBoolean(键,值);
        edit.commit();
    }
}


解决方案

我不知道有关的两项活动,但是可以在单一的使用情况给予siggestion。其主要思想在这里 - 店不仅是国家播放/暂停,但存储当前播放的位置也和简历 - 只是寻求它。

注意:在情况下,如果你想后台播放,那么你的方法是完全错误的,你需要使用来自的使用与MediaPlayer的服务(我不打算在这里介绍它,因为它是完全不同的,在文档中很好地解释)。

我建议尝试以下code(正常工作对我来说):

 公共类MyActivity扩展活动实现CompoundButton.OnCheckedChangeListener {
    /** 演奏音乐 */
    私人的MediaPlayer MPLAYER = NULL;
    / **开始/暂停音乐* /
    私人复选框mSwitch = NULL;
    / **要存储暂停状态* /
    私人布尔mIsPlayerPaused = FALSE;    / **关键复选框的商店状态* /
    私有静态最后弦乐CHECKBOX_KEY =com.example.TestApp.CHECKBOX_KEY;
    / **键保存当前播放位置* /
    私有静态最后弦乐PLAY_POSITION_KEY =com.example.TestApp.PLAY_POSITION_KEY;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //显示与测试视图布局
        的setContentView(R.layout.main);        mSwitch =(复选框)findViewById(R.id.checkBox);
    }    @覆盖
    保护无效的onPause(){
        super.onPause();        saveState和();        如果(mPlayer.isPlaying()|| mIsPlayerPaused){
            mPlayer.stop();
        }        mPlayer.release();
        MPLAYER = NULL;        mSwitch.setOnCheckedChangeListener(NULL);
    }    @覆盖
    保护无效onResume(){
        super.onResume();        MPLAYER = MediaPlayer.create(这一点,R.raw.roy);
        mPlayer.setLooping(真);        负荷preFS();
        mSwitch.setOnCheckedChangeListener(本);
    }    / **
     *负载preferences。集侦听器复选框前应该被调用。
     * /
    私人无效负荷preFS(){
        最后一个共享preferences SP = preferenceManager.getDefaultShared preferences(本);
        //获得出场位置
        最终诠释位置= sp.getInt(PLAY_POSITION_KEY,0);        如果(位置大于0){
            mPlayer.seekTo(位置);
        }        最终布尔cbValue = sp.getBoolean(CHECKBOX_KEY,FALSE);        如果(cbValue){
            mPlayer.start();
            mSwitch.setChecked(真);
        }其他{
            mIsPlayerPaused = TRUE;
        }
    }    @覆盖
    公共无效onCheckedChanged(最终CompoundButton buttonView,最终布尔器isChecked){
        如果(器isChecked){
            如果(!mPlayer.isPlaying()){
                mPlayer.start();
            }
        }其他{
            mPlayer.pause();
            mIsPlayerPaused = TRUE;
        }
    }    / **
     *保存当前的播放状态。
     * /
    私人无效saveState和(){
        最后一个共享preferences SP = preferenceManager.getDefaultShared preferences(本);
        最后一个共享preferences.Editor编辑= sp.edit();        edit.putBoolean(CHECKBOX_KEY,mSwitch.isChecked());        如果(MPLAYER = NULL&放大器;!及(mPlayer.isPlaying()|| mIsPlayerPaused)){
            edit.putInt(PLAY_POSITION_KEY,mPlayer.getCurrentPosition());
        }其他{
            edit.putInt(PLAY_POSITION_KEY,0);
        }        edit.commit();
    }
}

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 works fine except for one way. When I uncheck the music while playing it stops the music as it should. now if I restart the app the music doesnt turn on which is good as well, but when I go to check music it doesnt turn back on until I go back to the previous page. i want to to start once it's rechecked. If the music is playing and I uncheck it, it pauses, and if I check it it starts t back up so I am confused.

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

解决方案

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

这篇关于Android的媒体播放器不会使用复选框工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆