tags:

views:

64

answers:

2

I have added a mute button to a menu on my application and am wondering if it is possible to store the user's latest preference of either muted or unmuted for use when he/she reopens the application.

Here is the code I am using for setting mute or umute:

public void isMute() {

      if(mIsMute){    
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
          mIsMute = false;

      }else{
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
          mIsMute = true;
      }
    }
+1  A: 

Use SharedPreferences to store the state. Read it when application starts and set current state.

I modified a little example from android documentation

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){         
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       mIsMute = settings.getBoolean("IsMute", false);
       isMute();
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("IsMute", mIsMute);

      // Commit the edits!
      editor.commit();
    }
}
Orsol
I'm not sure how to do this. I have updated my original question with my method that sets mute or unmute. Would I need to store the result in SharePreferences and call to see what is there in my init()?
taraloca
I have updated my answer
Orsol
First, thank you for your help...I have it working kinda. By kinda I mean that every time I start my app it is muted even if I closed my app while unmuted. I tried switching the value in the putBoolean to no avail. Any ideas?
taraloca
Show me the code. See in debugger or log what value have mIsMute after you doing mIsMute = settings.getBoolean("IsMute", false); if it's true then something wrong with isMute() function or you set it to false in another place after.
Orsol
I "Answered" my own so I could include all my code and such for anyone to look at. Still not working, but on my way.
taraloca
+1  A: 

Here is what I am doing and still not getting consistency with the mute.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Video
    mVid = (VideoView) findViewById(R.id.videoview);
    mVid.setOnTouchListener(this);
    //mVid.setOnClickListener(this);

    // Audio mgr
    mAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    // Restore preferences
    SharedPreferences sp = getSharedPreferences(ACCUWX.Preferences.ALL, MODE_PRIVATE);
    mIsMute = sp.getBoolean(ACCUWX.Preferences.STORED_VOLUME, false);
    mAm.setStreamMute(AudioManager.STREAM_MUSIC, mIsMute);
    Log.d(DEB_TAG, "!@#!@#!In onCreate value of mIsMute is " + mIsMute);
    //isMute();

    //start
    init();
}

public void isMute() {
      SharedPreferences settings = getSharedPreferences(ACCUWX.Preferences.ALL, MODE_PRIVATE);
      SharedPreferences.Editor editor = settings.edit();

      if(mIsMute){    
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
          mIsMute = false;
          editor.putBoolean(ACCUWX.Preferences.STORED_VOLUME, mIsMute);
      }else{
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
          mIsMute = true;
          editor.putBoolean(ACCUWX.Preferences.STORED_VOLUME, mIsMute);
      }
      // Commit the edits!
      Log.d(DEB_TAG, "Inside isMute value of mIsMute" + mIsMute);
      editor.commit();
    }

This is where I determine which menu button the user is selecting and the action that goes with it. public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){

    // Mute
    case R.id.main_menu_mute:
        isMute();
        break;
        .........
        .........
        .........
        }

    return super.onOptionsItemSelected(item);
}

Here is some of my logCat lines...this is when I ended my app in mute mode. It begins correctly, but then when I try to unmute, the correct value is stored, but I get no sound.

07-08 11:51:08.754: DEBUG/WordWeather(2984): !@#!@#!In onCreate value of mIsMute is true

07-08 11:51:26.942: WARN/AudioService(1015): stream was not muted by this client

07-08 11:51:26.950: ERROR/AudioService(1015): Could not get client death handler for stream: 3

07-08 11:51:26.950: DEBUG/WordWeather(2984): Inside isMute value of mIsMutefalse

07-08 11:51:26.997: WARN/InputManagerService(1015): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@446f0100
taraloca
Are you sure that mIsMute changes only in onCreate and in isMute() and nowhere else?
Orsol
taraloca