「听java录音」java音频
今天给各位分享听java录音的知识,其中也会对java音频进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用java做一个可视化小程序,可以录音并予以保存。
- 2、java如何播放声音
- 3、javacv怎么调用音频录制
- 4、大家好,我现在想用java在页面上实现一下录音和播放功能,想问下各位高手有啥好方法没?
- 5、java通过JACOB实现TTS.如何实现录音
- 6、怎么通过C#或JAVA得到从麦克风得到声音或它的声频文件?
用java做一个可视化小程序,可以录音并予以保存。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class RecordPlay {
boolean stopCapture = false; // 控制录音标志
AudioFormat audioFormat; // 录音格式
// 读取数据:从TargetDataLine写入ByteArrayOutputStream录音
ByteArrayOutputStream byteArrayOutputStream;
int totaldatasize = 0;
TargetDataLine targetDataLine;
// 播放数据:从AudioInputStream写入SourceDataLine播放
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
private Button captureBtn;
private Button stopBtn;
private Button playBtn;
private Button saveBtn;
private Label myLabel;
private Shell shell;
private Display display;
public RecordPlay() {
super();
display = new Display();
shell = new Shell(display);
shell.setSize(350, 150);
shell.setText("录音机程序");
//
myLabel = new Label(shell, SWT.NONE);
myLabel.setBounds(38, 21, 100, 18);
myLabel.setText("录音机");
// 创建按钮
captureBtn = new Button(shell, SWT.NONE);
captureBtn.setBounds(30, 61, 60, 18);
captureBtn.setText("录音");
captureBtn.setEnabled(true);
stopBtn = new Button(shell, SWT.NONE);
stopBtn.setBounds(100, 61, 60, 18);
stopBtn.setText("停止");
stopBtn.setEnabled(false);
playBtn = new Button(shell, SWT.NONE);
playBtn.setBounds(170, 61, 60, 18);
playBtn.setText("播放");
playBtn.setEnabled(false);
saveBtn = new Button(shell, SWT.NONE);
saveBtn.setBounds(240, 61, 60, 18);
saveBtn.setText("保存");
saveBtn.setEnabled(false);
// 注册录音事件
captureBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
// 开始录音
capture();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册停止事件
stopBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
// 停止录音
stop();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册播放事件
playBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 播放录音
play();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册保存事件
saveBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 保存录音
save();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
}
public void start() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void main(String[] args) {
RecordPlay label = new RecordPlay();
label.start();
}
// (1)录音事件,保存到ByteArrayOutputStream中
private void capture() {
try {
// 打开录音
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
// 创建独立线程进行录音
Thread captureThread = new Thread(new CaptureThread());
captureThread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// (2)播放ByteArrayOutputStream中的数据
private void play() {
try {
// 取得录音数据
byte audioData[] = byteArrayOutputStream.toByteArray();
// 转换成输入流
InputStream byteArrayInputStream = new ByteArrayInputStream(
audioData);
AudioFormat audioFormat = getAudioFormat();
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioData.length / audioFormat.getFrameSize());
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 创建独立线程进行播放
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// (3)停止录音
public void stop() {
stopCapture = true;
}
// (4)保存文件
public void save() {
// 取得录音输入流
AudioFormat audioFormat = getAudioFormat();
byte audioData[] = byteArrayOutputStream.toByteArray();
InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioData.length / audioFormat.getFrameSize());
// 写入文件
try {
File file = new File("d:/myjava/test.wav");
AudioSystem
.write(audioInputStream, AudioFileFormat.Type.WAVE, file);
} catch (Exception e) {
e.printStackTrace();
}
}
// 取得AudioFormat
private AudioFormat getAudioFormat() {
float sampleRate = 16000.0F;
// 8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
// 8,16
int channels = 1;
// 1,2
boolean signed = true;
// true,false
boolean bigEndian = false;
// true,false
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
int cnt;
// 读取数据到缓存数据
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (cnt 0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
class CaptureThread extends Thread {
// 临时数组
byte tempBuffer[] = new byte[10000];
public void run() {
byteArrayOutputStream = new ByteArrayOutputStream();
totaldatasize = 0;
stopCapture = false;
try {// 循环执行,直到按下停止录音按钮
while (!stopCapture) {
// 读取10000个数据
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
if (cnt 0) {
// 保存该数据
byteArrayOutputStream.write(tempBuffer, 0, cnt);
totaldatasize += cnt;
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
java如何播放声音
对声音媒体的直接支持可以说是Java的一大特色,尤其是在动画中配上声音效果,就可以使人在视觉上和听觉上均得到美的享受,那才叫过瘾。Java中播放声音文件与显示图像文件一样方便,同样只需要先将声音文件装载进来,然后播放就行了。
Java目前支持的声音文件只有一种格式,那就是SUN公司的AU格式(.AU文件),也称为u-law格式。由于AU格式的声音仅有8KHz的采样频率且不支持立体声效果,所以音质不算太好。唯一的好处就是AU声音文件的尺寸比其它格式小,有利于网上传输。一般,我们较熟悉的大都是WAV格式的声音文件,因此必须先将它们转换为AU格式(可以选用Goldwave软件来进行这种格式转换)。
声音文件准备好以后,就可以考虑将它装载进来并播放。在Applet类中提供的play( )方法可以将声音文件的装载与播放一并完成,其调用格式如下:
void play(URL url)
void play(URL url, String name)
可见,play( )方法的调用格式与getImage( )方法是完全一样的,也采用URL来定位声音文件。例如,某声音文件audio.au与applet文件存放在同一目录下,可以这样写:
play(getCodeBase( ),"audio.au");
一旦play( )方法装载了该声音文件,就立即播放。如果找不到指定URL下的声音文件,play( )方法不返回出错信息,只是听不到想听的声音而已。
由于play( )方法只能将声音播放一遍,若想循环播放某声音作为背景音乐,就需要用到功能更强大的AudioClip类,它能更有效地管理声音的播放操作。因为它被定义在java.applet程序包中,所以使用该类的话,不要忘了在程序头部加上:
import java.applet.AudioClip;
为了得到AudioClip对象,我们可以调用Applet类中的getAudioClip( )方法。它能装载指定URL的声音文件,并返回一个AudioClip对象,其调用格式如下:
AudioClip getAudioClip(URL url)
AudioClip getAudioClip(URL url, String name)
得到AudioClip对象以后,就可以调用AudioClip类中所提供的各种方法来操作其中的声音数据,这些方法如表4-4所示。
如果getAudioClip( )方法没有找到所指定的声音文件,就会返回null值。所以,在调用表4-4中所列的方法前,应该先检查一下得到的AudioClip对象不是null,因为在null对象上调用上述方法将导致出错。
如果需要的话,我们还可以在applet中同时装载几个声音文件来一起播放,到时候,这些声音将混合在一起,就象二重奏一样。另外还有一点要说明的是,如果我们使用AudioClip对象的loop( )方法来重复播放背景音乐时,千万不要忘记在适当的时候调用AudioClip对象的stop( )方法来结束放音,否则的话,即使用户离开这一Web页面,该声音也不会停止,这无疑将会惹恼用户。因此,一般我们都在applet的stop( )方法中添上停止播放的代码。
例如,下面这段程序将播放两段声音,一段是连续播放的背景音乐,另一段是讲话录音。
import java.applet.AudioClip;
public class Audios extends java.applet.Applet{
AudioClip bgmusic,speak;
public void init(){
bgmusic=getAudioClip(getDocumentBase(),"space.au");
speak=getAudioClip(getDocumentBase(),"intro.au");
}
public void start(){
if(bgmusic!=null)
bgmusic.loop();
if(speak!=null)
speak.play();
}
public void stop(){
if(bgmusic!=null)
bgmusic.stop(); //关闭背景音乐,切记。
}
}
package com.hongyuan.test;
import java.io.File;
import java.io.IOException;
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.UnsupportedAudioFileException;
public class MusicTest {
public static final String MUSIC_FILE = "相逢一笑.wav";
public static void main(String[] args) throws
LineUnavailableException,
UnsupportedAudioFileException, IOException {
// 获取音频输入流
AudioInputStream audioInputStream =
AudioSystem
.getAudioInputStream(new File(MUSIC_FILE));
//
获取音频编码对象
AudioFormat audioFormat = audioInputStream.getFormat();
// 设置数据输入
DataLine.Info dataLineInfo = new
DataLine.Info(SourceDataLine.class,
audioFormat,
AudioSystem.NOT_SPECIFIED);
SourceDataLine sourceDataLine =
(SourceDataLine)
AudioSystem
.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
/*
* 从输入流中读取数据发送到混音器
*/
int count;
byte tempBuffer[]
= new byte[1024];
while ((count = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (count 0)
{
sourceDataLine.write(tempBuffer, 0, count);
}
}
//
清空数据缓冲,并关闭输入
sourceDataLine.drain();
sourceDataLine.close();
}
}
javacv怎么调用音频录制
Adobe Audition3.0(简称AA),先下载伴奏,打开AA,,点文件,新建,确定。点视图,选多轨,在第一音轨点右键插入音频,找到你的伴奏插入,点第二音轨的R键开启录音状态,点左下的红色圆圈录制就行了,录制完成后双击音轨2的波形,进入编辑视图,这里可以进行人声效果的处理,可以到百度搜索一下相关教程,很多的,如降噪、混响、均衡等都在这里处理,处理完成后返回多轨视图,听一下,如果声音大小与伴奏不协调可以在编辑视图的效果》压限》标准化进程里调节音量。都做好后在多轨视图里点右键混缩到文件里的第一项,就把伴奏和人声混合起来了,点文件》另存,保存成WAV或MP3随便,完成了。
大家好,我现在想用java在页面上实现一下录音和播放功能,想问下各位高手有啥好方法没?
页面上录音,那你的就是web程序,除非是applet程序,但是这样就有局限性,客户端必须安装jdk.
flex+java好像可以, 你可以研究下flex.
java通过JACOB实现TTS.如何实现录音
第一就是运用JMF。录制系统的发音。但是这样的开发太过于牵强,万一系统还在放歌,或者其他声音,那岂不是一起录制了。
第二,其实问题没有那么复杂,用IO流来实现。不过不知道能否实现。
怎么通过C#或JAVA得到从麦克风得到声音或它的声频文件?
JAVA声音
API进一步描述三种设备子界面:
InputDevice
InputDevice界面提供一个方法getInputChannel来获得一个InputChannel对象,从中捕获可读的音频数据。
OutputDevice
OutputDevice界面提供一个方法getOutputChannel来获得一个OutputChannel对象,音频数据可以写入该输出通道,并予播放。
Mixer
Mixer支持多个InputChannel和/或Clip。
另外,它提供了查询方法,从中可得到它所支持的通道数量,它也提供了支持同步暂停和唤醒多个通道播放的方法。
控件
通道和音频端口
(比如扬声器和麦克风)一般能支持一组控件比如增益和定位。通过将它的类作为参数传给getControl()方法,JAVA声音
API的通道对象和端口对象可以获得一个特别的控件。
编码器
Codecs可以对音频数据编码和解码,允许在不同格式和编码之间转换。JAVA声音
API通过AudioSystem类中的方法为这些转换提供了高级接口。如果给了一个特殊的
音频流
,应用程序会查询音频系统来找到相应的转换,从而得到指定格式的音频流。
文件和流
音频流是与音频数据格式和数据长度相关的
输入流
,
文件流
是与文件类型和数据长度相关的输入流。JAVA声音
API在AudioSystem类中为音频文件和音频流之间的转换提供了接口。
查询和访问安装组件
AudioSystem类充当到采样音频系统资源入口的角色。该类允许程序员查询和访问输入设备、
输出设备
以及安装好的混音设备。另外,AudioSystem包含许多在不同音频数据格式间转换的方法。它也提供一些方法,使得在不需要对设备直接操作的情况下,直接获得输入通道或输出通道。
你可以试试做
听java录音的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java音频、听java录音的信息别忘了在本站进行查找喔。