首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java应用程序中播放音频

如何在Java应用程序中播放音频
EN

Stack Overflow用户
提问于 2011-01-17 05:41:51
回答 2查看 7.6K关注 0票数 1

我正在制作一个java应用程序,我需要播放音频。我主要播放我的大炮射击(这是一个大炮射击游戏)和子弹爆炸的小声音文件,尽管我计划有循环的背景音乐。我已经找到了两种不同的方法来实现这一点,但这两种方法都不能按我想要的方式工作。

第一种方法是字面上的一种方法:

代码语言:javascript
复制
        public void playSoundFile(File file) {//http://java.ittoolbox.com/groups/technical-functional/java-l/sound-in-an-application-90681

        try {
//get an AudioInputStream
            AudioInputStream ais = AudioSystem.getAudioInputStream(file);
//get the AudioFormat for the AudioInputStream
            AudioFormat audioformat = ais.getFormat();
            System.out.println("Format: " + audioformat.toString());
            System.out.println("Encoding: " + audioformat.getEncoding());
            System.out.println("SampleRate:" + audioformat.getSampleRate());
            System.out.println("SampleSizeInBits: " + audioformat.getSampleSizeInBits());
            System.out.println("Channels: " + audioformat.getChannels());
            System.out.println("FrameSize: " + audioformat.getFrameSize());
            System.out.println("FrameRate: " + audioformat.getFrameRate());
            System.out.println("BigEndian: " + audioformat.isBigEndian());
//ULAW format to PCM format conversion
            if ((audioformat.getEncoding() == AudioFormat.Encoding.ULAW)
                    || (audioformat.getEncoding() == AudioFormat.Encoding.ALAW)) {
                AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                        audioformat.getSampleRate(),
                        audioformat.getSampleSizeInBits() * 2,
                        audioformat.getChannels(),
                        audioformat.getFrameSize() * 2,
                        audioformat.getFrameRate(), true);
                ais = AudioSystem.getAudioInputStream(newformat, ais);
                audioformat = newformat;
            }

//checking for a supported output line
            DataLine.Info datalineinfo = new DataLine.Info(SourceDataLine.class, audioformat);
            if (!AudioSystem.isLineSupported(datalineinfo)) {
                //System.out.println("Line matching " + datalineinfo + " is not supported.");
            } else {
                //System.out.println("Line matching " + datalineinfo + " is supported.");
//opening the sound output line
                SourceDataLine sourcedataline = (SourceDataLine) AudioSystem.getLine(datalineinfo);
                sourcedataline.open(audioformat);
                sourcedataline.start();
//Copy data from the input stream to the output data line
                int framesizeinbytes = audioformat.getFrameSize();
                int bufferlengthinframes = sourcedataline.getBufferSize() / 8;
                int bufferlengthinbytes = bufferlengthinframes * framesizeinbytes;
                byte[] sounddata = new byte[bufferlengthinbytes];
                int numberofbytesread = 0;
                while ((numberofbytesread = ais.read(sounddata)) != -1) {
                    int numberofbytesremaining = numberofbytesread;
                    sourcedataline.write(sounddata, 0, numberofbytesread);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这样做的问题是,我的整个程序会停止,直到声音文件完成,或者至少接近完成。

第二种方法是:

代码语言:javascript
复制
    File file = new File("Launch1.wav");
    AudioClip clip;
    try {
        clip = JApplet.newAudioClip(file.toURL());
        clip.play();
    } catch (Exception e) {
        e.getMessage();
    }

我这里的问题是,每次声音文件提前结束或根本不播放,取决于我放置代码的位置。

他们有没有办法在没有上述问题的情况下播放声音?我做错了什么吗?任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-17 05:47:41

对于第一种方法,您必须为音频创建另一个线程。

例如:

代码语言:javascript
复制
new Thread(
            new Runnable() {
                public void run() {
                    try {
                        // PLAY AUDIO CODE
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

当然,您必须确保先前的声音不会继续播放。

票数 6
EN

Stack Overflow用户

发布于 2011-01-17 05:49:32

您应该创建一个处理音频播放的线程。但要确保它能够混合声音,这样在彼此之后发生的两个镜头可以在正确的时间播放它们的声音,而不需要等待前一个声音结束。应该有为你做混合的框架。

一个很好的起点是:http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4708254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档