From 5c0d0c01393a31c8460be8f5b69785653963042e Mon Sep 17 00:00:00 2001 From: Jhen Date: Sun, 1 Oct 2023 11:12:35 +0800 Subject: [PATCH] feat(example): add updateAudioSession --- example/src/App.tsx | 26 +++++++++++++++++++++++++- ios/RNWhisperAudioSessionUtils.m | 2 +- src/AudioSessionIos.ts | 12 ++++++++---- src/NativeRNWhisper.ts | 2 +- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 4541c16..e6b5148 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -12,7 +12,7 @@ import { import RNFS from 'react-native-fs' import { unzip } from 'react-native-zip-archive' import Sound from 'react-native-sound' -import { initWhisper, libVersion } from '../../src' // whisper.rn +import { initWhisper, libVersion, AudioSessionIos } from '../../src' // whisper.rn import type { WhisperContext } from '../../src' import contextOpts from './context-opts' @@ -93,6 +93,28 @@ const createDir = async (log: any) => { const filterPath = (path: string) => path.replace(RNFS.DocumentDirectoryPath, '') +const updateAudioSession = async (log: any) => { + if (Platform.OS !== 'ios') return + + // Log current audio session + // log('Category & Options:', JSON.stringify(await AudioSessionIos.getCurrentCategory())) + // log('Mode:', await AudioSessionIos.getCurrentMode()) + + await AudioSessionIos.setCategory( + AudioSessionIos.Category.PlayAndRecord, [ + AudioSessionIos.CategoryOptions.MixWithOthers, + AudioSessionIos.CategoryOptions.AllowBluetooth, + ], + ) + await AudioSessionIos.setMode(AudioSessionIos.Mode.SpokenAudio) + await AudioSessionIos.setActive(true) + + const categoryResult = await AudioSessionIos.getCurrentCategory() + log('Category:', categoryResult.category) + log('Category Options:', categoryResult.options.join(', ')) + log('Mode:', await AudioSessionIos.getCurrentMode()) +} + export default function App() { const [whisperContext, setWhisperContext] = useState(null) const [logs, setLogs] = useState([`whisper.cpp version: ${libVersion}`]) @@ -263,6 +285,7 @@ export default function App() { log('Start realtime transcribing...') try { await createDir(log) + await updateAudioSession(log) const { stop, subscribe } = await whisperContext.transcribeRealtime({ language: 'en', @@ -358,6 +381,7 @@ export default function App() { log('Recorded file does not exist') return } + await updateAudioSession(log) const player = new Sound(recordFile, '', (e) => { if (e) { log('error', e) diff --git a/ios/RNWhisperAudioSessionUtils.m b/ios/RNWhisperAudioSessionUtils.m index c0578ad..5eaf5a5 100644 --- a/ios/RNWhisperAudioSessionUtils.m +++ b/ios/RNWhisperAudioSessionUtils.m @@ -81,4 +81,4 @@ +(AVAudioSessionCategoryOptions)getOptions:(NSArray *)options { return result; } -@end \ No newline at end of file +@end diff --git a/src/AudioSessionIos.ts b/src/AudioSessionIos.ts index ea825f9..d1d0dc3 100644 --- a/src/AudioSessionIos.ts +++ b/src/AudioSessionIos.ts @@ -36,22 +36,26 @@ const checkPlatform = () => { } export default { + Category: AudioSessionCategory, + CategoryOptions: AudioSessionCategoryOptions, + Mode: AudioSessionMode, + getCurrentCategory: async (): Promise<{ category: AudioSessionCategory, options: AudioSessionCategoryOptions[], }> => { checkPlatform() - const { category, options } = await RNWhisper.getAudioSessionCurrentCategory() + const result = await RNWhisper.getAudioSessionCurrentCategory() return { - category: category as AudioSessionCategory, - options: options as AudioSessionCategoryOptions[], + category: (result.category.replace('AVAudioSessionCategory', '') as AudioSessionCategory), + options: result.options?.map((option: string) => (option.replace('AVAudioSessionCategoryOptions', '') as AudioSessionCategoryOptions)), } }, getCurrentMode: async (): Promise => { checkPlatform() const mode = await RNWhisper.getAudioSessionCurrentMode() - return mode as AudioSessionMode + return (mode.replace('AVAudioSessionMode', '') as AudioSessionMode) }, setCategory: async ( diff --git a/src/NativeRNWhisper.ts b/src/NativeRNWhisper.ts index 4ac9941..9aa725a 100644 --- a/src/NativeRNWhisper.ts +++ b/src/NativeRNWhisper.ts @@ -80,7 +80,7 @@ export interface Spec extends TurboModule { // iOS specific getAudioSessionCurrentCategory: () => Promise<{ category: string, - options: {}, + options: Array, }>; getAudioSessionCurrentMode: () => Promise; setAudioSessionCategory: (category: string, options: Array) => Promise;