Skip to content

Commit

Permalink
[Video] set audio category to ambient every time a new player is made (
Browse files Browse the repository at this point in the history
…#4934)

* set auto category to ambient every time a new player is made

* mute on foregrounding

* remember previous state

---------

Co-authored-by: Samuel Newman <[email protected]>
Co-authored-by: Hailey <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent 26d3777 commit 21e214c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ExpoModulesCore

public class ExpoPlatformInfoModule: Module {
private var prevAudioActive: Bool?
private var prevAudioCategory: AVAudioSession.Category?

public func definition() -> ModuleDefinition {
Name("ExpoPlatformInfo")

Expand All @@ -10,13 +13,20 @@ public class ExpoPlatformInfoModule: Module {

Function("setAudioCategory") { (audioCategoryString: String) in
let audioCategory = AVAudioSession.Category(rawValue: audioCategoryString)

if audioCategory == self.prevAudioCategory {
return
}
self.prevAudioCategory = audioCategory
DispatchQueue.global(qos: .background).async {
try? AVAudioSession.sharedInstance().setCategory(audioCategory)
}
}

Function("setAudioActive") { (active: Bool) in
if active == self.prevAudioActive {
return
}
self.prevAudioActive = active
if active {
DispatchQueue.global(qos: .background).async {
try? AVAudioSession.sharedInstance().setActive(true)
Expand Down
2 changes: 1 addition & 1 deletion src/view/com/composer/videos/VideoPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function VideoPreview({
}) {
const player = useVideoPlayer(video.uri, player => {
player.loop = true
player.muted = true
player.play()
player.volume = 0
})

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export function VideoEmbedInnerNative() {
useEffect(() => {
try {
if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioActive(false)
player.muted = true
player.play()
} else if (player.playing) {
player.pause()
Expand Down
7 changes: 7 additions & 0 deletions src/view/com/util/post-embeds/VideoPlayerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type {VideoPlayer} from 'expo-video'
import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video'

import {logger} from '#/logger'
import {
AudioCategory,
PlatformInfo,
} from '../../../../../modules/expo-bluesky-swiss-army'

const VideoPlayerContext = React.createContext<VideoPlayer | null>(null)

Expand All @@ -16,6 +20,9 @@ export function VideoPlayerProvider({
// eslint-disable-next-line @typescript-eslint/no-shadow
const player = useExpoVideoPlayer(source, player => {
try {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioActive(false)

player.loop = true
player.muted = true
player.play()
Expand Down

0 comments on commit 21e214c

Please sign in to comment.