Skip to content
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

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

Merged
merged 2 commits into from
Aug 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ public class ExpoPlatformInfoModule: Module {
try? AVAudioSession.sharedInstance().setCategory(audioCategory)
}

Function("setAudioMixWithOthers") { (mixWithOthers: Bool) in
var options: AVAudioSession.CategoryOptions
Function("setAudioActive") { (active: Bool) in
var categoryOptions: AVAudioSession.CategoryOptions
let currentCategory = AVAudioSession.sharedInstance().category
if mixWithOthers {
options = [.mixWithOthers]

if active {
categoryOptions = [.mixWithOthers]
try? AVAudioSession.sharedInstance().setActive(true)
} else {
options = [.duckOthers]
categoryOptions = [.duckOthers]
try? AVAudioSession
.sharedInstance()
.setActive(
false,
options: [.notifyOthersOnDeactivation]
)
}

try? AVAudioSession
.sharedInstance()
.setCategory(
currentCategory,
mode: .default,
options: options
options: categoryOptions
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export function getIsReducedMotionEnabled(): boolean {
return NativeModule.getIsReducedMotionEnabled()
}

export function setAudioMixWithOthers(mixWithOthers: boolean): void {
export function setAudioActive(active: boolean): void {
if (Platform.OS !== 'ios') return
NativeModule.setAudioMixWithOthers(mixWithOthers)
NativeModule.setAudioActive(active)
}

export function setAudioCategory(audioCategory: AudioCategory): void {
Expand Down
8 changes: 5 additions & 3 deletions modules/expo-bluesky-swiss-army/src/PlatformInfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @see https://developer.apple.com/documentation/avfaudio/avaudiosession/setactiveoptions/1616603-notifyothersondeactivation
*/
export function setAudioMixWithOthers(mixWithOthers: boolean): void {
throw new NotImplementedError({mixWithOthers})
export function setAudioActive(active: boolean): void {
throw new NotImplementedError({active})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/expo-bluesky-swiss-army/src/PlatformInfo/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function getIsReducedMotionEnabled(): boolean {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches
}

export function setAudioMixWithOthers(mixWithOthers: boolean): void {
throw new NotImplementedError({mixWithOthers})
export function setAudioActive(active: boolean): void {
throw new NotImplementedError({active})
}

export function setAudioCategory(audioCategory: AudioCategory): void {
Expand Down
2 changes: 1 addition & 1 deletion src/App.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function App() {

React.useEffect(() => {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioMixWithOthers(true)
PlatformInfo.setAudioActive(true)
initPersistedState().then(() => setReady(true))
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export function VideoEmbedInnerNative() {
nativeControls={true}
onEnterFullscreen={() => {
PlatformInfo.setAudioCategory(AudioCategory.Playback)
PlatformInfo.setAudioMixWithOthers(false)
PlatformInfo.setAudioActive(false)
player.muted = false
}}
onExitFullscreen={() => {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioMixWithOthers(true)
PlatformInfo.setAudioActive(true)
player.muted = true
if (!player.playing) player.play()
}}
Expand Down Expand Up @@ -139,7 +139,7 @@ function Controls({
const category = muted ? AudioCategory.Ambient : AudioCategory.Playback

PlatformInfo.setAudioCategory(category)
PlatformInfo.setAudioMixWithOthers(mix)
PlatformInfo.setAudioActive(mix)
player.muted = muted
}, [player])

Expand Down
Loading