Java的长度无限的AudioInputStream [英] Java Length Unlimited AudioInputStream

查看:203
本文介绍了Java的长度无限的AudioInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆code,将在运行时,产生程序的声音。不幸的是,它仅持续几秒钟。理想情况下,将运行,直到我告诉它停下来。我不是在谈论循环,算法生成它提供了2 ^ 64个样品,此刻,所以它不会在可预见的未来内用完。为的AudioInputStream的构造函数的第三输入,这是我能理想地只是删除。我可以提供一个巨大的数字,但是,似乎是错误的方式去了解它。

I have a bunch of code that will, when run, produce procedural sound. Unfortunately, it only lasts a few seconds. Ideally it would run until I tell it to stop. I'm not talking about looping, the algorithm to generate it provides 2^64 samples, at the moment, so it isn't going to run out within the foreseeable future. The constructor for AudioInputStream takes a third input, which I could ideally just remove. I could just provide a huge number, but that seems like the wrong way to go about it.

我想过使用SourceDataLine的,但是算法将理想的按需调用,运行不提前和写作的道路。思考?

I thought about using SourceDataLine, but the algorithm ideally would be called on-demand, not running ahead and writing the path. Thoughts?

推荐答案

看来我已经回答了我的问题。

It seems I've answered my own question.

在进一步的研究,使用的SourceDataLine 是要走的路,因为当你给它足够的一起工作,它会阻止。

Upon further research, using the SourceDataLine is the way to go, as it'll block when you've given it enough to work with.

对于缺乏适当的Javadoc的道歉。

Apologies for the lack of proper Javadoc.

class SoundPlayer
{
    // plays an InputStream for a given number of samples, length
    public static void play(InputStream stream, float sampleRate, int sampleSize, int length) throws LineUnavailableException
    {
        // you can specify whatever format you want...I just don't need much flexibility here
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        AudioInputStream audioStream = new AudioInputStream(stream, format, length);
        Clip clip = AudioSystem.getClip();
        clip.open(audioStream);
        clip.start();
    }

    public static void play(InputStream stream, float sampleRate, int sampleSize) throws LineUnavailableException
    {
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        SourceDataLine line = AudioSystem.getSourceDataLine(format);
        line.open(format);
        line.start();
        // if you wanted to block, you could just run the loop in here
        SoundThread soundThread = new SoundThread(stream, line);
        soundThread.start();
    }

    private static class SoundThread extends Thread
    {
        private static final int buffersize = 1024;

        private InputStream stream;
        private SourceDataLine line;

        SoundThread(InputStream stream, SourceDataLine line)
        {
            this.stream = stream;
            this.line = line;
        }

        public void run()
        {
            byte[] b = new byte[buffersize];
            // you could, of course, have a way of stopping this...
            for (;;)
            {
                stream.read(b);
                line.write(b, 0, buffersize);
            }
        }
    }
}

这篇关于Java的长度无限的AudioInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