共享偏好编辑器混合值

时间:2017-06-10 03:25:12

标签: java android sharedpreferences text-to-speech

在我的文本到语音风格的应用程序中我可以设置语音的速度和音高,还有2个变量用于超声波音高设置。如果我设置速度和音高并退出设置菜单一切都很好,问题是如果我按下完成按钮并且频率分析仪运行并设置超声波变量,变量会在偏好编辑器中保存在错误的位置,从而导致在超声值设置的速度和音高,我已经尝试了2天来修复它但没有任何作用,下面是相关的代码,如果你需要更多请问,谁能看到我做错了什么? / p> 编辑:这是一个apk显示问题,转到设置菜单(从工具栏右上角)设置速度和音高,按完成,关闭应用程序,打开应用程序,转到设置,你会看到荒谬的值设置为速度和音调 Canine Remote apk

课程开始时的相关初始化变量:

public SharedPreferences appPreferences;
boolean settingUp=false;
int result = 0;
int remote = 0;
int settingLanguage=0;
private float progress = (float) 1.0;
private float progress2 = (float) 1.0;
private static final String LSTYLE = "usa";//language
private static final String MYPITCH = "1.0";//normal use voice pitch
private static final String MYSPEED = "1.0";//normal use voice speed
private static final String ULTRADOG = "1.0";//22000 +/- user pitch khz
private static final String ULTRACAT = "1.0";//48000 +/- user pitch khz
private static final String HUMANDOGCAT = "0";//human=0, dog=1, cat=2
private static final String REMOTE = "0";//speak through device=0, speak 
//through remote bluetooth speaker=1
private TextView edit;
private TextView edit2;
private Button edit3;
private Button edit4;
private Button edit5;
private Button edit6;
private TextView edit7;
private ImageButton edit8;
private Button edit9;
private CheckBox ckultra;
private CheckBox cklocal;
private CheckBox ckultra2;
private SeekBar cpitch;
private SeekBar cspeed;
AudioRecord mAudioRecord;
int freq = 11025;
int Nb;
int N;
int running=0;
double FreqMin = 0;
double FreqMax = 2300;
int muestras = 1000;
double PI2n = 2 * Math.PI/muestras;
double FreqMuestras=freq/muestras;
int indMin = (int) (FreqMin/FreqMuestras);
int indMax = (int) (FreqMax/FreqMuestras);
double newFrequency = 170;
double freakMin=170;
double freakMax=170;
double mean=170;


    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_canine_start);
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);

此按钮在首选项编辑器中设置音高

edit3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Globals g = (Globals) getApplication();
    SharedPreferences.Editor editor = appPreferences.edit();
    float pitch = g.getData2();
    if (pitch < 0.1) pitch = (float) 0.1;
    editor.putFloat(MYPITCH, pitch);
    editor.commit();
    tts.setPitch(pitch);
}
});

此按钮在首选项编辑器中设置速度,如果需要查看,我还在此处包含滑块和复选框代码

edit4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Globals g = (Globals) getApplication();
    SharedPreferences.Editor editor2 = appPreferences.edit();
    float speed=g.getData3();
    if (speed < 0.1) speed = (float) 0.1;
    editor2.putFloat(MYSPEED, speed);
    editor2.commit();
    tts.setSpeechRate(speed);
}
});
//slider bars to get pitch and speed
    cpitch.setProgress(1);
edit.setText("Adjust voice Pitch: " + cpitch.getProgress());
cpitch.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
Globals g = (Globals) getApplication();
@Override
public void onProgressChanged(SeekBar cpitch, int progresValue, boolean fromUser) {
    progress = (float) (progresValue * 0.1);
    edit.setText("Adjust voice pitch: " + progress);
}
@Override
public void onStartTrackingTouch(SeekBar cpitch) {
}
@Override
public void onStopTrackingTouch(SeekBar cpitch) {
    edit.setText("Adjust voice pitch: " + progress);
    g.setData2(progress);
   }
});
cspeed.setProgress(1);
edit2.setText("Adjust voice speed: " + cspeed.getProgress());
cspeed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
Globals g = (Globals) getApplication();
@Override
public void onProgressChanged(SeekBar cspeed, int progresValue2, boolean fromUser2) {
    progress2 = (float) (progresValue2 * 0.1);
    edit2.setText("Adjust voice speed: " + progress2);
}
@Override
public void onStartTrackingTouch(SeekBar cspeed) {
}
@Override
public void onStopTrackingTouch(SeekBar cspeed) {
    edit2.setText("Adjust voice speed: " + progress2);
    g.setData3(progress2);
}
});
}

