Skip to content

fix:修复音频播放、录制相关状态问题 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions harmony/audio_toolkit/src/main/ets/RNCAudioPlayerTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface PlayConfig {
looping?: boolean;
speed?: number;
autoDestroy?: boolean;
continueToPlayInBackground?: boolean;
continuesToPlayInBackground?: boolean;
}

interface PlayInfo {
Expand All @@ -45,7 +45,7 @@ interface PlayInfo {
}

interface Error {
err: string;
err?: string;
message?: string;
}

Expand Down Expand Up @@ -143,10 +143,13 @@ export class RCTAudioPlayerTurboModule extends TurboModule {

pauseOnBackground(): void {
this.playConfigMap.forEach((config, playerId) => {
if (!config.continueToPlayInBackground) {
if (!config.continuesToPlayInBackground) {
const player = this.playerMap.get(playerId);
if (player) {
player.pause();
this.toEmit(playerId, 'pause', {
message: 'Playback paused due toBackground',
});
}
}
});
Expand All @@ -163,7 +166,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
});
}

setAVPlayerCallback(avPlayer: media.AVPlayer, playerId: number): void {
setAVPlayerCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
// seek操作结果回调函数
avPlayer.on('seekDone', (seekDoneTime: number) => {
logger.debug(`AVPlayer seek succeeded, seek time is ${seekDoneTime}`);
Expand All @@ -178,7 +181,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
avPlayer.reset(); // 调用reset重置资源,触发idle状态
});
// 状态机变化回调函数
this.avPlayerStateChangeCallback(avPlayer, playerId);
this.avPlayerStateChangeCallback(avPlayer, playerId, next);
avPlayer.on('seekDone', () => {
const call = this.playSeekCallbacks.get(playerId);
if (call) {
Expand Down Expand Up @@ -229,7 +232,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
});
}

avPlayerStateChangeCallback(avPlayer: media.AVPlayer, playerId: number): void {
avPlayerStateChangeCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
logger.debug(`stateChange:${state}`);
switch (state) {
Expand All @@ -242,6 +245,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
case StateChange.PREPARED: // prepare调用成功后上报该状态机
logger.debug(`prepared called.to and apply config and play`);
this.applyConfig(playerId);
next(null);
break;
case StateChange.PLAYING: // play成功调用后触发该状态机上报
break;
Expand Down Expand Up @@ -272,11 +276,11 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
});
}

async createPlayer(pathStr: string, playerId: number): Promise<void> {
async createPlayer(pathStr: string, playerId: number, next: (object?) => void): Promise<void> {
logger.debug(`createPlayer path:${pathStr}`);
try {
const avPlayer: media.AVPlayer = await media.createAVPlayer();
this.setAVPlayerCallback(avPlayer, playerId);
this.setAVPlayerCallback(avPlayer, playerId, next);
if (pathStr.startsWith(this.HTTP_START)) {
avPlayer.url = pathStr;
} else {
Expand Down Expand Up @@ -312,39 +316,47 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
}
}

prepare(playerId: number, path: string, option: PlayConfig, next: () => void): void {
prepare(playerId: number, path: string, option: PlayConfig, next: (object?) => void): void {
logger.debug(`prepare start`);
this.setConfig(playerId, option);
this.createPlayer(path, playerId).then(() => {
next();
this.createPlayer(path, playerId, next).then(() => {
});
}

checkPlayer(playerId: number, next: (err?: string) => void): boolean {
checkPlayer(playerId: number, next: (object?) => void): boolean {
const hasPlayer = this.playerMap.has(playerId);
if (hasPlayer) {
return true;
} else {
next?.('not found player');
logger.debug('not found player')
let err: Error = {
err: 'not found player',
message: 'not found media player',
};
next?.(err);
return false;
}
}

async play(playerId: number, callback: (err: string, result?: PlayInfo) => void): Promise<void> {
async play(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
try {
logger.debug(`play start`);
if (!this.checkPlayer(playerId, callback)) {
return;
}
const player = this.getPlayer(playerId);
await player.play();
callback('', this.getInfo(playerId));
callback(null, this.getInfo(playerId));
} catch (e) {
callback?.(`player call play function err:${JSON.stringify(e)}`);
let err: Error = {
err: 'player function err',
message: `player call play function err:${JSON.stringify(e)}`,
};
callback?.(err, this.getInfo(playerId));
}
}

async pause(playerId: number, callback: (err: string, result?: PlayInfo) => void): Promise<void> {
async pause(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
logger.debug(`pause start`);
if (!this.checkPlayer(playerId, callback)) {
return;
Expand All @@ -354,7 +366,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
this.toEmit(playerId, 'pause', {
message: 'player paused',
});
callback('', this.getInfo(playerId));
callback(null, this.getInfo(playerId));
}

async stop(playerId: number, callback: () => void): Promise<void> {
Expand Down
17 changes: 12 additions & 5 deletions harmony/audio_toolkit/src/main/ets/RNCAudioRecorderTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ const PERMISSIONS: Array<Permissions> = [
'ohos.permission.MICROPHONE',
];

interface Error {
err?: string;
message?: string;
}

export class RCTAudioRecorderTurboModule extends TurboModule {
private _file: fs.File;
// 音频参数
private avRecorder: media.AVRecorder | undefined = undefined;
private avProfile: media.AVRecorderProfile;
private avConfig: media.AVRecorderConfig;
private readonly AUDIOBITRATE_DEFAULT = 100000;
private readonly AUDIOBITRATE_DEFAULT = 48000;
private readonly AUDIOCHANNELS_DEFAULT = 2;
private readonly AUDIOSAMPLERATE = 48000;
private readonly FD_PATH = 'fd://';
Expand Down Expand Up @@ -107,11 +112,13 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
await this.avRecorder.prepare(this.avConfig);
await this.avRecorder.start();
next(null, audioSaveResult[0]);
} else {
next(null);
}
});
});
} catch (error) {
next(error);
next(null);
}
}

Expand All @@ -134,10 +141,10 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
this.avRecorder = await media.createAVRecorder();
this.setAudioRecorderCallback(recorderId);
this.avProfile = {
audioBitrate: option.bitRate || this.AUDIOBITRATE_DEFAULT, // 音频比特率
audioChannels: option.channels || this.AUDIOCHANNELS_DEFAULT, // 音频声道数
audioBitrate: option.bitRate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
audioChannels: option.channels ?? this.AUDIOCHANNELS_DEFAULT, // 音频声道数
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
audioSampleRate: this.AUDIOSAMPLERATE, // 音频采样率
audioSampleRate: option.sampleRate ?? this.AUDIOSAMPLERATE, // 音频采样率
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
};
this.avConfig = {
Expand Down