怎么捕獲音頻及輸出音頻。
發(fā)表時間:2023-08-19 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]這是我原來用過的兩段代碼,輸出音頻和捕獲音頻。 構(gòu)造器里的socket是用來接受來自網(wǎng)絡(luò)的音頻數(shù)據(jù)。不做網(wǎng)絡(luò)音頻可以去掉它。希望能與大家分享經(jīng)驗。8-)import java.io.*; impor...
這是我原來用過的兩段代碼,輸出音頻和捕獲音頻。
構(gòu)造器里的socket是用來接受來自網(wǎng)絡(luò)的音頻數(shù)據(jù)。不做網(wǎng)絡(luò)音頻可以去掉它。
希望能與大家分享經(jīng)驗。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){//構(gòu)造器 取得socket以獲得網(wǎng)絡(luò)輸入流
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));//封裝成音頻輸出流,如果網(wǎng)絡(luò)流是經(jīng)過壓縮的需在此加套解壓流
}
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];//此處數(shù)組的大小跟實時性關(guān)系不大,可根據(jù)情況進(jìn)行調(diào)整
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){//構(gòu)造器 取得socket以獲得網(wǎng)絡(luò)輸出流
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());//建立輸出流 此處可以加套壓縮流用來壓縮數(shù)據(jù)
}
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可以情況進(jìn)行調(diào)整,應(yīng)跟下面的1024應(yīng)保持一致
int numBytesRead=0;
line.start();
while (thread != null) {
numBytesRead = line.read(data, 0,1024);//取數(shù)據(jù)(1024)的大小直接關(guān)系到傳輸?shù)乃俣龋话阍叫≡娇欤?
try {
captrueOutputStream.write(data, 0, numBytesRead);//寫入網(wǎng)絡(luò)流
}
catch (Exception ex) {
break;
}
}
line.stop();
line.close();
line = null;
try {
captrueOutputStream.flush();
captrueOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}