1

再会!

我正在做一个游戏,我希望它有背景声音。我为它创建了一个类,并在我的主目录中调用它。我的代码如下:

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。我可以知道我做错了什么吗?您的建议将不胜感激。先感谢您。

4

8 回答 8

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();
               }
于 2011-02-02T16:13:41.653 回答
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;
}

}

于 2012-04-02T12:30:07.873 回答
2

我相信这只是 mp3,但如果这是一个选项,请查看 JLayer 和朋友:http ://www.javazoom.net/projects.html

于 2011-02-02T13:42:55.323 回答
2

我从这样的 Wave 生成​​ AudioInputStream:

AudioSystem.getAudioInputStream(new FileInputStream("chickendance.wav"));

虽然我不玩。

于 2011-02-02T13:55:34.507 回答
2

我的猜测是该wav文件已以AudioStream类不理解的格式编码。我找不到该课程的文档(??),但我会尝试另一个不是Microsoft Wave Sound. 同样,不知道该编码的细节,但它是 Microsoft,它可能是专有的,因此不在 Sun 的AudioStream.

于 2011-02-02T14:06:34.390 回答
0

我使用了 .au 文件格式,它工作正常。:)

于 2011-02-02T15:52:27.133 回答
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[]){
    }
}
于 2015-04-13T02:22:03.290 回答
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();
    }
}
于 2016-06-30T20:12:52.127 回答