使用togglebutton为mediaplayer setDataSource创建两个选项

时间:2014-05-10 17:46:57

标签: android android-mediaplayer

我目前正在寻找为togglebutton创建共享偏好。在选中时,我想将mediaplayer setDataSource设置为audio1;在取消选择时,我想将mediaplayer setDataSource设置为audio2。然后我想在第二个活动中实现媒体播放器,根据切换按钮播放正确的音频..

我在浏览那里的许多教程时遇到了一些问题,包括:togglebutton一旦退出活动就重置其状态;并且togglebutton没有实现正确的音频文件。

有人可以指出我正确的方向吗?

public static final String PREFS_NAME = "MyPreferences";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence_personalisation);

    final ToggleButton toggle = (ToggleButton) findViewById(R.id.togglePersonalise);

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // What do I need to write here?
            } else {
                // What do I need to write here?
            }
        }
    });

偏好活动:

public class Preferences extends Activity {

public static final String PREFS_NAME = "MyPreferences";

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence_personalisation);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   

    final ToggleButton toggle = (ToggleButton) findViewById(R.id.togglePersonalise);

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // Code to set MediaPlayer setDataSource to audio1
            } else {
                // Code to set MediaPlayer setDataSource to audio2
            }
        }
    });

播放相应音频的第二个活动:

public class Confidence extends ActionBarActivity {

public static final String PREFS_NAME = "MyPreferences";

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       

    ImageButton b1 = (ImageButton) findViewById(R.id.button111);

    b1.setOnClickListener(new OnClickListener() {
        // Play audio corresponding to toggle button 
        @Override
        public void onClick(View v) {
            MediaPlayer mp1 = MediaPlayer.create(Confidence.this, R.raw.page12);
            mp1.start();
        }
    });

1 个答案:

答案 0 :(得分:0)

如果要通过共享首选项对其进行备份,则需要将onCreate中的检查状态设置为共享首选项的值。然后在onCheckChanged中,您只需将共享首选项的值设置为值isChecked

编辑:

所以在你的新代码中 - 在播放按钮的onClick上使用:

        MediaPlayer mp1 = MediaPlayer.create(Confidence.this, R.raw.page12);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       
        if(settings.getBoolean("ToggleName",true)){
            mp1.setDataSource(source1);
        }
        else{
            mp1.setDataSource(source2);
        }
        mp1.prepare();
        mp1.start();

在两个onCheckChanged函数中,使用

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       
        SharedPreferences.Editor edit = settings.edit();
        edit.putBoolean("ToggleName",isChecked);
        edit.commit();

您可能还想查看使用PreferencesActivity,如果您正在制作标准偏好设置屏幕,它将为您完成大量工作。