栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Record麦克风到字节阵列并播放声音

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Record麦克风到字节阵列并播放声音

编辑:这是与下面相同的想法的更好的版本,将在您录制时直接播放

AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);    TargetDataLine microphone;    SourceDataLine speakers;    try {        microphone = AudioSystem.getTargetDataLine(format);        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);        microphone = (TargetDataLine) AudioSystem.getLine(info);        microphone.open(format);        ByteArrayOutputStream out = new ByteArrayOutputStream();        int numBytesRead;        int CHUNK_SIZE = 1024;        byte[] data = new byte[microphone.getBufferSize() / 5];        microphone.start();        int bytesRead = 0;        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);        speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);        speakers.open(format);        speakers.start();        while (bytesRead < 100000) { numBytesRead = microphone.read(data, 0, CHUNK_SIZE); bytesRead += numBytesRead; // write the mic data to a stream for use later out.write(data, 0, numBytesRead);  // write mic data to stream for immediate playback speakers.write(data, 0, numBytesRead);        }        speakers.drain();        speakers.close();        microphone.close();    } catch (LineUnavailableException e) {        e.printStackTrace();    }

忍受我,因为这确实很粗糙,但是它可以通过扬声器播放录制的音频。

为了使其听起来更好,您将需要添加线程并优化输入/输出流。

http://www.developer.com/java/other/article.php/1579071/Java-Sound-Getting-
Started-Part-2-Capture-Using-Specified-
Mixer.htm

package audio;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.TargetDataLine;public class AudioTest {    public static void main(String[] args) {        AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);        TargetDataLine microphone;        AudioInputStream audioInputStream;        SourceDataLine sourceDataLine;        try { microphone = AudioSystem.getTargetDataLine(format); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); microphone = (TargetDataLine) AudioSystem.getLine(info); microphone.open(format); ByteArrayOutputStream out = new ByteArrayOutputStream(); int numBytesRead; int CHUNK_SIZE = 1024; byte[] data = new byte[microphone.getBufferSize() / 5]; microphone.start(); int bytesRead = 0; try {     while (bytesRead < 100000) { // Just so I can test if recording    // my mic works...         numBytesRead = microphone.read(data, 0, CHUNK_SIZE);         bytesRead = bytesRead + numBytesRead;         System.out.println(bytesRead);         out.write(data, 0, numBytesRead);     } } catch (Exception e) {     e.printStackTrace(); } byte audioData[] = out.toByteArray(); // Get an input stream on the byte array // containing the data InputStream byteArrayInputStream = new ByteArrayInputStream(         audioData); audioInputStream = new AudioInputStream(byteArrayInputStream,format, audioData.length / format.getframeSize()); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(format); sourceDataLine.start(); int cnt = 0; byte tempBuffer[] = new byte[10000]; try {     while ((cnt = audioInputStream.read(tempBuffer, 0,tempBuffer.length)) != -1) {         if (cnt > 0) {  // Write data to the internal buffer of  // the data line where it will be  // delivered to the speaker.  sourceDataLine.write(tempBuffer, 0, cnt);         }// end if     } } catch (IOException e) {     e.printStackTrace(); } // Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); microphone.close();        } catch (LineUnavailableException e) { e.printStackTrace();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419364.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号