方法以红色下划线。我试图弄清楚为什么会发生这种情况,但我总是遇到无法解决的方法。我在主TextoToSpeechExample1类中有这些方法,并在gui类中调用这些方法。
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();
}
});
}
}`
我希望调用这些方法。如果你能回顾一下,看看我做错了什么,我会非常感激的。这是我自己正在做的一个项目。
发布于 2022-11-25 22:28:21
这对StackOverflow来说不是个好问题,但无论如何.
据我所见,您在主方法中实现了两种方法-- start和pause -但是您需要在它之外实现它们。
试试这个:
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();
}
});
}
}发布于 2022-11-25 22:34:51
快速查看您的代码,有几件事情会跳出:
public static void main(..)函数中声明一个新的函数static void start()。这在java是不允许的。您应该在类本身内声明函数: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类中声明的函数。
发布于 2022-11-25 22:48:41
空白开始和暂停在您的TextoToSpeechExample1类的主空中,从主空中删除它们,它不会产生错误。
https://stackoverflow.com/questions/74578252
复制相似问题