import javax.sound.sampled.*;
import java.io.*;
import javax.swing.*;
class muse{
// this plays music files
Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread music, Throwable ex) {
System.out.println("Uncaught exception: " + ex);
}};
String dian="yun.wav";
static void mate(String dian)
{
Thread music = new Thread(dian)
{
public void run(String dian)
{
try
{
Clip clip= null;
File in = new File(dian);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
JOptionPane.showMessageDialog(null, "Click OK to stop music");
clip.drain();
}
catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex)
{
ex.printStackTrace();
}
throw new RuntimeException();
}
};
}
public static void main(String args[]) throws InterruptedException {
music.start();
}
}
我创建了这段代码,以便我可以在另一个.java文件中输入歌曲文件的名称,但是即使netbeans中没有红色错误,代码最终也是无法编译的。 如果我删除静态void配合(String dian){} 代码工作正常,任何人都可以帮助解释它为什么不编译或允许代码从另一个.java文件运行的替代方案
答案 0 :(得分:0)
静态方法music
中声明的mate
对象在本地声明,在主方法中无法访问。
尝试修改您的示例:
static Thread mate(String diane) {
return new Thread(dian){
...
}
然后在你的主要方法
public static void main(String args[]) throws InterruptedException {
Thread music = mate("filename");
music.start();
}
答案 1 :(得分:0)
编译失败可能发生在
public static void main(String args[]) throws InterruptedException {
mate(dian);
music.start();
}
因为 music 变量未在main()可访问的作用域中定义。
轻松破解你可以将线程音乐变量设为私有静态变量,因此主要可以访问它。
private static String dian = "yun.wav";
private static Thread music = null;
static void mate(String dian){
music = new Thread(dian){...}
}
答案 2 :(得分:0)
您的代码存在多个错误或错误:
1)main方法看不到音乐,你可以声明它是一个可见的类变量
2)你正在向run()传递一个参数(现在它没有覆盖Thread类方法)所以当你在音乐上调用start()时它将不会做什么,因为它会调用Thread类的Run方法。这就是你需要删除传递给run()的String参数的原因。
3)你正在硬编码文件名。你可以从命令行把它作为输入。
class muse{
//music will be available to main method
private static Thread music ;
// this plays music files
Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread music, Throwable ex) {
System.out.println("Uncaught exception: " + ex);
}
};
static void mate(String dian){
music = new Thread(dian){
//removed argument so it can run when you call start();
@Override
public void run(){
try{
Clip clip= null;
File in = new File(dian);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
JOptionPane.showMessageDialog(null, "Click OK to stop music");
clip.drain();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
ex.printStackTrace();
}
throw new RuntimeException();
}
};
}
public static void main(String args[]) throws InterruptedException {
String dian=null;
//you can take dian as inut file from command line also using scanner
Scanner s=new Scanner(System.in);
dian=s.next();
s.close();
////////////
//call Mate() so it will create a new new music thread with clip assigned to dian;
mate(dian);
//start music
music.start();
}
}