簡體   English   中英

如何讓這段代碼在單獨的線程上運行?

[英]How do I get this code to run on a separate thread?

我是 java 的新手。 如您所見,這段代碼阻塞了 ui 線程。 當它運行時,應用程序會凍結,直到它完成運行(它不會彈出 ANR)我需要在此代碼運行時為進度按鈕設置動畫。有什么想法嗎?

 @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void onSpeechButtonClicked (View v) {
        TextView outputMessage = this.findViewById(R.id.outputMessage);
        EditText speakText = this.findViewById(R.id.speakText);

        try {
            // Note: this will block the UI thread, so eventually, you want to register for the event
            SpeechSynthesisResult result = synthesizer.SpeakText(speakText.getText().toString());
            assert(result != null);

            if (result.getReason() == ResultReason.SynthesizingAudioCompleted) {
                ((LoadingButton) findViewById(R.id.button_test)).stopLoading("SPEAK");
                outputMessage.setText("Speech synthesis succeeded.");
                System.out.println("Synthesis succeeded.");}

            else if (result.getReason() == ResultReason.Canceled) {
                String cancellationDetails =
                        SpeechSynthesisCancellationDetails.fromResult(result).toString();
                ((LoadingButton)findViewById(R.id.button_test)).stopLoading("There was a problem...");
                outputMessage.setText("Error synthesizing. Error detail: " +
                        System.lineSeparator() + cancellationDetails +
                        System.lineSeparator() + "Did you update the subscription info?");
            }

            result.close();
        } catch (Exception ex) {
            Log.e("SpeechSDKDemo", "unexpected " + ex.getMessage());
            assert(false);
        }
    }

您可以在代碼中使用 asynctasks。 此外,您可以使用帶有 runOnUiThread() 的可運行線程或新線程。 不會提供任何代碼,因為網上有很多例子..

感謝 Johny Deph,這就是我修復代碼的方式。 希望其他人也覺得它有幫助。

 @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void onSpeechButtonClicked (View v) {
        final TextView outputMessage = this.findViewById(R.id.outputMessage);
        final EditText speakText = this.findViewById(R.id.speakText);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (this){
                    try {
                        
                        SpeechSynthesisResult result = synthesizer.SpeakText(speakText.getText().toString());
                        assert(result != null);

                        if (result.getReason() == ResultReason.SynthesizingAudioCompleted) {
                            ((LoadingButton) findViewById(R.id.button_test)).stopLoading("SPEAK");
                            outputMessage.setText("Speech synthesis succeeded.");
                            System.out.println("Synthesis okuur started.");}

                        else if (result.getReason() == ResultReason.Canceled) {
                            String cancellationDetails =
                                    SpeechSynthesisCancellationDetails.fromResult(result).toString();
                            ((LoadingButton)findViewById(R.id.button_test)).stopLoading("There was a problem...");
                            outputMessage.setText("Error synthesizing. Error detail: " +
                                    System.lineSeparator() + cancellationDetails +
                                    System.lineSeparator() + "Did you update the subscription info?");
                        }

                        result.close();
                    } catch (Exception ex) {
                        Log.e("SpeechSDKDemo", "unexpected " + ex.getMessage());
                        assert(false);
                    }

                }
            }
        });
        t.start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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