通过 Clip 和 AudioInputStream 重放 Java 声音不起作用

发布于 2024-12-04 19:22:45 字数 846 浏览 1 评论 0原文

这是 Java Sound 信息页面的一个稍微修改过的示例。 https://stackoverflow.com/tags/javasound/info 不幸的是,它只播放声音一次,但意图是两次。

import java.io.File;
import javax.sound.sampled.*;

public class TestNoise {
    public static void main(String[] args) throws Exception {
        File f = new File("/home/brian/drip.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(f);

        AudioFormat af = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, af);
        Clip clip = (Clip)AudioSystem.getLine(info);

        clip.open(ais);
        clip.start();    // heard this
        Java.killTime(); 
        clip.start();    // NOT HEARD
        Java.killTime();
    }
}

编辑:要了解答案,请参阅 Wanderlust 提供的链接,或者按照其答案下方评论中的说明进行操作。

This is a slightly modified example from the Java Sound info page. https://stackoverflow.com/tags/javasound/info Unfortunately, it only plays the sound once but the intention is twice.

import java.io.File;
import javax.sound.sampled.*;

public class TestNoise {
    public static void main(String[] args) throws Exception {
        File f = new File("/home/brian/drip.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(f);

        AudioFormat af = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, af);
        Clip clip = (Clip)AudioSystem.getLine(info);

        clip.open(ais);
        clip.start();    // heard this
        Java.killTime(); 
        clip.start();    // NOT HEARD
        Java.killTime();
    }
}

Edit: To understand the answer, see the link provided by Wanderlust or just do what it says in the comment below his answer.

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

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

发布评论

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

评论(2

回忆追雨的时光 2024-12-11 19:22:45

这个美妙的 API 对我有用:

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

/**
 * Handles playing, stopping, and looping of sounds for the game.
 *
 * @author Langdon Staab
 * @author Tyler Tomas
 */
public class Sound {
    Clip clip;

    @SuppressWarnings("CallToPrintStackTrace")
    public Sound(String filename) {
        try (InputStream in = getClass().getResourceAsStream(filename)) {
            assert in != null;
            InputStream bufferedIn = new BufferedInputStream(in);
            try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(bufferedIn)) {
                clip = AudioSystem.getClip();
                clip.open(audioIn);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: \n" + e);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: \n" + e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: \n" + e);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: \n" + e);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // play, stop, loop the sound clip
    }

    public void play() {
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }

    public void loop() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    public void stop() {
        clip.stop();
    }

    public boolean isPlaying() {
        //return false;
        return clip.getFrameLength() > clip.getFramePosition();
    }
}

This wonderful API worked for me:

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

/**
 * Handles playing, stopping, and looping of sounds for the game.
 *
 * @author Langdon Staab
 * @author Tyler Tomas
 */
public class Sound {
    Clip clip;

    @SuppressWarnings("CallToPrintStackTrace")
    public Sound(String filename) {
        try (InputStream in = getClass().getResourceAsStream(filename)) {
            assert in != null;
            InputStream bufferedIn = new BufferedInputStream(in);
            try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(bufferedIn)) {
                clip = AudioSystem.getClip();
                clip.open(audioIn);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: \n" + e);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: \n" + e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: \n" + e);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: \n" + e);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // play, stop, loop the sound clip
    }

    public void play() {
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }

    public void loop() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    public void stop() {
        clip.stop();
    }

    public boolean isPlaying() {
        //return false;
        return clip.getFrameLength() > clip.getFramePosition();
    }
}
冷血 2024-12-11 19:22:45

要第二次播放剪辑,您必须

clip.start();
clip.stop();

在第二次调用clip.start()后调用cause;它试图从

For playing the clip for the second time you must call

clip.start();
clip.stop();

cause after the second call of clip.start(); it is trying to play the file from the place where it stopped previously.

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