-1

(对不起,不是说英语的人,预计会有很多语法/句法错误)

我正在开发一款软件来管理 D-Link Ip Cam(DCS-xxxx 系列和其他)。因为这台相机暴露了一个音频流(有些型号甚至有一个用于双向通信的扬声器),我想根据用户的要求播放它。

所有入口点都在 http 基本身份验证之后(但奇怪的是我不能使用 http:\USER:PASS@192.168.1.100,因为我得到了 401)。

我使用 javax.sound.* 包来做到这一点,但由于某种原因,音频在15 到 20 秒后开始播放,总延迟为 30-40 秒 编辑:平均 45 秒,但音频是从开始,所以情况更糟。

这是课程(最低限度,仅用于测试目的)

import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlayer implements Runnable{

    private URL URL;
    private String USERNAME;
    private String PASSWORD;

    private volatile boolean stop = false;

    public AudioPlayer(String url, String user, String pass) throws MalformedURLException{
        this.URL = new URL(url);
        this.USERNAME = user;
        this.PASSWORD = pass;
    }

    public void shutdown() {
        stop = true;
    }

    @Override
    public void run() {
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
            }
        });

        try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(URL);
            clip.open(inputStream);
            clip.start();
            while(!stop && clip.isRunning()) {}
            clip.stop();
            System.err.println("AUDIO PLAYER STOPPED");
        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }

}

需要 Authenticator 部分,因为 ipcam 使用基本的 http autentication。

我在某处读过,AudioSystem使用不同的算法进行多次传递以获得正确的传递,然后将流重置到开头,然后才开始播放。因此,正因为如此,可能AudioSystem在意识到要使用哪种类型的编解码器(可能需要某种标头)方面存在一些问题,并且在开始播放音频之前花了相当长的时间。

值得知道的是,即使是 VLC 也很难跟上流媒体播放的速度,在播放前最多会损失 8 秒(8 秒比 20 秒好得多)。IpCam 在本地网络上。

我的代码有问题吗?一些我看不到的方法?

真的不知道在哪里看这个。

我无法在这里或其他地方找到任何有意义的答案。

4

1 回答 1

0

在摆弄一个答案后,我找到了解决方案,提供 1 到 2 秒的延迟(这与官方应用程序或网页配置的延迟相同,非常完美)。

private void playStreamedURL() throws IOException {

        //to avoid 401 error
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                //USERNAME and PASSWORD are defined in the class
                return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray()); 
            }
        });

        AudioInputStream AIS = null;
        SourceDataLine line = null;

        try {
//get the input stream
            AIS = AudioSystem.getAudioInputStream(this.URL);

//get the format, Very Important!
            AudioFormat format = AIS.getFormat();
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

//create the output line
            line = (SourceDataLine) AudioSystem.getLine(info);
//open the line with the specified format (other solution manually create the format
//and thats is a big problem because things like sampleRate aren't standard
//For example, the IpCam i use for testing use 11205 as sample rate.
            line.open(format);

            int framesize = format.getFrameSize();

//NOT_SPECIFIED is -1, wich create problem with the buffer definition, so it's revalued if necessary
            if(framesize == AudioSystem.NOT_SPECIFIED)
                framesize = 1;

//the buffer used to read and write bytes from stream to audio line
            byte[] buffer = new byte[4 * 1024 * framesize];
            int total = 0;

            boolean playing = false;
            int r, towrite, remaining;
            while( (r = AIS.read(buffer, total, buffer.length - total)) >= 0 ) { //or !=-1
                total += r;

//avoid start the line more than one time
                if (!playing) {
                    line.start();
                    playing = true;
                }

//actually play the sound (the frames in the buffer)
                towrite = (total / framesize) * framesize;
                line.write(buffer, 0, towrite);

//if some byte remain, overwrite them into the buffer and change the total
                remaining = total - towrite;
                if (remaining > 0)
                    System.arraycopy(buffer, towrite, buffer, 0, remaining);
                total = remaining;
            }

//line.drain() can be used, but it will consume the rest of the buffer.
            line.stop();
            line.flush();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        } finally {
            if (line != null)
                line.close();
            if (AIS != null)
                AIS.close();
        }

    }

尽管如此,可以进行一些优化,但它确实有效。

于 2019-04-14T09:15:45.173 回答