获取消息speak失败:未绑定到tts引擎

qyyhg6bp  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(204)

我被这个问题困住了我得到这个错误说话失败:没有绑定到tts引擎,也得到这个错误e/texttospeech:绑定到com.google.android.tts失败。我正在提供我的代码请帮助我解决它。我试图尽我所能解决这个问题。

public class TechnicalScreening extends AppCompatActivity {

TextView tl, tvQ, tvQuestionNumber;

ProgressDialog progressDialog;
ReviewProbeViewModel reviewProbeViewModel;
StartAudioRecord startAudioRecord;
String questionId = "";
FloatingActionButton btnStart, btnEnd;
ImageView btnPlay;
ArrayList<TechnicalScreeningModel> technicalScreeningModelArrayList;
int currentlyShowingQuestion = 0;
boolean hasAnswerGiven = false;
int timeLeftSec = 0;
CountDownTimer countDownTimer;
TextToSpeech textToSpeech;
String skill = "";
boolean testCompleted = false;
boolean testPaused = false;
private final ReviewProbeApiInit reviewProbeApiInit = new ReviewProbeApiInit();

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_technical_screening);
    tvQuestionNumber = findViewById(R.id.tvQuestionNumber);
    tvQ = findViewById(R.id.tvQ);
    tl = findViewById(R.id.tl);
    btnStart = findViewById(R.id.btnStart);
    btnEnd = findViewById(R.id.btnEnd);
    btnPlay= findViewById(R.id.btnPlay);
    progressDialog = new ProgressDialog(TechnicalScreening.this);
    progressDialog.setMessage("PLEASE WAIT for tech screening ");
    reviewProbeViewModel = new ViewModelProvider(this).get(ReviewProbeViewModel.class);
    startAudioRecord = new StartAudioRecord(TechnicalScreening.this);
    btnStart.setVisibility(View.VISIBLE);
    skill = getIntent().getStringExtra("skill");

    textToSpeech = new TextToSpeech(getApplicationContext(), status -> {
       if(status == TextToSpeech.SUCCESS){
            textToSpeech.setLanguage(Locale.US);
        }
    });

    startAudioRecord.setPostAudioRecordFinish(file -> {
        progressDialog.setMessage("Processing...");
        countDownTimer.cancel();

        new Handler().post(() -> {
            try (InputStream in = new FileInputStream(file)) {
                byte[] buf;
                buf = new byte[in.available()];
                while (in.read(buf) != -1) ;
                ValidateTechnicalScreeningAnswerModel validateTechnicalScreeningAnswerModel = new ValidateTechnicalScreeningAnswerModel(questionId, skill, buf, Utils.getFirebaseUser().getUid(), Utils.getCountryName());

                reviewProbeViewModel.postTechnicalSkillAnswer(validateTechnicalScreeningAnswerModel).observe(TechnicalScreening.this, aBoolean -> {

                    progressDialog.dismiss();
                    if (technicalScreeningModelArrayList.size() > currentlyShowingQuestion + 1) {

                        currentlyShowingQuestion++;
                        updateView(currentlyShowingQuestion);

                    } else {

                        Toast.makeText(TechnicalScreening.this, "All question have been answered ", Toast.LENGTH_SHORT).show();
                        getResultsAfterScreening();
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }

        });

    });

    progressDialog.show();
    reviewProbeViewModel.getTechnicalSkillQA( skill,Utils.getFirebaseUser().getUid()).observe(TechnicalScreening.this, technicalScreeningModelGenericConnectivityList -> {
        progressDialog.dismiss();
        Log.d("TECH_SCR",""+technicalScreeningModelGenericConnectivityList.isSuccessful());
        if(technicalScreeningModelGenericConnectivityList.isSuccessful()) {
            if (!technicalScreeningModelGenericConnectivityList.getData().isEmpty()){
                technicalScreeningModelArrayList = technicalScreeningModelGenericConnectivityList.getData();
            Log.d("TECH_SCR", skill);
            updateView(currentlyShowingQuestion);
             }
            else
            {

                AlertDialog.Builder builder = new AlertDialog.Builder(TechnicalScreening.this);
                builder.setTitle("Audio Screening")
                        .setMessage("We are currently preparing question for this section")
                        .setCancelable(false)
                        .setPositiveButton("OK", (dialog, which) -> finish());

                //Creating dialog box
                AlertDialog dialog  = builder.create();
                dialog.show();
            }

        }
        else {
            String message = technicalScreeningModelGenericConnectivityList.getErrorMessage();

            AlertDialog.Builder builder = new AlertDialog.Builder(TechnicalScreening.this);
            builder.setTitle("Audio Screening Result")
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton("OK", (dialog, which) -> finish());

            //Creating dialog box
            AlertDialog dialog  = builder.create();
            dialog.show();
        }
    });

    textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onDone(String utteranceId) {
             Log.d("TECHNICAL_SCR", "TTS finished "+timeLeftSec);
            countDownTimer.start();
            btnStart.setEnabled(true);

        }

        @Override
        public void onError(String utteranceId) {
        }

        @Override
        public void onStart(String utteranceId) {
        }
    });

    btnPlay.setOnClickListener(v -> textToSpeech.speak(tvQ.getText().toString(), TextToSpeech.QUEUE_ADD, null, "utter"));

private void updateView(int i) {

    new Handler().post(() -> {
        tvQuestionNumber.setText("Question : " + (i + 1));
        tvQ.setText(technicalScreeningModelArrayList.get(i).getQuestion());
        tl.setText("00:" + technicalScreeningModelArrayList.get(i).getTimeInSec());
        questionId = technicalScreeningModelArrayList.get(i).getQuestionId();
        timeLeftSec = technicalScreeningModelArrayList.get(i).getTimeInSec();
      //textToSpeech.speak(tvQ.getText(), TextToSpeech.QUEUE_ADD, null, "utter");
        setCounterAtStart(timeLeftSec * 1000);
        btnStart.setEnabled(false);
        btnPlay.setVisibility(View.VISIBLE);
        hasAnswerGiven = false;
    });

}

private void setCounterAtStart(long timeLeft) {

    countDownTimer = new CountDownTimer(timeLeft, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            updateTime();

        }

private void updateTime() {

    if (timeLeftSec != 0) {
        tl.setText(String.format(Locale.getDefault(), "%02d:%02d", 0, timeLeftSec));
        timeLeftSec = timeLeftSec - 1;
    }

}

@Override
protected void onDestroy() {

    if(textToSpeech != null){
        textToSpeech.stop();
        textToSpeech.shutdown();
    }

    super.onDestroy();
}

}
请帮我解决这个问题!!!!提前谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题