当前位置: 技术问答>java相关
java 如何录音?
来源: 互联网 发布时间:2015-02-08
本文导语: 目的,实时的语音传输,可能吗? 谢谢。 | 这是我实现的基于JMF的RTP实时语音采集和传输程序: /** * 采集语音和发送RTP流的JavaBean * @author:sharetop * @version:1.0.0 * * date:2001-6-26 */ import javax.med...
目的,实时的语音传输,可能吗?
谢谢。
谢谢。
|
这是我实现的基于JMF的RTP实时语音采集和传输程序:
/**
* 采集语音和发送RTP流的JavaBean
* @author:sharetop
* @version:1.0.0
*
* date:2001-6-26
*/
import javax.media.*;
import javax.media.format.*;
import javax.media.rtp.*;
import javax.media.rtp.rtcp.*;
import javax.media.protocol.*;
import javax.media.rtp.event.*;
import javax.media.control.*;
import java.util.*;
import java.io.*;
import java.net.*;
import com.sitechasia.debug.Code;
public class RTPVoiceSender
{
private Processor mProcessor = null;
private RTPManager mRTPManager = null;
private StateHelper mStateHelper = null;
private DataSource mDataSource = null;
private SendStream mSendStream = null;
private Vector mIPAddress = null;
// boolean haveMulticastAddress = false;
private MonitorWindow monitorWindow=null;
public RTPVoiceSender() throws VoiceException
{
mIPAddress = new Vector();
init();
}
public void addTargets(Vector userList) throws VoiceException
{
mIPAddress = userList;
Code.debug("--start add targets....");
try{
for (Enumeration e = mIPAddress.elements() ; e.hasMoreElements() ;) {
SessionAddress destAddress = new SessionAddress(InetAddress.getByName((String)e.nextElement()),Port.AudioReceivePort,Port.TTL);
mRTPManager.addTarget(destAddress);
System.out.println(destAddress.toString());
Code.debug(" - add one target");
}
mSendStream = mRTPManager.createSendStream(mDataSource,0);
} catch (Exception e){
throw new VoiceException("Cannot add targets: " + e.getMessage());
}
Code.debug(" - Create Transmiter ok ...");
}
public void addTarget(String ip) throws VoiceException
{
try{
InetAddress ipAddr = InetAddress.getByName(ip);
SessionAddress sa = new SessionAddress(ipAddr,Port.AudioReceivePort,Port.TTL);
mRTPManager.addTarget(sa);
mIPAddress.add(ip);
}catch(Exception e){
throw new VoiceException("addTarget exception"+e.getMessage());
}
}
public void removeTarget(String ip) throws VoiceException
{
try{
InetAddress ipAddr = InetAddress.getByName(ip);
SessionAddress sa = new SessionAddress(ipAddr,Port.AudioReceivePort,Port.TTL);
mRTPManager.removeTarget(sa,"remove one target");
mIPAddress.remove(ip);
}catch(Exception e){
throw new VoiceException("del user exception :"+e.getMessage());
}
}
private void init() throws VoiceException
{
//第一步:配置采集设备
/*****在win平台下直接用DirectSoundCapture采集
AudioFormat format = new AudioFormat(AudioFormat.ULAW,8000,8,1);
Vector devices= CaptureDeviceManager.getDeviceList( format);
CaptureDeviceInfo di= null;
if (devices.size() > 0) {
di = (CaptureDeviceInfo) devices.elementAt(0);
}
else {
// 如果没有合适的采集设备
throw new VoiceException("Can't detect CaptureDevice");
}
*****/
CaptureDeviceInfo di = CaptureDeviceManager.getDevice("DirectSoundCapture");
Code.debug(" - Open capture device ok ");
//第二步:构建Processor和StateHelper
try{
mProcessor = Manager.createProcessor(di.getLocator());
mStateHelper = new StateHelper(mProcessor);
} catch (IOException e) {
throw new VoiceException("IO Exception : "+e.getMessage());
} catch (NoProcessorException e) {
throw new VoiceException("No Processor Exception: "+e.getMessage());
}
Code.debug(" - Create Processor ok ");
//第三步:configure,设置内容类型,realize
if (!mStateHelper.configure(10000)) {
throw new VoiceException("Error configuring processor");
}
mProcessor.setContentDescriptor(new ContentDescriptor( ContentDescriptor.RAW_RTP));
TrackControl track[] = mProcessor.getTrackControls();
boolean encodingOk = false;
for (int i = 0; i