diff --git a/demo/android/OrcaDemo/orca-demo-app/src/main/java/ai/picovoice/orcademo/MainActivity.java b/demo/android/OrcaDemo/orca-demo-app/src/main/java/ai/picovoice/orcademo/MainActivity.java index 6a7bb971..317fa8dc 100644 --- a/demo/android/OrcaDemo/orca-demo-app/src/main/java/ai/picovoice/orcademo/MainActivity.java +++ b/demo/android/OrcaDemo/orca-demo-app/src/main/java/ai/picovoice/orcademo/MainActivity.java @@ -360,9 +360,14 @@ public void onStreamSwitchClick(View view) { try { if (orcaStream == null) { orcaStream = orca.streamOpen(new OrcaSynthesizeParams.Builder().build()); + runOnUiThread(() -> { + streamSecsTextView.setText(""); + streamSecsTextView.setVisibility(View.VISIBLE); + }); } else { orcaStream.close(); orcaStream = null; + runOnUiThread(() -> streamSecsTextView.setVisibility(View.INVISIBLE)); } } catch (OrcaException e) { onOrcaException(e); @@ -398,7 +403,6 @@ private void stopStreamPlay() { setUIState(UIState.EDIT); infoTextView.setText(""); streamTextView.setVisibility(View.INVISIBLE); - streamSecsTextView.setVisibility(View.INVISIBLE); synthesizeEditText.setVisibility(View.VISIBLE); }); } catch (Exception e) { @@ -418,6 +422,10 @@ private void runStreamSynthesis(final String text) { AtomicBoolean isStreamingText = new AtomicBoolean(false); ArrayList textStream = new ArrayList<>(); + AtomicBoolean isQueueingStreamingPcm = new AtomicBoolean(false); + ConcurrentLinkedQueue pcmQueue = new ConcurrentLinkedQueue<>(); + CountDownLatch latch = new CountDownLatch(1); + executor.submit(() -> { isStreamingText.set(true); @@ -425,9 +433,12 @@ private void runStreamSynthesis(final String text) { for (String word : words) { word += " "; try { - textStream.add(word); + String finalWord = word; + mainHandler.post(() -> { + textStream.add(finalWord); + streamTextView.append(finalWord); + }); Thread.sleep(100); - streamTextView.append(word); } catch (InterruptedException e) { throw new RuntimeException(e); } @@ -436,15 +447,11 @@ private void runStreamSynthesis(final String text) { isStreamingText.set(false); }); - AtomicBoolean isQueueingStreamingPcm = new AtomicBoolean(false); - ConcurrentLinkedQueue pcmQueue = new ConcurrentLinkedQueue<>(); - CountDownLatch latch = new CountDownLatch(1); - executorStreamingSynthesis.submit(() -> { try { mainHandler.post(() -> { streamTextView.setText(""); - streamSecsTextView.setText(""); + streamSecsTextView.setText("Seconds of audio synthesized: 0.000s"); synthesizeButton.setEnabled(false); }); @@ -461,7 +468,8 @@ private void runStreamSynthesis(final String text) { if (pcm != null && pcm.length > 0) { pcmQueue.add(pcm); secs += (float) pcm.length / orca.getSampleRate(); - streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", secs) + "s"); + float finalSecs = secs; + mainHandler.post(() -> streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", finalSecs) + "s")); if (numIterations == STREAMING_NUM_AUDIO_WAIT_CHUNKS) { latch.countDown(); isPcmPlayStarted = true; @@ -479,7 +487,8 @@ private void runStreamSynthesis(final String text) { if (flushedPcm != null && flushedPcm.length > 0) { pcmQueue.add(flushedPcm); secs += (float) flushedPcm.length / orca.getSampleRate(); - streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", secs) + "s"); + float finalSecs = secs; + mainHandler.post(() -> streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", finalSecs) + "s")); } if (!isPcmPlayStarted) { diff --git a/demo/android/OrcaDemo/orca-demo-app/src/main/res/layout/orca_demo.xml b/demo/android/OrcaDemo/orca-demo-app/src/main/res/layout/orca_demo.xml index 71f01b08..f541b179 100644 --- a/demo/android/OrcaDemo/orca-demo-app/src/main/res/layout/orca_demo.xml +++ b/demo/android/OrcaDemo/orca-demo-app/src/main/res/layout/orca_demo.xml @@ -9,52 +9,93 @@ android:foregroundTint="#002A1F65" tools:context=".MainActivity"> - + + + + + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toBottomOf="@+id/textViewBarrier" /> - + app:layout_constraintStart_toStartOf="parent" /> + app:layout_constraintTop_toTopOf="@+id/streamTextView" + app:layout_constraintVertical_bias="0.0" /> + + - - + app:layout_constraintEnd_toEndOf="@+id/streamTextView" + app:layout_constraintTop_toBottomOf="@+id/streamTextView"/> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/streamSecsTextView" /> - - + app:layout_constraintTop_toBottomOf="@+id/streamSecsTextView" /> - - - + app:layout_constraintStart_toStartOf="parent"/>