Home > database >  My Methods start() and pause() are underlined at the parenthesis, I am not sure why?
My Methods start() and pause() are underlined at the parenthesis, I am not sure why?

Time:11-26

Methods are underlined in red. I have tried to figure out why this is happening, but I keep running into cannot resolve method. I have the methods in the main TextoToSpeechExample1 class and am invoking the methods in my gui class.


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();
                }
            });
        }
    }

`

I expected the methods to be invoked. Please if you can review this and see what I am doing wrong I would greatly appreciate it. This is a project I am working on myself.

CodePudding user response:

This isn't a good question for StackOverflow but anyway...

As far as I can see it, you implemented the two methodes start and pause in the main methode but you need to implement them outside of it.

Try this:

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();
                }
            });
        }
    }

CodePudding user response:

Quickly looking over your code, a few things jump out:

  1. You're attempting to declare a new function static void start() inside your public static void main(..) function. This isn't allowed in java. You should be declaring your functions inside the class itself:
class TextToSpeechExample1 {
   static void start() { ...your code here... }
   public static void pause() { ... your code here ... }
   public static void main (String args[]) { ... your code here ...}
}

You should probably also look into the basics of access modifiers/specifiers in Java. You won't be able to call a private function, or a function declared in a private class from another class, for example.

CodePudding user response:

The void start and pause are inside the main void of your TextoToSpeechExample1 class, remove them from inside the main void and it will not give an error.

  •  Tags:  
  • java
  • Related