首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以语为语

以语为语
EN

Stack Overflow用户
提问于 2019-03-04 11:22:55
回答 2查看 363关注 0票数 1

Javafx:如何对动画文本进行文本到语音的处理;我已经在文本上应用了打字机的效果来制作动画文本,现在我希望它能够逐字逐句地打印出来。使用"FreeTTS是语音合成引擎“的文本到语音信息管理系统。

下面是我的项目的代码片段

代码语言:javascript
复制
    public void AnimattedTextToSpeech()
{       
        // Text to Speech
        Voice voice;
         VoiceManager vm=VoiceManager.getInstance();
         voice=vm.getVoice("kevin16");
         voice.allocate();

         // TypeWritter Effect to the text
         String str="Welcome! This is the Lesson number one";
         final IntegerProperty i = new SimpleIntegerProperty(0);
             Timeline timeline = new Timeline();
            KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.1), event2 -> {
                        if (i.get() > str.length()) {
                            timeline.stop();
                        } else {
                            textArea.setText(str.substring(0, i.get()));    
                            i.set(i.get() + 1);
                            textArea.requestFocus();
                            textArea.end();
                        }
                    });
            voice.speak(str);
            timeline.getKeyFrames().add(keyFrame);
            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play();    
}

但是它在打字的时候说出了每一个角色。但我要它逐字逐句地说。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-04 15:14:05

这是可行的,但似乎你需要在一个不同的线程上运行演讲。

代码语言:javascript
复制
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class FreeTTTS extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        TextArea textArea = new TextArea();
        // Text to Speech
        Voice voice;
        VoiceManager vm = VoiceManager.getInstance();
        voice = vm.getVoice("kevin16");
        voice.allocate();

        // TypeWritter Effect to the text
        String str = "Welcome! This is the Lesson number one";
        final IntegerProperty i = new SimpleIntegerProperty(0);
        Timeline timeline = new Timeline();
        AtomicInteger startIndex = new AtomicInteger();
        AtomicInteger endIndex = new AtomicInteger();

        KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.1), event2 -> {
            if (i.get() >= str.length()) {
                timeline.stop();
                startIndex.set(endIndex.get());
                endIndex.set(i.get());

                String word = str.substring(startIndex.get(), endIndex.get()).trim().replaceAll("[^a-zA-Z ]", "");
                System.out.println(word);
                voice.speak(word);
            }
            else {
                textArea.appendText(Character.toString(str.charAt(i.get())));
                if (str.charAt(i.get()) == ' ') {
                    if (endIndex.get() == 0) {
                        endIndex.set(i.get());

                        String word = str.substring(startIndex.get(), endIndex.get()).trim().replaceAll("[^a-zA-Z ]", "");
                        System.out.println(word);
                        voice.speak(word);
                    }
                    else {
                        startIndex.set(endIndex.get());
                        endIndex.set(i.get());

                        String word = str.substring(startIndex.get(), endIndex.get()).trim().replaceAll("[^a-zA-Z ]", "");
                        System.out.println(word);
                        voice.speak(word);
                    }
                }

                i.set(i.get() + 1);

            }
        });
        //voice.speak(str);

        StackPane root = new StackPane(textArea);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        timeline.getKeyFrames().add(keyFrame);
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
        //voice.speak("Hello World");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
票数 2
EN

Stack Overflow用户

发布于 2019-03-04 11:25:14

我认为您应该将文本划分为字符串,并在启动每个字符串的TypeWritter效果时启动TTS。如下所示:

代码语言:javascript
复制
 String str1 = "Welcome! This is the Lesson number one";
    String[] temp = str1.split(" ");
    final IntegerProperty i = new SimpleIntegerProperty(0);
    Timeline timeline = new Timeline();
    for (String str : temp) {
        KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.1), event2 -> {
            if (i.get() > str.length()) {
                timeline.stop();
            } else {
                textArea.setText(str.substring(0, i.get()));
                i.set(i.get() + 1);
                textArea.requestFocus();
                textArea.end();
            }
        });
        voice.speak(str);
        timeline.getKeyFrames().add(keyFrame);

        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54982235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档