Java 中的音乐循环
再会!
我正在做一个游戏,我希望它有背景声音。我为它创建了一个类,并在我的 main 上调用它。我的代码如下:
import sun.audio.*;
import java.io.*;
public class Sound {
public void music() {
AudioStream backgroundMusic;
AudioData musicData;
AudioPlayer musicPlayer = AudioPlayer.player;
ContinuousAudioDataStream loop = null;
try {
backgroundMusic = new AudioStream(new FileInputStream("chickendance.wav"));
musicData = backgroundMusic.getData();
loop = new ContinuousAudioDataStream(musicData);
musicPlayer.start(loop);
} catch (IOException error) { System.out.println(error);
}
}
}
这是我调用它的主类。
public class HangmanLauncher extends javax.swing.JFrame {
public HangmanLauncher() {
initComponents();
Sound sound = new Sound();
sound.music();
}
我的问题是音乐不播放。 错误:java.io.IOException:无法从输入流创建音频流。
这是什么意思?我的文件类型是 Microsoft Wave Sound Format,大小为 796kb。我可以知道我做错了什么吗?我们将非常感谢您的建议。先感谢您。
Good day!
I am doing a game and I want it to have a background sound. I created a class for it and I call it on my main. My code is as follows:
import sun.audio.*;
import java.io.*;
public class Sound {
public void music() {
AudioStream backgroundMusic;
AudioData musicData;
AudioPlayer musicPlayer = AudioPlayer.player;
ContinuousAudioDataStream loop = null;
try {
backgroundMusic = new AudioStream(new FileInputStream("chickendance.wav"));
musicData = backgroundMusic.getData();
loop = new ContinuousAudioDataStream(musicData);
musicPlayer.start(loop);
} catch (IOException error) { System.out.println(error);
}
}
}
This is my main class where i call it.
public class HangmanLauncher extends javax.swing.JFrame {
public HangmanLauncher() {
initComponents();
Sound sound = new Sound();
sound.music();
}
My problem is that the music doesn't play. Error: java.io.IOException: could not create audio stream from input stream.
What does it mean? The type of my file is Microsoft Wave Sound Format and its size is 796kb. May I know what I am doing wrong? Your suggestions will be highly appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我可以使用以下代码播放 .wav 文件。
请注意,如果您使用 JFrame,您可能希望在线程中播放声音文件,以便可以继续其他操作。
I can play .wav files using the following code.
Mind you if you are using a JFrame you will likely want to play your sound file in a Thread so you can continue other operations.
所以我一直在尝试自己做这件事,最后我找到了如何实现它
此特定代码从给定的目录中随机选择一个曲目,然后循环选择另一个随机文件
循环位于从 run() 方法调用的 startPlayback() 中,因为这是一个单独的线程,因此不会停止程序执行
}
so i've been trying to do this myself and i found how to implement it finally
This particular code chooses a track at random from the directory given and then loop choosing another random file
The loop is in the startPlayback() which is called from the run() method, as this is a seperate thread this will not stop program execution
}
我的猜测是
wav
文件已以AudioStream
类无法理解的格式进行编码。我找不到该类的文档 (??),但我会尝试另一个不是Microsoft Wave Sound
的文件。同样,我不知道该编码的具体情况,但它可能是 Microsoft 专有的,因此不在AudioStream
的 Sun 实现中。My guess is that the
wav
file has been encoded in a format theAudioStream
class doesn't understand. I couldn't find the docs for the class (??) but I would try another file that isn'tMicrosoft Wave Sound
. Again, don't know the specifics of that encoding but it being Microsoft it's probably proprietary and therefore not in the Sun implementation of theAudioStream
.我相信这只是 mp3,但如果这是一个选项,请查看 JLayer 和朋友: http://www .javazoom.net/projects.html
I believe this is mp3-only, but if that's an option, check out JLayer and friends: http://www.javazoom.net/projects.html
我从这样的 Wave 中生成一个 AudioInputStream:
但我不播放它。
I produce an AudioInputStream from a Wave like this:
I don't play it though.
我使用 .au 文件格式并且它有效。 :)
I Used .au file format and it worked. :)
我尝试了许多不同的播放音频的方式,我发现这是我最成功的类文件。
I have tried many different ways of playing audio, and I have found this to be my most successful class file that does so.
感谢您的信息,在上述帮助和其他一些帮助下,我有了这个有效的代码:
thanks for the info, with above help and some others i have this code which works: