美好的一天!
我正在做一个游戏,我希望它有背景声音。我为它创建了一个类,我在我的主要部分调用它。我的代码如下:
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();
}
我的问题是音乐没有播放。 Error: java.io.IOException: could not create audio stream from input stream.
这是什么意思?我的文件类型是Microsoft Wave Sound Format,其大小为796kb。我可以知道我做错了什么吗?您的建议将受到高度赞赏。提前谢谢。
答案 0 :(得分:3)
我可以使用以下代码播放.wav文件。
请注意,如果您使用的是JFrame,您可能希望在线程中播放声音文件,以便继续其他操作。
import javax.sound.sampled.*;
import java.io.*;
import javax.swing.*;
AudioInputStream as1 = AudioSystem.getAudioInputStream(new java.io.FileInputStream("chickenDance.wav"));
AudioFormat af = as1.getFormat();
Clip clip1 = AudioSystem.getClip();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Line line1 = AudioSystem.getLine(info);
if ( ! line1.isOpen() )
{
clip1.open(as1);
clip1.loop(Clip.LOOP_CONTINUOUSLY);
clip1.start();
}
答案 1 :(得分:3)
所以我一直在努力做到这一点,我终于找到了如何实现它 此特定代码从给定目录中随机选择一个轨道,然后循环选择另一个随机文件 循环在startPlayback()中,它是从run()方法调用的,因为这是一个单独的线程,不会停止程序执行
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/**
*
* @author Mangusbrother
*/
public class MusicPlayer extends Thread {private AudioStream as;
private AudioPlayer p;
private boolean playback;
public void run() {
startPlayback();
}
private void setRandom() {
File[] files = getTracks();
try {
String f = files[(int) (Math.random() * (files.length - 1))].getAbsolutePath();
System.out.println("Now Playing: " + f);
as = new AudioStream(new FileInputStream(f));
} catch (IOException ex) {
Logger.getLogger(MusicPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void startPlayback() {
playback = true;
setRandom();
p.player.start(as);
try {
do {
} while (as.available() > 0 && playback);
if (playback) {
startPlayback();
}
} catch (IOException ex) {
Logger.getLogger(MusicPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void stopPlayback() {
playback = false;
p.player.stop(as);
}
private File[] getTracks() {
File dir = new File(System.getProperty("user.dir") + "\\music");
File[] a = dir.listFiles();
ArrayList<File> list = new ArrayList<File>();
for (File f : a) {
if (f.getName().substring(f.getName().length() - 3, f.getName().length()).equals("wav")) {
list.add(f);
}
}
File[] ret = new File[list.size()];
for (int i = 0; i < list.size(); i++) {
ret[i] = list.get(i);
}
return ret;
}
}
答案 2 :(得分:2)
我相信这只是mp3,但如果这是一个选项,请查看JLayer和朋友:http://www.javazoom.net/projects.html
答案 3 :(得分:2)
我从Wave生成一个AudioInputStream:
AudioSystem.getAudioInputStream(new FileInputStream("chickendance.wav"));
我不玩它。
答案 4 :(得分:2)
我的猜测是wav
文件已经以AudioStream
类不理解的格式编码。我找不到类的文档(??)但我会尝试另一个不是Microsoft Wave Sound
的文件。同样,不知道该编码的细节,但它是Microsoft,它可能是专有的,因此不在AudioStream
的Sun实现中。
答案 5 :(得分:0)
我使用.au文件格式并且它有效。 :)
答案 6 :(得分:0)
我尝试了许多不同的音频播放方式,我发现这是我最成功的类文件。
package Classes;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
public class SoundLoader2 implements Runnable{
public AudioPlayer Player = AudioPlayer.player;
public AudioStream Stream = null;
public AudioData data=null;
InputStream inputStream=null;
InputStream inputStream2=null;
InputStream Stream2=null;
public ContinuousAudioDataStream loop;
public String url;
public URL Url;
public boolean repeat;
public SoundLoader2(String url,boolean repeat)throws IOException{
this.repeat=repeat;
Url=getClass().getResource(url);
inputStream = Url.openStream();
inputStream2 = Url.openStream();
Stream=new AudioStream(inputStream);
data=Stream.getData();
loop = new ContinuousAudioDataStream(data);
Stream2=new AudioStream(inputStream2);
}
public void play(){
if(this.repeat==true){
Player.start(loop);
}
else if(this.repeat==false);
Player.start(Stream2);
}
@Override
public void run() {
play();
}
public static void main(String args[]){
}
}
答案 7 :(得分:0)
感谢您的信息,以上帮助和其他一些我有这个代码可以工作:
public void play(InputStream inputStream) {
try {
AudioInputStream soundIn = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream) );
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(soundIn);
clip.start();
sleep(clip.getMicrosecondLength() / 1000);// Thread.yield();
} catch (Exception e) {
log.warn("could not play");
e.printStackTrace();
}
}
private void sleep(long sleep) {
try {
Thread.sleep(sleep);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}