调用其他类的静态变量为可运行类。

问题描述 投票:0回答:1

我有一个安卓应用,有两个按钮,两个按钮都应该执行相同的运行功能,以实现 runnable我试着在主类中设置公共静态变量,然后我设置为 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;

    }
}

可运行函数

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();
    }
}
java android runnable
1个回答
0
投票

在没有看到的情况下,很难得出结论。startrecordingstoprecording 的功能。然而,看起来你没有正确地停止之前启动的runnable,因此你有录音无法启动的问题。

我想建议,为说话和录音设置单独的线程runnable。这样的话,你可以更好地控制线程。如果你想在说话的时候就开始录音,那么启动一个新的线程来进行录音,应该会使整个代码流变得更干净,更不容易出错。

希望对大家有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.