如何在Java中播放* .wav循环?

时间:2011-06-17 15:07:27

标签: java audio javasound

到达终点时如何播放* .wav循环?

我的代码如下:

public class SoundPlayer implements Runnable{
public SoundPlayer(String filename){
    is=Main.class.getResourceAsStream("sounds/"+filename);
}
@Override
public void run() {
    // TODO Auto-generated method stub
    playSound();
}

public void playSound(){
    try {
        audioStream = AudioSystem.getAudioInputStream(is);
    } catch (Exception e){
        e.printStackTrace();
       System.exit(1);
    }

    audioFormat = audioStream.getFormat();

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    try {
        sourceLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    sourceLine.start();

    int nBytesRead = 0;
    int bufferSize = audioFormat.getFrameSize() *
    Math.round(audioFormat.getSampleRate() / 10);
    byte[] abData = new byte[bufferSize];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
        else {
            audioStream.reset(); //I add this code but didn't effect
        }
    }

    sourceLine.drain();
    sourceLine.close();
}


private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
private InputStream is;

请帮帮我。

3 个答案:

答案 0 :(得分:1)

我创建了这个类,在一个单独的线程中播放.wav声音。如果您愿意,请随意使用和改编:

import java.io.*;
import javax.sound.sampled.*;
public class CPSound implements Runnable
{
        String fileLocation = "alarm.wav";
        CPSound()
        {

        }
        public void play(String fileName)
        {
                Thread t = new Thread(this);
                fileLocation = fileName;
                t.start();
        }
    public void run ()
    {
        playSound(fileLocation);
    }
        private void playSound(String fileName)
        {
                File    soundFile = new File(fileName);
                AudioInputStream        audioInputStream = null;
                try
                {
                        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                AudioFormat     audioFormat = audioInputStream.getFormat();
                SourceDataLine  line = null;
                DataLine.Info   info = new DataLine.Info(SourceDataLine.class,audioFormat);
                try
                {
                        line = (SourceDataLine) AudioSystem.getLine(info);
                        line.open(audioFormat);
                }
                catch (LineUnavailableException e)
                {
                        e.printStackTrace();
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                line.start();
                int     nBytesRead = 0;
                byte[]  abData = new byte[128000];
                while (nBytesRead != -1)
                {
                        try
                        {
                                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                        }
                        catch (IOException e)
                        {
                                e.printStackTrace();
                        }
                        if (nBytesRead >= 0)
                        {
                                int     nBytesWritten = line.write(abData, 0, nBytesRead);
                        }
                }
                line.drain();
                line.close();
        }
}

干杯!

答案 1 :(得分:1)

import java.io.File;

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

public class Sound_player implements Runnable {
    private static boolean play_music = false;
    private AudioInputStream audioInputStream = null;
    private File soundFile;
    private static String filename;
    private int EXTERNAL_BUFFER_SIZE = 2048;

    public void run(){
        while(true){
            initialize();
        }
    }

    public void setFile(String file){
        filename = file;
    }

    private boolean initialize(){

        soundFile = new File(filename);

        if(!soundFile.exists()){
            System.err.println("Wav file not found: " + filename);
            return false;
        }


        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }

        AudioFormat format = audioInputStream.getFormat();

        SourceDataLine auline = null;

        // describe a desired line
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try
        {
            auline = (SourceDataLine) AudioSystem.getLine(info);

            auline.open();
        } catch (Exception e){
            System.err.println(e);
            return false;
        }

        // Allows line to engage in data i/o
        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
        try
        {
            while(nBytesRead != -1){
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                auline.write(abData, 0, nBytesRead);
                }
            }
        } catch(Exception e){
            System.err.println(e);
            return false;
        }
        finally
        {
            auline.drain();

            auline.close();
        }
        return true;
    }
}

答案 2 :(得分:0)

如果是短声音样本,请使用Clip。使用loop(int)方法设置重复次数。

请参阅此处查看an example