[Video] Resume background audio whenever muting video audio (#4915)

zio/stable
Hailey 2024-08-09 16:52:23 -07:00 committed by GitHub
parent c2131bb039
commit 65d6e561d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 17 deletions

View File

@ -13,20 +13,29 @@ public class ExpoPlatformInfoModule: Module {
try? AVAudioSession.sharedInstance().setCategory(audioCategory) try? AVAudioSession.sharedInstance().setCategory(audioCategory)
} }
Function("setAudioMixWithOthers") { (mixWithOthers: Bool) in Function("setAudioActive") { (active: Bool) in
var options: AVAudioSession.CategoryOptions var categoryOptions: AVAudioSession.CategoryOptions
let currentCategory = AVAudioSession.sharedInstance().category let currentCategory = AVAudioSession.sharedInstance().category
if mixWithOthers {
options = [.mixWithOthers] if active {
categoryOptions = [.mixWithOthers]
try? AVAudioSession.sharedInstance().setActive(true)
} else { } else {
options = [.duckOthers] categoryOptions = [.duckOthers]
try? AVAudioSession
.sharedInstance()
.setActive(
false,
options: [.notifyOthersOnDeactivation]
)
} }
try? AVAudioSession try? AVAudioSession
.sharedInstance() .sharedInstance()
.setCategory( .setCategory(
currentCategory, currentCategory,
mode: .default, mode: .default,
options: options options: categoryOptions
) )
} }
} }

View File

@ -9,9 +9,9 @@ export function getIsReducedMotionEnabled(): boolean {
return NativeModule.getIsReducedMotionEnabled() return NativeModule.getIsReducedMotionEnabled()
} }
export function setAudioMixWithOthers(mixWithOthers: boolean): void { export function setAudioActive(active: boolean): void {
if (Platform.OS !== 'ios') return if (Platform.OS !== 'ios') return
NativeModule.setAudioMixWithOthers(mixWithOthers) NativeModule.setAudioActive(active)
} }
export function setAudioCategory(audioCategory: AudioCategory): void { export function setAudioCategory(audioCategory: AudioCategory): void {

View File

@ -6,11 +6,13 @@ export function getIsReducedMotionEnabled(): boolean {
} }
/** /**
* Set whether the app's audio should mix with other apps' audio. * Set whether the app's audio should mix with other apps' audio. Will also resume background music playback when `false`
* if it was previously playing.
* @param mixWithOthers * @param mixWithOthers
* @see https://developer.apple.com/documentation/avfaudio/avaudiosession/setactiveoptions/1616603-notifyothersondeactivation
*/ */
export function setAudioMixWithOthers(mixWithOthers: boolean): void { export function setAudioActive(active: boolean): void {
throw new NotImplementedError({mixWithOthers}) throw new NotImplementedError({active})
} }
/** /**

View File

@ -8,8 +8,8 @@ export function getIsReducedMotionEnabled(): boolean {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches return window.matchMedia('(prefers-reduced-motion: reduce)').matches
} }
export function setAudioMixWithOthers(mixWithOthers: boolean): void { export function setAudioActive(active: boolean): void {
throw new NotImplementedError({mixWithOthers}) throw new NotImplementedError({active})
} }
export function setAudioCategory(audioCategory: AudioCategory): void { export function setAudioCategory(audioCategory: AudioCategory): void {

View File

@ -159,7 +159,7 @@ function App() {
React.useEffect(() => { React.useEffect(() => {
PlatformInfo.setAudioCategory(AudioCategory.Ambient) PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioMixWithOthers(true) PlatformInfo.setAudioActive(true)
initPersistedState().then(() => setReady(true)) initPersistedState().then(() => setReady(true))
}, []) }, [])

View File

@ -60,12 +60,12 @@ export function VideoEmbedInnerNative() {
nativeControls={true} nativeControls={true}
onEnterFullscreen={() => { onEnterFullscreen={() => {
PlatformInfo.setAudioCategory(AudioCategory.Playback) PlatformInfo.setAudioCategory(AudioCategory.Playback)
PlatformInfo.setAudioMixWithOthers(false) PlatformInfo.setAudioActive(false)
player.muted = false player.muted = false
}} }}
onExitFullscreen={() => { onExitFullscreen={() => {
PlatformInfo.setAudioCategory(AudioCategory.Ambient) PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioMixWithOthers(true) PlatformInfo.setAudioActive(true)
player.muted = true player.muted = true
if (!player.playing) player.play() if (!player.playing) player.play()
}} }}
@ -139,7 +139,7 @@ function Controls({
const category = muted ? AudioCategory.Ambient : AudioCategory.Playback const category = muted ? AudioCategory.Ambient : AudioCategory.Playback
PlatformInfo.setAudioCategory(category) PlatformInfo.setAudioCategory(category)
PlatformInfo.setAudioMixWithOthers(mix) PlatformInfo.setAudioActive(mix)
player.muted = muted player.muted = muted
}, [player]) }, [player])