
import javaioByteArrayInputStream;
import javaioByteArrayOutputStream;
import javaioFile;
import javaioInputStream;
import orgeclipseswtSWT;
import orgeclipseswtwidgets;
import orgeclipseswtevents;
import javaxsoundsampledAudioFileFormat;
import javaxsoundsampledAudioFormat;
import javaxsoundsampledAudioInputStream;
import javaxsoundsampledAudioSystem;
import javaxsoundsampledDataLine;
import javaxsoundsampledSourceDataLine;
import javaxsoundsampledTargetDataLine;
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);
shellsetSize(350, 150);
shellsetText("录音机程序");
//
myLabel = new Label(shell, SWTNONE);
myLabelsetBounds(38, 21, 100, 18);
myLabelsetText("录音机");
// 创建按钮
captureBtn = new Button(shell, SWTNONE);
captureBtnsetBounds(30, 61, 60, 18);
captureBtnsetText("录音");
captureBtnsetEnabled(true);
stopBtn = new Button(shell, SWTNONE);
stopBtnsetBounds(100, 61, 60, 18);
stopBtnsetText("停止");
stopBtnsetEnabled(false);
playBtn = new Button(shell, SWTNONE);
playBtnsetBounds(170, 61, 60, 18);
playBtnsetText("播放");
playBtnsetEnabled(false);
saveBtn = new Button(shell, SWTNONE);
saveBtnsetBounds(240, 61, 60, 18);
saveBtnsetText("保存");
saveBtnsetEnabled(false);
// 注册录音事件
captureBtnaddSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtnsetEnabled(false);
stopBtnsetEnabled(true);
playBtnsetEnabled(false);
saveBtnsetEnabled(false);
// 开始录音
capture();
}
public void widgetDefaultSelected(SelectionEvent event) {
// textsetText("No worries!");
}
});
// 注册停止事件
stopBtnaddSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtnsetEnabled(true);
stopBtnsetEnabled(false);
playBtnsetEnabled(true);
saveBtnsetEnabled(true);
// 停止录音
stop();
}
public void widgetDefaultSelected(SelectionEvent event) {
// textsetText("No worries!");
}
});
// 注册播放事件
playBtnaddSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 播放录音
play();
}
public void widgetDefaultSelected(SelectionEvent event) {
// textsetText("No worries!");
}
});
// 注册保存事件
saveBtnaddSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 保存录音
save();
}
public void widgetDefaultSelected(SelectionEvent event) {
// textsetText("No worries!");
}
});
}
public void start() {
shellopen();
while (!shellisDisposed()) {
if (!displayreadAndDispatch()) {
displaysleep();
}
}
}
public static void main(String[] args) {
RecordPlay label = new RecordPlay();
labelstart();
}
// (1)录音事件,保存到ByteArrayOutputStream中
private void capture() {
try {
// 打开录音
audioFormat = getAudioFormat();
DataLineInfo dataLineInfo = new DataLineInfo(
TargetDataLineclass, audioFormat);
targetDataLine = (TargetDataLine) AudioSystemgetLine(dataLineInfo);
targetDataLineopen(audioFormat);
targetDataLinestart();
// 创建独立线程进行录音
Thread captureThread = new Thread(new CaptureThread());
captureThreadstart();
} catch (Exception e) {
eprintStackTrace();
Systemexit(0);
}
}
// (2)播放ByteArrayOutputStream中的数据
private void play() {
try {
// 取得录音数据
byte audioData[] = byteArrayOutputStreamtoByteArray();
// 转换成输入流
InputStream byteArrayInputStream = new ByteArrayInputStream(
audioData);
AudioFormat audioFormat = getAudioFormat();
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioDatalength / audioFormatgetFrameSize());
DataLineInfo dataLineInfo = new DataLineInfo(
SourceDataLineclass, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystemgetLine(dataLineInfo);
sourceDataLineopen(audioFormat);
sourceDataLinestart();
// 创建独立线程进行播放
Thread playThread = new Thread(new PlayThread());
playThreadstart();
} catch (Exception e) {
eprintStackTrace();
Systemexit(0);
}
}
// (3)停止录音
public void stop() {
stopCapture = true;
}
// (4)保存文件
public void save() {
// 取得录音输入流
AudioFormat audioFormat = getAudioFormat();
byte audioData[] = byteArrayOutputStreamtoByteArray();
InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioDatalength / audioFormatgetFrameSize());
// 写入文件
try {
File file = new File("d:/myjava/testwav");
AudioSystem
write(audioInputStream, AudioFileFormatTypeWAVE, file);
} catch (Exception e) {
eprintStackTrace();
}
}
// 取得AudioFormat
private AudioFormat getAudioFormat() {
float sampleRate = 160000F;
// 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 = audioInputStreamread(tempBuffer, 0,
tempBufferlength)) != -1) {
if (cnt > 0) {
// 写入缓存数据
sourceDataLinewrite(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLinedrain();
sourceDataLineclose();
} catch (Exception e) {
eprintStackTrace();
Systemexit(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 = targetDataLineread(tempBuffer, 0,
tempBufferlength);
if (cnt > 0) {
// 保存该数据
byteArrayOutputStreamwrite(tempBuffer, 0, cnt);
totaldatasize += cnt;
}
}
byteArrayOutputStreamclose();
} catch (Exception e) {
eprintStackTrace();
Systemexit(0);
}
}
}
}
苹果手机通话录音方法如下:
*** 作环境:iphone13手机,ios1561系统,微信8029版本等。
1、在微信中搜索“通话录音”,选择其中一个点击进入小程序界面。
2、在小程序的界面中,开始输入号码,接着点击拨号,等待生成录音号。
3、通话挂断后,点击下方的录音记录,再点击刚才的电话号码。
4、跳转到录音详情的界面后,找到复制下载地址,点击它。
5、复制成功后,打开至Safari浏览器,选择粘贴并前往,再点击界面中的下载。
6、最后在Safari浏览器的下载列表中找到刚才的录音文件,就可以播放通话录音了。
小程序设置虚拟号录音功能可以通过集成第三方语音API实现,用户可以在通话过程中开启录音存储通话内容。在实现过程中需要考虑用户隐私保护,应提供录音提示功能以增强用户知情权和控制权;同时,对于录音内容的存储应遵守法律法规,并制定明确的个人信息保护政策。另外,为了提升用户体验,还应该考虑录音文件保存路径、录音格式支持、录音时长限制等因素。最后,需要定期进行维护,确保录音功能的稳定和安全。
以上就是关于用java做一个可视化小程序,可以录音并予以保存。全部的内容,包括:用java做一个可视化小程序,可以录音并予以保存。、iphone电话录音功能怎么打开、小程序设置虚拟号录音功能等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)