java 如何录音?

blre 2001-09-24 06:40:00
目的,实时的语音传输,可能吗?
谢谢。
...全文
423 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
blre 2001-10-06
  • 打赏
  • 举报
回复
谢谢各位,给分了。
lner 2001-09-30
  • 打赏
  • 举报
回复
在 www.javasoft.com 上你可以找到所有需要的东西。
sharetop 2001-09-29
  • 打赏
  • 举报
回复

这是我实现的基于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 < track.length; i++) {
if (!encodingOk && track[i] instanceof FormatControl) {
if (((FormatControl)track[i]).setFormat( new AudioFormat(AudioFormat.GSM_RTP,8000,8,1)) == null) {
track[i].setEnabled(false);
}
else {
encodingOk = true;
}
}
else {
track[i].setEnabled(false);
}
}

if(!encodingOk){
throw new VoiceException("Error on track.setFormat ");
}
if (!mStateHelper.realize(10000)) {
throw new VoiceException("Error realizing processor");
}

Code.debug(" - Realize Processor ok ");

//第四步:取出DataSource
try {
mDataSource = mProcessor.getDataOutput();
} catch (NotRealizedError e){
throw new VoiceException("Not Realize Error:"+e.getMessage());
}
Code.debug(" - Get DataSource ok ");

//第五步:配置传输器
try{
mRTPManager = RTPManager.newInstance();

Code.debug("- start init RTPManager");

// 新的初始化
//构造一个SouceDescription数组

SourceDescription[] userdesclist= new SourceDescription[] {
new SourceDescription(SourceDescription.SOURCE_DESC_CNAME,
"sharetop",1,false),
new SourceDescription(SourceDescription.SOURCE_DESC_TOOL,
"JMF v2.1.1",1,false)
};
InetAddress[] localHosts = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
SessionAddress[] localHostArray = new SessionAddress[localHosts.length];
for(int j=0;j<localHosts.length;j++){
localHostArray[j] = new SessionAddress(localHosts[j],Port.AudioTransmitPort,Port.TTL);
System.out.println(localHostArray[j].toString());
}
mRTPManager.initialize(localHostArray ,userdesclist,0.95,0.05,null);
} catch (Exception e){
throw new VoiceException("Cannot init RTPManager: " + e.getMessage());
}

monitorWindow = new MonitorWindow(mProcessor);
monitorWindow.initialize();
Code.debug(" - ok init manager");
}

/**
* 开始处理
*/
public void start() throws VoiceException
{
monitorWindow.setVisible(true);

try {
mProcessor.start();
Code.debug("- Start capture processor");
mSendStream.start();
Code.debug(" - Start send stream");
} catch(Exception e) {
throw new VoiceException("Error start DataSink"+e.getMessage());
}
}

/**
* 停止
*/
public void stop() throws VoiceException
{
try {
mProcessor.stop();
mSendStream.stop();

// mIPAddress.clear();
monitorWindow.setVisible(false);

mRTPManager.removeTargets("close all targets....");

} catch(Exception e) {
throw new VoiceException("Error stop DataSink"+e.getMessage());
}
}

/**
* 关闭
*/
public void close() throws VoiceException
{
try {
// mSendStream.close(); //如果与视频发送器一起,也会影响到视频,所以也不能要!!!!!!
monitorWindow.close();
mRTPManager.dispose();
mRTPManager=null;
} catch(Exception e) {
throw new VoiceException("Error close DataSink"+e.getMessage());
}
}

} //end class
sharetop 2001-09-25
  • 打赏
  • 举报
回复

如果只是录音,java.sound可以实现。

如果要实时语音传输,可以使用JMF,你可以在java.sun.com找到相应资料。

我前一阵子刚完成一个基于JMF的音视频教学系统(不过还有问题正在修改中)。

有兴趣一起研究它吧。
hailong326 2001-09-25
  • 打赏
  • 举报
回复
gz
hexiaofeng 2001-09-25
  • 打赏
  • 举报
回复
gz
n6002 2001-09-24
  • 打赏
  • 举报
回复
我只知道可能

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