国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

捕獲音頻及輸出音頻

2019-11-18 15:12:15
字體:
來源:轉載
供稿:網友

這是我原來用過的兩段代碼,輸出音頻和捕捉音頻。
構造器里的socket是用來接受來自網絡的音頻數據。不做網絡音頻可以去掉它。

希望能與大家分享經驗。8-)

import java.io.*;
import javax.sound.sampled.*;
import java.net.*;

/**
* Title: VoiceChat
* Description: 輸出音頻(放音程序)
* Copyright: Copyright (c) 2001
* Company:
* @author 你猜!
* @version 1.0
*/

class Playback implements Runnable {

final int bufSize = 16384;
SourceDataLine line;
Thread thread;
Socket s;

Playback(Socket s){//構造器 取得socket以獲得網絡輸入流
this.s=s;
}
public void start() {

thread = new Thread(this);
thread.setName("Playback");
thread.start();
}

public void stop() {
thread = null;
}

public void run() {

AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
BufferedInputStream playbackInputStream;

try {
playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));//封裝成音頻輸出流,假如網絡流是經過壓縮的需在此加套解壓流
}
catch (IOException ex) {
return;
}

DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);

try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);
} catch (LineUnavailableException ex) {
return;
}

byte[] data = new byte[1024];//此處數組的大小跟實時性關系不大,可根據情況進行調整
int numBytesRead = 0;
line.start();

while (thread != null) {
try{
numBytesRead = playbackInputStream.read(data);
line.write(data, 0,numBytesRead);
} catch (IOException e) {
break;
}
}

if (thread != null) {
line.drain();
}

line.stop();
line.close();
line = null;
}
}

import java.io.*;
import javax.sound.sampled.*;
import java.net.*;

/**
* Title: VoiceChat
* Description: 音頻捕捉(錄音程序)
* Copyright: Copyright (c) 2001
* Company:
* @author 你猜!
* @version 1.0
*/

class Capture implements Runnable {

TargetDataLine line;
Thread thread;
Socket s;
BufferedOutputStream captrueOutputStream;

Capture(Socket s){//構造器 取得socket以獲得網絡輸出流
this.s=s;
}

public void start() {

thread = new Thread(this);
thread.setName("Capture");
thread.start();
}

public void stop() {
thread = null;
}

public void run() {

try {
captrueOutputStream=new BufferedOutputStream(s.getOutputStream());//建立輸出流 此處可以加套壓縮流用來壓縮數據
}
catch (IOException ex) {
return;
}

AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);

try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (Exception ex) {
return;
}

byte[] data = new byte[1024];//此處的1024可以情況進行調整,應跟下面的1024應保持一致
int numBytesRead=0;
line.start();

while (thread != null) {
numBytesRead = line.read(data, 0,1024);//取數據(1024)的大小直接關系到傳輸的速度,一般越小越快,
try {
captrueOutputStream.write(data, 0, numBytesRead);//寫入網絡流
}
catch (Exception ex) {
break;
}
}

line.stop();
line.close();
line = null;

try {
captrueOutputStream.flush();
captrueOutputStream.close();
} catch (IOException ex) {
ex.PRintStackTrace();
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵定县| 鹰潭市| 枞阳县| 九龙县| 佛教| 合肥市| 离岛区| 安顺市| 城市| 峡江县| 福泉市| 佛山市| 错那县| 社会| 楚雄市| 逊克县| 兰考县| 偃师市| 阿拉善右旗| 嘉义县| 阜阳市| 抚远县| 浦县| 乐至县| 治县。| 大城县| 五莲县| 仁布县| 和田市| 宁武县| 延津县| 贵港市| 贵阳市| 白山市| 甘德县| 崇信县| 怀集县| 阜阳市| 兴宁市| 若尔盖县| 大庆市|