【问题标题】:How to play a binary stream in Java如何在 Java 中播放二进制流
【发布时间】:2014-02-13 19:33:55
【问题描述】:

这个问题可能甚至不会被正确地提出,但我向你保证我会尽力而为。场景如下:我编写了一个从服务器接收音频流的小 Java 应用程序。现在,当我将二进制流重定向到一个文件,并将该文件传送到 mplayer 时,音频可以正确播放。我现在想要的是从我自己的应用程序中播放音频。到目前为止,这是我得到的:

mplayer 用于播放流的编解码器: 音频:22050 Hz,2 ch,s16le,0.0 kbit/0.00%(比率:0->88200) 选择的音频编解码器:[ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))

到目前为止我编码的内容:

public class StreamPlayer {
    public final AudioFormat audioFormat;
    public final DataLine.Info info;
    public final SourceDataLine soundLine;

    public StreamPlayer() throws LineUnavailableException {
        audioFormat = new AudioFormat(22050, 16, 2, true, true);
        info = new DataLine.Info(SourceDataLine.class, audioFormat, 1500);
        soundLine = (SourceDataLine) AudioSystem.getLine(info);
    }

    public void startSoundLine() throws LineUnavailableException {
        soundLine.open(audioFormat);
        soundLine.start();
    }

    public void playStream(byte[] buffer) {
        soundLine.write(buffer, 0, buffer.length);
    }
}

我为每个收到的数据包调用 playStream 函数。没有报错,也听不到声音。我是不是快要做到这一点了,还是遥遥无期?

附:我在 google 上找到了一些第三方库,但我真的很想保留它们作为最后的手段。

谢谢!

【问题讨论】:

  • 这个soundLine 有没有播放任何东西?

标签: java audio-streaming


【解决方案1】:

将您的字节放入 ByteArrayInputStream 并将其输入到流播放器中... ByteArrayInputStream audioStream = new ByteArrayInputStream(buffer);

【讨论】:

  • 嗯,谢谢,但是将 ByteArrayInputStream 提供给 StreamPlayer 是什么意思? SourceDataLine.write() 函数只有一个接受 byte[] 数组的版本。你有其他功能吗?
  • 你的权利......当我想到 javastreamingaudio 类有问题时,我只看了你的问题的一半。现在才发现您使用的是完全不同的东西。对不起
  • 没问题,感谢您的帮助。
  • 你设置了调音台的音量吗?
  • 这里有人接缝设置音量播放声音stackoverflow.com/questions/4668863/…
猜你喜欢
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2016-04-28
  • 2012-01-07
  • 2020-01-14
  • 2011-07-06
  • 1970-01-01
相关资源
最近更新 更多