Java - 如何从文件中播放.wav和.mp3等音频文件

时间:2016-12-26 12:28:01

标签: java file audio playing

我一直在寻找一些方法来做到这一点,但我发现在网上没有任何想法可以帮助我。

我创建了一个新文件,如:File f = new File("path/to/.wav/file"); 但是我找不到任何方法来播放它,我尝试使用网络代码,但它们没有用。

我甚至有这个,但不会工作:

String wav = ".\\Others\\audio\\timesup.wav";
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
                  audioPlayer.class.getResourceAsStream(".\\Others\\audio\\timesup.wav"));
        clip.open(inputStream);
        clip.start(); 

你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

这可能会对你有帮助。

String soundFile = "path/to/.wav/file";
InputStream in = new FileInputStream(soundFile );

// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);

// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);

请点击此链接。 http://alvinalexander.com/java/java-audio-example-java-au-play-sound

答案 1 :(得分:1)

如果没有外部API,还有另一种方法可以做到这一点

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AePlayWave extends Thread {

    protected static final boolean DEBUG = false;

    protected InputStream inputStream;

    public AePlayWave(InputStream inputStream) {
        this.inputStream = inputStream;
        if (DEBUG) System.out.println("AePlayWave constructor");
    }

    @Override
    public void run() {
        if (DEBUG) System.out.println("AePlayWave running");

        AudioInputStream audioInputStream = verifyInputStream();
        if (audioInputStream == null) {
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine audioLine = openInputStream(format);

        if (DEBUG) System.out.println(audioLine.getLineInfo());

        if (audioLine != null) {
            audioLine.start();
            playInputStream(audioInputStream, audioLine);
        }
    }

    protected AudioInputStream verifyInputStream() {
        if (DEBUG) System.out.println("AePlayWave verifyInputStream");
        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(inputStream);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return audioInputStream;
    }

    protected SourceDataLine openInputStream(AudioFormat format) {
        if (DEBUG) System.out.println("AePlayWave openInputStream");
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine audioLine = null;
        if (DEBUG) System.out.println("AePlayWave openInputStream try");
        try {
            audioLine = (SourceDataLine) AudioSystem.getLine(info);
            if (DEBUG) System.out.println("AePlayWave openInputStream getLine");
            audioLine.open(format);
            if (DEBUG) System.out.println("AePlayWave openInputStream open");
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return audioLine;
    }

    protected void playInputStream(AudioInputStream audioInputStream,
            SourceDataLine audioLine) {
        if (DEBUG) System.out.println("AePlayWave playInputStream");
        int externalBufferSize = (int) audioInputStream.getFrameLength() * 4;
        if (DEBUG) System.out.println("AePlayWave playInputStream externalBufferSize: " 
                + externalBufferSize);
        int nBytesRead = 0;
        byte[] abData = new byte[externalBufferSize];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) {
                    if (DEBUG) System.out.println("Bytes read: " + nBytesRead);
                    audioLine.write(abData, 0, nBytesRead);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            audioLine.drain();
            audioLine.close();
        }
    }
}

您可以将其用作

InputStream inputStream = getClass().getResourceAsStream("alarm-clock-1.wav");
AePlayWave playWave = new AePlayWave(inputStream);
playWave.run();