首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的方法start()和not ()在括号中加了下划线,我不知道为什么?

我的方法start()和not ()在括号中加了下划线,我不知道为什么?
EN

Stack Overflow用户
提问于 2022-11-25 22:16:53
回答 3查看 40关注 0票数 0

方法以红色下划线。我试图弄清楚为什么会发生这种情况,但我总是遇到无法解决的方法。我在主TextoToSpeechExample1类中有这些方法,并在gui类中调用这些方法。

代码语言:javascript
复制
class TextToSpeechExample1 {
    public static void main(String args[]) throws IOException, EngineException, AudioException, InterruptedException {

    static void start(){
            // code to be executed
            String fileName = "/Users/stevenshivayka/Documents/kjv.txt";
            Path path = Paths.get(fileName);
            byte[] bytes = Files.readAllBytes(path);
            List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);

//setting properties as Kevin Dictionary
            System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us" + ".cmu_us_kal.KevinVoiceDirectory");
//registering speech engine
            Central.registerEngineCentral("com.sun.speech.freetts" + ".jsapi.FreeTTSEngineCentral");
//create a Synthesizer that generates voice
            Synthesizer synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
//allocates a synthesizer
            synthesizer.allocate();
//resume a Synthesizer
            synthesizer.resume();
//speak the specified text until the QUEUE become empty
            synthesizer.speakPlainText(allLines.toString(), null);
            synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
//deallocating the Synthesizer
            synthesizer.deallocate();
    }

    public static void pause(){
                Synthesizer synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
                synthesizer.allocate();
                synthesizer.resume();
                synthesizer.deallocate();
                synthesizer.pause();
        }
    }
}
    class gui {
        public static void main(String args[]) {
            JFrame frame = new JFrame("Babbel Audio Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setLayout(new FlowLayout());

            JButton button = new JButton("Start Audio");
            frame.add(button); // Adds Button to content pane of frame
            frame.setVisible(true);

            JButton button2 = new JButton("Pause Audio");
            frame.add(button2);
            frame.setVisible(true);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //your actions
                    TextToSpeechExample1.start();

                }

            });

            button2.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //your actions
                    pause();
                }
            });
        }
    }

`

我希望调用这些方法。如果你能回顾一下,看看我做错了什么,我会非常感激的。这是我自己正在做的一个项目。

EN

回答 3

Stack Overflow用户

发布于 2022-11-25 22:28:21

这对StackOverflow来说不是个好问题,但无论如何.

据我所见,您在主方法中实现了两种方法-- start和pause -但是您需要在它之外实现它们。

试试这个:

代码语言:javascript
复制
class TextToSpeechExample1 {
    public static void main(String args[]) throws IOException, EngineException, AudioException, InterruptedException {}

    static void start(){
            // code to be executed
            String fileName = "/Users/stevenshivayka/Documents/kjv.txt";
            Path path = Paths.get(fileName);
            byte[] bytes = Files.readAllBytes(path);
            List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);

//setting properties as Kevin Dictionary
            System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us" + ".cmu_us_kal.KevinVoiceDirectory");
//registering speech engine
            Central.registerEngineCentral("com.sun.speech.freetts" + ".jsapi.FreeTTSEngineCentral");
//create a Synthesizer that generates voice
            Synthesizer synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
//allocates a synthesizer
            synthesizer.allocate();
//resume a Synthesizer
            synthesizer.resume();
//speak the specified text until the QUEUE become empty
            synthesizer.speakPlainText(allLines.toString(), null);
            synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
//deallocating the Synthesizer
            synthesizer.deallocate();
    }

    public static void pause(){
                Synthesizer synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
                synthesizer.allocate();
                synthesizer.resume();
                synthesizer.deallocate();
                synthesizer.pause();
        }
    }

    class gui {
        public static void main(String args[]) {
            JFrame frame = new JFrame("Babbel Audio Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setLayout(new FlowLayout());

            JButton button = new JButton("Start Audio");
            frame.add(button); // Adds Button to content pane of frame
            frame.setVisible(true);

            JButton button2 = new JButton("Pause Audio");
            frame.add(button2);
            frame.setVisible(true);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //your actions
                    TextToSpeechExample1.start();

                }

            });

            button2.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //your actions
                    TextToSpeechExample1.pause();
                }
            });
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2022-11-25 22:34:51

快速查看您的代码,有几件事情会跳出:

  1. 您正试图在public static void main(..)函数中声明一个新的函数static void start()。这在java是不允许的。您应该在类本身内声明函数:

代码语言:javascript
复制
class TextToSpeechExample1 {
   static void start() { ...your code here... }
   public static void pause() { ... your code here ... }
   public static void main (String args[]) { ... your code here ...}
}

您可能还应该了解Java中访问修饰符/说明符的基础知识。例如,您将无法从另一个类调用private函数或在private类中声明的函数。

票数 0
EN

Stack Overflow用户

发布于 2022-11-25 22:48:41

空白开始和暂停在您的TextoToSpeechExample1类的主空中,从主空中删除它们,它不会产生错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74578252

复制
相关文章

相似问题

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