簡體   English   中英

從另一個 class 調用 static 變量到可運行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM