[英]calling static variable from another class into runnable
我有一个带有两个按钮的 android 应用程序,它们都应该为runnable
执行相同的运行 function,但一个用于扬声器,另一个是扬声器 + 记录。 我尝试在主 class 中设置 public static 变量,然后在单击记录时设置为true
,以便我也执行记录代码,但它总是返回false
。
主要活动
View.OnClickListener mSpeak_onClick = new View.OnClickListener() {
public void onClick(View v) {
if (((CheckableImageButton) v).isChecked()) {
MicActivity.this.stop();
isRecording = false;
MicActivity.this.start();
record.setVisibility(View.GONE);
//record.setBackgroundColor(getResources().getColor(R.color.white));
//stop.setVisibility(View.VISIBLE);
//isRecording = true;
//Speak2MicThread.isRecording(true);
} else {
// anim.cancel();
// anim.reset();
record.setVisibility(View.VISIBLE);
MicActivity.this.stop();
isRecording = false;
}
}
};
public void onClick(View view) {
if (view.getId() == R.id.record) {
MicActivity.this.stop();
anim.setDuration(500); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
//record.startAnimation(anim);
record.startAnimation(anim);
record.setEnabled(false);
mChkSpeak.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
isRecording = true;
MicActivity.this.startrecording();
isRecording = true;
}
if (view.getId() == R.id.stop) {
anim.cancel();
anim.reset();
mChkSpeak.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
record.setEnabled(true);
isRecording = false;
MicActivity.this.stoprecording();
//isRecording = false;
}
}
可运行运行 function
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this.mBoooyee);
Process.setThreadPriority(-19);
this.mNoWait = true;
this.mAlive = true;
try {
if (!init()) {
onError(this.mErrorCode, (int) R.string.msg_res_alreay_in_use);
return;
}
short[] buffer = new short[this.mBufferSize];
if (this.mARec.getState() == 1) {
onStart();
while (true) {
if (!this.mAlive) {
break;
}
Log.e("MicActivity.isRecording",""+ isRecording);
try {
fileOutputStream = new FileOutputStream(getTempFilename());
} catch (FileNotFoundException e2) {
Log.e("fileOutputStream","hhh");
e2.printStackTrace();
//mAudioInput.stop();
return;
}
// if(isRecording){
while (this.mNoWait) {
try {
int read = this.mARec.read(buffer, 0, this.mFragSize);
int gain = this.mGain;
fileOutputStream.write(MyShortToByte(buffer), 0, read);
if (gain > 0) {
adjustMicGain(buffer, read, gain);
}
if(read > 0){
this.mATrack.write(buffer, 0, read);
}
if (this.mISStereo) {
this.mATrack.setStereoVolume(this.mVolumeLeft, this.mVolumeRight);
}
} catch (Exception e) {
this.l.printStackTrace(e);
if (this.mARec.getState() != 3) {
audioRecordSafeStop();
if (!audioRecordInit()) {
onError(this.mErrorCode, e.getMessage());
break;
}
}
if (this.mATrack.getState() != 3) {
audioPlaySafeStop();
if (!audioPlayInit()) {
onError(this.mErrorCode, e.getMessage());
break;
}
} else {
continue;
}
}
}
try {
fileOutputStream.close();
} catch (Exception e3) {
e3.printStackTrace();
}
}
// }
}
audioRecordSafeStop();
audioPlaySafeStop();
this.mNoWait = false;
this.mAlive = false;
if (this.mErrorCode == 0) {
onComplete();
}
} catch (Exception e2) {
this.l.printStackTrace(e2);
onError(9, e2.getMessage());
} finally {
audioRecordSafeStop();
audioPlaySafeStop();
}
}
没有看到你的开始录制和stoprecording
录制startrecording
很难下结论。 但是,您似乎没有停止早先正确启动的可运行文件,因此您遇到了录制未启动的问题。
我想建议,为口语和录音设置单独的线程/可运行。 这样,您可以更好地控制线程。 如果您想在讲话时开始录音,那么启动一个仅用于录音的新线程应该会使整个代码流更加清晰且不易出错。
我希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.