//check boxes
class clicker implements CheckBox.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView,
                             boolean isChecked) {
    SharedPreferences.Editor editor5 = appPreferences.edit();
    if (isChecked) {
        if (buttonView == ckultra) {
            editor5.putInt(HUMANDOGCAT, 1).commit();
            ckultra2.setChecked(false);
        }
        if (buttonView == ckultra2) {
            editor5.putInt(HUMANDOGCAT, 2).commit();
            ckultra.setChecked(false);
        }
        if (buttonView == cklocal) {
            editor5.putInt(REMOTE, 1).commit();
        }
    }
    if (!isChecked) {
        if (buttonView == ckultra) {
            editor5.putInt(HUMANDOGCAT, 0).commit();
        }
        if (buttonView == ckultra2) {
            editor5.putInt(HUMANDOGCAT, 0).commit();
        }
        if (buttonView == cklocal) {
            editor5.putInt(REMOTE, 0).commit();
        }
    }
}
}

此按钮是导致错误的代码的开头

edit6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    edit7.setVisibility(View.VISIBLE);
    edit8.setVisibility(View.VISIBLE);
    edit9.setVisibility(View.VISIBLE);
    edit.setVisibility(View.GONE);
    edit2.setVisibility(View.GONE);
    edit3.setVisibility(View.GONE);
    edit4.setVisibility(View.GONE);
    edit5.setVisibility(View.GONE);
    edit6.setVisibility(View.GONE);
    cpitch.setVisibility(View.GONE);
    cspeed.setVisibility(View.GONE);
    ckultra.setVisibility(View.GONE);
    ckultra2.setVisibility(View.GONE);
    cklocal.setVisibility(View.GONE);
    speakIT("Completed.");
    Spectrometer_Start();
}
});
//analyze pitch to set ultrasound variables
public void Spectrometer_Start() {
try {
    Nb = AudioRecord.getMinBufferSize(freq, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) * 4;
    N = Nb * Byte.SIZE / Short.SIZE;
    mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, Nb);
    mAudioRecord.startRecording();
    running=1;
    analizer();
    } catch (IllegalArgumentException i) {
}
}

此方法将偏好设置保存在首选项编辑器中的错误位置

