java 如何在java中播放pcm原始数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:25:02  来源:igfitidea点击:

how to play pcm raw data in java

javaaudiopcm

提问by Geos

I have PCM samples in a short array. What is the best way to play this out? The format is 8000Hz, Mono, 16 bit, big endian. (The PCM samples are generated in the code and not read through some file)

我有一个简短的 PCM 样本。玩这个的最好方法是什么?格式为 8000Hz,单声道,16 位,大端。(PCM 样本是在代码中生成的,而不是通过某些文件读取的)

Thanks

谢谢

采纳答案by Daniel Rikowski

With the javax.sound.sampledpackage it's pretty much straightforward, but you have to use some boilerplate.

使用javax.sound.sampled包非常简单,但您必须使用一些样板文件。

Here's a good tutorial on that: www.wikijava.org/wiki/Play_a_wave_sound_in_Java

这是一个很好的教程:www.wikijava.org/wiki/Play_a_wave_sound_in_Java

Basically you have to create an InputStreamfrom your array and use that to create an AudioInputStream. There you have to specify the format of your audio data.

基本上你必须InputStream从你的数组中创建一个并使用它来创建一个AudioInputStream. 您必须在那里指定音频数据的格式。

Then you open an output stream (SourceDataLine) and copy the bytes from the audio stream into that stream.

然后打开一个输出流 ( SourceDataLine) 并将音频流中的字节复制到该流中。

回答by Zorayr

Check out this article - http://download.oracle.com/javase/tutorial/sound/playing.html.

查看这篇文章 - http://download.oracle.com/javase/tutorial/sound/playing.html

More specifically, read about SourceDataLine and how to set up AudioFormat.

更具体地说,请阅读 SourceDataLine 以及如何设置 AudioFormat。