简体   繁体   中英

shared preference editor mixing up values

in my text to speech style app I can set the speed and pitch of the voice, and also 2 variables for ultrasound pitch settings. If I set the speed and pitch and exit the settings menu everything is fine, The problem is that if I press the finish button and the frequency analyzer runs and sets the ultrasound variables the variables get saved in the preference editor in the wrong spot, causing the speed and pitch to be set at the ultrasound values, I have been trying for 2 days to fix it but nothing works, below is the relevant code, if you need more please ask, can anyone see what I'm doing wrong?

EDIT: here is an apk showing the problem, go to settings menu (from toolbar upper right) set the speed and pitch, press finish, close app, open app, go to settings and you will see the ridiculous values set as speed and pitch Canine Remote apk

Relevant initialized variables at start of class:

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

This button sets the pitch in the preference editor

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

This button sets the speed in the preference editor, also I've included the slider bars and check boxes codes here if you need to see them

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

This button is the start of the code causing the error

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) {
}
}

This method saves the ultrasound settings in the wrong place in the preference editor

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

This button starts the speech entered, and receives the wrong values from the preference editor

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

My settings menu in the toolbar with comment so you know which objects do what

@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;
}

Your are using invalid keys to stores your values in sharedPreferences:

These values are used as keys:

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";

Here to store values

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

And here to get values

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

You should instead use unique and meaningful keys:

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM