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

发布于 2024-11-16 02:29:02 字数 1802 浏览 4 评论 0原文

如何在到达结尾时循环播放*.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;

请帮助我。

How to play *.wav loop when reach end?

My code look like this:

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;

Help me please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归途 2024-11-23 02:29:02

我创建这个类是为了在单独的线程中播放 .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();
        }
}

干杯!

I created this class to play .wav sound in a separate thread. Feel free to use and adapt if you want:

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();
        }
}

Cheers!

醉生梦死 2024-11-23 02:29:02
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;
    }
}
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;
    }
}
仅冇旳回忆 2024-11-23 02:29:02

如果是简短的声音示例,请使用 剪辑。使用 loop(int) 方法设置重复次数。

请参阅此处一个例子

If it is a short sound sample, use a Clip. Use the loop(int) method to set how many times to repeat.

See here for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文