public void analizer() {
int a=10;
while(tts.isSpeaking())
{
    a-=1;
    if(a==0)
    {
        short[] data= new short[muestras];
        try {
            mAudioRecord.read(data, 0, muestras-1);
            Dft(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        a=10;
    }
}
mAudioRecord.stop();
mAudioRecord.release();
Globals g = (Globals) getApplication();
running=0;
mean=((freakMax-freakMin)*0.5)+freakMin;
float calcDog = (float) (22000 / mean);
float calcCat = (float) (48000 / mean);
g.setData4(calcDog);
g.setData5(calcCat);
SharedPreferences.Editor editor7 = appPreferences.edit();
editor7.putFloat(ULTRADOG, calcDog);
editor7.putFloat(ULTRACAT, calcCat);
editor7.commit();
}

public void Dft(short[] inreal) {
for (int k = indMin; k < indMax; k++)
{
    float sumreal = 0;
    float sumimag = 0;
    float PI2kn= (float) (PI2n * k);
    for (int t = 0; t < muestras; t++) {
        double angle = t*PI2kn;
        sumreal +=  inreal[t] * Math.cos(angle);
        sumimag += inreal[t] * Math.sin(angle);
    }
        newFrequency = (Math.sqrt(sumreal * sumreal + sumimag * sumimag));
        if (newFrequency < freakMin && newFrequency >= 85) {
            freakMin = newFrequency;
        }
        if (newFrequency > freakMax && newFrequency <= 255) {
            freakMax = newFrequency;
        }
    }
}

此按钮启动输入的语音,并从首选项编辑器接收错误的值

edit9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    String sendComands= edit7.getText().toString();
    if (!sendComands.equals("") && settingLanguage == 0){
        float NPITCH = appPreferences.getFloat(MYPITCH, (float) 1.0);
        float NSPEED = appPreferences.getFloat(MYSPEED, (float) 1.0);
        float UDOG = appPreferences.getFloat(ULTRADOG, (float) 1.0);
        float UCAT = appPreferences.getFloat(ULTRACAT, (float) 1.0);
        int HDC = appPreferences.getInt(HUMANDOGCAT, 0);
       switch (HDC) {
           case 0:
               tts.setPitch(NPITCH);
               break;
           case 1:
               tts.setPitch(UDOG);
               break;
           case 2:
               tts.setPitch(UCAT);
               break;
       }
        tts.setSpeechRate(NSPEED);
        speakIT(sendComands);
        edit7.setHint("Enter text to send");
        settingUp=false;
    }
    if (settingLanguage == 1) {
        countryC(sendComands);
        settingLanguage=0;
        edit7.setHint("Enter text to send");
        edit9.setText("Send");
        edit7.setVisibility(View.GONE);
        edit9.setVisibility(View.GONE);
    }
}
});

工具栏中的我的设置菜单带有注释,以便您知道哪些对象执行了哪些操作

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.canine_start, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Globals g = (Globals) getApplication();
int id = item.getItemId();
if (id == R.id.action_settings) {
    edit.setVisibility(View.VISIBLE);//pitch slider text
    edit2.setVisibility(View.VISIBLE);//speed slider text
    edit3.setVisibility(View.VISIBLE);//button - set pitch
    edit4.setVisibility(View.VISIBLE);//button - set speed
    edit5.setVisibility(View.VISIBLE);//button - test speak
    edit6.setVisibility(View.VISIBLE);//button - finished with settings
    edit7.setVisibility(View.GONE);//edit text - text to speech
    edit8.setVisibility(View.GONE);//image button - speech recognition start
    edit9.setVisibility(View.GONE);//button - speak edit7 text
    cpitch.setVisibility(View.VISIBLE);//slider - adjust voice pitch
    cspeed.setVisibility(View.VISIBLE);//slider - adjust voice speed
    ckultra.setVisibility(View.VISIBLE);//checkbox - ultrasound dog on/off
    ckultra2.setVisibility(View.VISIBLE);//checkbox - ultrasound cat on/off
    cklocal.setVisibility(View.VISIBLE);//checkbox - send to bluetooth/local device
    settingUp=true;
    return true;
}

1 个答案:

答案 0 :(得分:2)

您使用无效密钥将值存储在sharedPreferences中:

这些值用作键:

private static final String MYPITCH = "1.0";
private static final String MYSPEED = "1.0";
private static final String ULTRADOG = "1.0";
private static final String ULTRACAT = "1.0";

此处存储值

editor.putFloat(MYPITCH, pitch);
editor2.putFloat(MYSPEED, speed);
editor7.putFloat(ULTRADOG, calcDog);
editor7.putFloat(ULTRACAT, calcCat);

此处获取值

float NPITCH = appPreferences.getFloat(MYPITCH, (float) 1.0);
float NSPEED = appPreferences.getFloat(MYSPEED, (float) 1.0);
float UDOG = appPreferences.getFloat(ULTRADOG, (float) 1.0);
float UCAT = appPreferences.getFloat(ULTRACAT, (float) 1.0);

您应该使用唯一和有意义的密钥:

private static final String MYPITCH = "my_pitch";
private static final String MYSPEED = "my_speed";
private static final String ULTRADOG = "ultra_dog";
private static final String ULTRACAT = "ultra_cat";