UTSAndroid is not defined
ReferenceError: UTSAndroid is not defined
at UTSRecorder.getAppContext (app-service.js:213:9)
at UTSRecorder.start (app-service.js:229:28)
at startRecord (app-service.js:620:23)
at handleTouchStart (app-service.js:651:9)
at Array.forEach (<anonymous>)
at Array.map (<anonymous>), at uni_modules/aquan-recorder/utssdk/app-android/index.uts:180 at uni_modules/aquan-recorder/utssdk/app-android/index.uts:180
14:04:55.650 获取应用上下文失败, at uni_modules/aquan-recorder/utssdk/app-android/index.uts:200 at uni_modules/aquan-recorder/utssdk/app-android/index.uts:200
如上是报错。
代码如下:
import Context from "android.content.Context";
import RecordSliceManager from "com.aquan.recorder.RecordSliceManager";
import RecordFrameCallback from "com.aquan.recorder.RecordFrameCallback";
import RecordSliceInfo from "com.aquan.recorder.RecordSliceInfo";
import List from "java.util.List";
import MutableList from "kotlin.collections.MutableList";
import Number from "java.lang.Number";
export interface RecordSliceInfoData {
sliceData: ArrayBuffer;
sliceIndex: number;
sliceDuration: number;
sliceSize: number;
mimeType: string;
}
// 开始录音的选项
export interface StartOptions {
frameSize?: number; // 目标帧大小(字节),默认 10240
}
// 回调函数类型定义
type OnStartCallback = () => void;
type OnPauseCallback = () => void;
type OnResumeCallback = () => void;
type OnStopCallback = (totalSliceCount: number, totalDuration: number, allSlices: RecordSliceInfoData[]) => void;
type OnFrameRecordedCallback = (sliceInfo: RecordSliceInfoData) => void;
type OnErrorCallback = (errorMsg: string) => void;
type OnInterruptionBeginCallback = () => void;
type OnInterruptionEndCallback = () => void;
type OnDecibelChangeCallback = (decibel: number) => void;
// 在UTS中实现RecordFrameCallback接口
class UTSRecordFrameCallback implements RecordFrameCallback {
private onStartCallback: OnStartCallback | null = null;
private onPauseCallback: OnPauseCallback | null = null;
private onResumeCallback: OnResumeCallback | null = null;
private onStopCallback: OnStopCallback | null = null;
private onFrameRecordedCallback: OnFrameRecordedCallback | null = null;
private onErrorCallback: OnErrorCallback | null = null;
private onInterruptionBeginCallback: OnInterruptionBeginCallback | null = null;
private onInterruptionEndCallback: OnInterruptionEndCallback | null = null;
private onDecibelChangeCallback: OnDecibelChangeCallback | null = null;
setOnStart(callback: OnStartCallback | null): void {
this.onStartCallback = callback;
}
setOnPause(callback: OnPauseCallback | null): void {
this.onPauseCallback = callback;
}
setOnResume(callback: OnResumeCallback | null): void {
this.onResumeCallback = callback;
}
setOnStop(callback: OnStopCallback | null): void {
this.onStopCallback = callback;
}
setOnFrameRecorded(callback: OnFrameRecordedCallback | null): void {
this.onFrameRecordedCallback = callback;
}
setOnError(callback: OnErrorCallback | null): void {
this.onErrorCallback = callback;
}
setOnInterruptionBegin(callback: OnInterruptionBeginCallback | null): void {
this.onInterruptionBeginCallback = callback;
}
setOnInterruptionEnd(callback: OnInterruptionEndCallback | null): void {
this.onInterruptionEndCallback = callback;
}
setOnDecibelChange(callback: OnDecibelChangeCallback | null): void {
this.onDecibelChangeCallback = callback;
}
// 实现RecordFrameCallback接口
override onStart(): void {
if (this.onStartCallback != null) {
this.onStartCallback();
}
}
override onRecordPaused(): void {
if (this.onPauseCallback != null) {
this.onPauseCallback();
}
}
override onRecordResumed(): void {
if (this.onResumeCallback != null) {
this.onResumeCallback();
}
}
override onRecordFinished(totalSliceCount: Number, totalDuration: Number, allSlices: MutableList<RecordSliceInfo>): void {
if (this.onStopCallback != null) {
// 转换MutableList<RecordSliceInfo>为RecordSliceInfoData[]
const slicesData: RecordSliceInfoData[] = [];
// MutableList.size 返回的是属性,不是方法
const size = allSlices.size;
for (let i: number = 0; i < size; i++) {
// 将 i 转换为 Int 类型
const index = i.toInt();
const slice = allSlices.get(index);
const sliceDataObj: any = {
sliceData: slice.getSliceData(),
sliceIndex: slice.getSliceIndex() as number,
sliceDuration: slice.getSliceDuration() as number,
sliceSize: slice.getSliceSize() as number,
mimeType: slice.getMimeType()
};
slicesData.push(sliceDataObj as RecordSliceInfoData);
}
// 使用toInt()和toLong()进行类型转换
this.onStopCallback(totalSliceCount.toInt() as number, totalDuration.toLong() as number, slicesData);
}
}
override onFrameRecorded(sliceInfo: RecordSliceInfo): void {
if (this.onFrameRecordedCallback != null) {
const sliceDataObj: any = {
sliceData: sliceInfo.getSliceData(),
sliceIndex: sliceInfo.getSliceIndex() as number,
sliceDuration: sliceInfo.getSliceDuration() as number,
sliceSize: sliceInfo.getSliceSize() as number,
mimeType: sliceInfo.getMimeType()
};
this.onFrameRecordedCallback(sliceDataObj as RecordSliceInfoData);
}
}
override onRecordError(errorMsg: string): void {
if (this.onErrorCallback != null) {
this.onErrorCallback(errorMsg);
}
}
override onInterruptionBegin(): void {
if (this.onInterruptionBeginCallback != null) {
this.onInterruptionBeginCallback();
}
}
override onInterruptionEnd(): void {
if (this.onInterruptionEndCallback != null) {
this.onInterruptionEndCallback();
}
}
override onDecibelChange(decibel: Number): void {
if (this.onDecibelChangeCallback != null) {
// 使用toDouble()进行类型转换
this.onDecibelChangeCallback(decibel.toDouble() as number);
}
}
}
export class UTSRecorder {
private recorder: RecordSliceManager | null = null;
private callback: UTSRecordFrameCallback | null = null;
private isInitialized: boolean = false;
constructor() {
// 创建回调实例
this.callback = new UTSRecordFrameCallback();
}
/**
-
获取应用上下文
*/
private getAppContext(): Context | null {
try {
return UTSAndroid.getAppContext();
} catch (e: Error) {
console.error("获取应用上下文失败:", e);
return null;
}
}/**
- 开始录音
-
@param options 录音选项
*/
start(options?: StartOptions): void {
// 如果已经初始化,先销毁
if (this.isInitialized && this.recorder != null) {
this.recorder.destroy();
this.recorder = null;
this.isInitialized = false;
}// 获取应用上下文
const context = this.getAppContext();
if (context == null) {
console.error("获取应用上下文失败");
return;
}// 创建 RecordSliceManager 实例
let recorder: RecordSliceManager | null = null;
try {
recorder = RecordSliceManager.create(context, this.callback!, 44100);
} catch (e: Error) {
console.error("创建 RecordSliceManager 失败:", e);
return;
}if (recorder == null) {
console.error("创建 RecordSliceManager 失败: 返回null");
return;
}// 设置目标帧大小(默认 10240 字节,即 10KB)
const frameSize = options?.frameSize ?? 10240;
// 使用toInt()转换为整数类型,确保是Int类型
const frameSizeNumber = frameSize as number;
const frameSizeInt = frameSizeNumber.toInt();
// 使用局部变量避免Smart cast问题
const localRecorder = recorder;
localRecorder.setTargetFrameSize(frameSizeInt);// 设置保存到文件
localRecorder.setSaveToFile(true);// 保存到实例变量
this.recorder = localRecorder;// 标记为已初始化
this.isInitialized = true;// 开始录音
localRecorder.start();
}
0 个回复