用java做一个可视化小程序,可以录音并予以保存。

用java做一个可视化小程序,可以录音并予以保存。,第1张

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);

}

}

}

}

学生小程序朗读打卡录音显示麦克风已关闭,可能是因为以下原因:

1 未授权小程序使用麦克风权限,需要在手机设置中打开小程序的麦克风权限。

2 手机麦克风被其他应用占用,需要关闭其他应用程序,并重新打开小程序尝试录音。

3 手机麦克风损坏或故障,需要检查麦克风是否正常工作。

4 小程序本身存在问题导致无法正常录音,需要联系小程序开发者或寻求相关技术支持。

综上所述,建议您检查小程序的麦克风权限、关闭其他应用程序、检查麦克风正常工作或联系小程序开发者或相关技术支持来解决该问题。

pick一下最终效果,然后一步一步来。

先把界面效果做出来。

wxml

wxss

js赋值

这里我的录音按钮点击扩散效果用的是纯css实现,而上方progress是使用animation实现的。

1、首先在手机的录音机中找到需要微信分享的录音,然后点击右上角,点击分享此录音,接着选择添加到微信收藏。

2、其次打开微信客户端,点击右下角的我,点击收藏,然后点击录音文件。

3、最后可以在d出的界面中点击右上角的省略号选择好友并发送即可。

以上就是关于用java做一个可视化小程序,可以录音并予以保存。全部的内容,包括:用java做一个可视化小程序,可以录音并予以保存。、学生小程序朗读打卡为啥录音显示麦克风已关闭、微信小程序录音界面以及功能实现等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9811918.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存