无法播放音乐的声音,没有Java的错误
我正在尝试运行自己的音乐,但它行不通。不会再在日食中出现错误,但是声音没有播放。这是我的代码,我
public class Music extends Thread {
private Player player;
private boolean isLoop;
private File file;
private FileInputStream fis;
private BufferedInputStream bis;
public Music(String name, boolean isLoop)
{
try {
this.isLoop = isLoop;
//Find the link to the file and play, save the file to the buffer
file = new File(Main.class.getResource("/music/" + name).toURI());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} catch(Exception e){
System.out.println("No music");
}
}
//Get the time of the music, how long it is played
public int getTime() {
if(player == null)
{
return 0;
}
return player.getPosition();
}
//Stop the music played
public void close() {
isLoop = false;
player.close();
this.interrupt();
}
@Override
public void run() {
try {
player.play();
do {
player.play();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} while(isLoop);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
谢谢您的帮助
I am trying to run the music that I have, but it does not work. There is no error showing up in the eclipse any more, but the sound is not played. This is my code that I have
public class Music extends Thread {
private Player player;
private boolean isLoop;
private File file;
private FileInputStream fis;
private BufferedInputStream bis;
public Music(String name, boolean isLoop)
{
try {
this.isLoop = isLoop;
//Find the link to the file and play, save the file to the buffer
file = new File(Main.class.getResource("/music/" + name).toURI());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} catch(Exception e){
System.out.println("No music");
}
}
//Get the time of the music, how long it is played
public int getTime() {
if(player == null)
{
return 0;
}
return player.getPosition();
}
//Stop the music played
public void close() {
isLoop = false;
player.close();
this.interrupt();
}
@Override
public void run() {
try {
player.play();
do {
player.play();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} while(isLoop);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Thank you for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题从这里开始 - >
file = new File(main.class.getResource(“/music/” + name).touri());
不能将嵌入的资源引用为
file> file
,因为那不是。而是直接使用类#GetResourCeasStream
。接下来,您应该避免从
thread
延伸,线程不是重新输入的,也就是说,一旦停止,就无法重新启动它们。而是实现运行
,例如...Your problem starts here ->
file = new File(Main.class.getResource("/music/" + name).toURI());
An embedded resource can't be reference as a
File
, because, well, it's not. Instead useClass#getResourceAsStream
directly.Next, you should avoid extending from
Thread
, thread's are not re-entrant, that is, once stopped, you can't restart them. Instead, implementRunnable
, for example...