java java字节数组播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12589156/
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
java byte array play sound
提问by user1246950
I have a small program that reads from dataline mic to byte array I am not 100% sure it works but just by printing part of the array it seem to chang when am making a sound next to the mic:)
我有一个从数据线麦克风读取到字节数组的小程序我不是 100% 确定它可以工作,但只是通过打印数组的一部分,当我在麦克风旁边发出声音时,它似乎会改变:)
I like to play the sound back how do i play the data from the buffer(byte array to sound)??
我喜欢播放声音如何播放缓冲区中的数据(字节数组到声音)??
package mic;
import javax.sound.sampled.*;
public class Mic extends Thread{
boolean flag;
TargetDataLine mic;
byte[] buffer;
AudioFormat format;
public static void main(String[] args) {
Mic a=new Mic();
a.start();
}
@Override
public void run()
{
flag=true;
startMic();
while(flag)
{
send();
}
}
public void send()
{
try{
mic.read(buffer,0,512);
System.out.println("1. "+buffer[0]);
}catch(Exception ex)
{
}
}
public void startMic()
{
try{
format=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0F,16,2,4,8000.0F,true);
DataLine.Info info=new DataLine.Info(TargetDataLine.class,format);
mic=(TargetDataLine)AudioSystem.getLine(info);
mic.open();
mic.start();
buffer=new byte[512];
}catch(Exception exx)
{
System.out.println("exx");
}
}
}
EDIT:
what i wanted to do in the end is send the byte array to other app and play right away
like live stream of a radio
编辑:
我最后想要做的是将字节数组发送到其他应用程序并
像收音机的直播一样立即播放
回答by kirbyfan64sos
回答by Julien Vermillard
A easy way to test the captured data is to save them into a file and load then in audacity using the RAW file import. Just specify your raw file format as : Sampling rate 8KHz format PCM big endian stereo 16 bits
测试捕获的数据的一种简单方法是将它们保存到一个文件中,然后使用 RAW 文件导入大胆加载。只需将您的原始文件格式指定为:采样率 8KHz 格式 PCM big endian 立体声 16 位