大家好,所以我有一个设置活动,它使用共享的pref来静音我的媒体播放器。
cb = (CheckBox) findViewById(R.id.cbmutestartup);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = sharedpreferences.edit();
boolean checkedFlag = sharedpreferences.getBoolean("checkboxstate",false);
cb.setChecked(checkedFlag);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cb.isChecked()) {
editor.putBoolean("soundMute",true);
editor.putBoolean("checkboxstate", true);
editor.commit();
Log.i("Sound: ", "Activated");
}else{
editor.clear();
editor.commit();
Log.i("Sound: ","Deactivated");
}
}
});
}
我的复选框状态的pref仍然在设置活动中。但是当激活soundMute pref然后返回到媒体播放器时会抛出非法状态。我的媒体播放器的onPrepare方法中的非法状态显示在我的日志中。
这是我的媒体播放器类,也是我呼吁共享pref的地方。
ublic class harropMediaplayer {
MediaPlayer player;
String media;
Context c;
public harropMediaplayer(String media,Context c){
this.media = media;
this.c = c;
}
public void volumeSetting(){
////MY NEW VOLUME MUTE METHOD INCLUDING THE FETCH SHARED PREF
SharedPreferences sharedpreferences =
PreferenceManager.getDefaultSharedPreferences(c);
boolean bnSoundMute = sharedpreferences.getBoolean("soundMute",false);
if(bnSoundMute==true){
mutevol();
Log.i("sound","muted");
}else{
volmax();
Log.i("sound","max vol");
}
}
public void plysound() {
player = App_Objects.mp;
Log.i("Url", media);
try {
player.setDataSource(media);
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Sound playing", "Ok");
player.start();
}
public void stopplying(){
player.stop();
player.release();
}
public void volmax(){
player = App_Objects.mp;
player.setVolume(1,1);
}
public void mutevol(){
player = App_Objects.mp;
player.setVolume(0,0);
}
}
解决这个问题的任何帮助都会是非常棒的欢呼声。
****** EDIT
java.lang.IllegalStateException
at android.media.MediaPlayer._setVolume(Native Method)
at android.media.MediaPlayer.playerSetVolume(MediaPlayer.java:1826)
at android.media.PlayerBase.baseSetVolume(PlayerBase.java:117)
at android.media.MediaPlayer.setVolume(MediaPlayer.java:1821)
at com.example.harrops.h20droidapp2.harropMediaplayer.mutevol(harropMediaplayer.java:71)
at com.example.harrops.h20droidapp2.harropMediaplayer.volumeSetting(harropMediaplayer.java:33)
at com.example.harrops.h20droidapp2.Splash$1.onDataChange(Splash.java:84)
at com.google.android.gms.internal.zzeeq.zza(Unknown Source)
at com.google.android.gms.internal.zzegl.zzbwe(Unknown Source)
at com.google.android.gms.internal.zzegr.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)