[Video] set audio category to ambient every time a new player is made (#4934)

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

* mute on foregrounding

* remember previous state

---------

Co-authored-by: Samuel Newman <10959775+mozzius@users.noreply.github.com>
Co-authored-by: Hailey <me@haileyok.com>
zio/stable
Samuel Newman 2024-08-14 20:21:14 +01:00 committed by GitHub
parent 26d3777ecc
commit 21e214c235
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 2 deletions

View File

@ -1,6 +1,9 @@
import ExpoModulesCore import ExpoModulesCore
public class ExpoPlatformInfoModule: Module { public class ExpoPlatformInfoModule: Module {
private var prevAudioActive: Bool?
private var prevAudioCategory: AVAudioSession.Category?
public func definition() -> ModuleDefinition { public func definition() -> ModuleDefinition {
Name("ExpoPlatformInfo") Name("ExpoPlatformInfo")
@ -10,13 +13,20 @@ public class ExpoPlatformInfoModule: Module {
Function("setAudioCategory") { (audioCategoryString: String) in Function("setAudioCategory") { (audioCategoryString: String) in
let audioCategory = AVAudioSession.Category(rawValue: audioCategoryString) let audioCategory = AVAudioSession.Category(rawValue: audioCategoryString)
if audioCategory == self.prevAudioCategory {
return
}
self.prevAudioCategory = audioCategory
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
try? AVAudioSession.sharedInstance().setCategory(audioCategory) try? AVAudioSession.sharedInstance().setCategory(audioCategory)
} }
} }
Function("setAudioActive") { (active: Bool) in Function("setAudioActive") { (active: Bool) in
if active == self.prevAudioActive {
return
}
self.prevAudioActive = active
if active { if active {
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
try? AVAudioSession.sharedInstance().setActive(true) try? AVAudioSession.sharedInstance().setActive(true)

View File

@ -16,8 +16,8 @@ export function VideoPreview({
}) { }) {
const player = useVideoPlayer(video.uri, player => { const player = useVideoPlayer(video.uri, player => {
player.loop = true player.loop = true
player.muted = true
player.play() player.play()
player.volume = 0
}) })
return ( return (

View File

@ -28,6 +28,9 @@ export function VideoEmbedInnerNative() {
useEffect(() => { useEffect(() => {
try { try {
if (isAppFocused === 'active' && isScreenFocused && !player.playing) { if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioActive(false)
player.muted = true
player.play() player.play()
} else if (player.playing) { } else if (player.playing) {
player.pause() player.pause()

View File

@ -3,6 +3,10 @@ import type {VideoPlayer} from 'expo-video'
import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video' import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video'
import {logger} from '#/logger' import {logger} from '#/logger'
import {
AudioCategory,
PlatformInfo,
} from '../../../../../modules/expo-bluesky-swiss-army'
const VideoPlayerContext = React.createContext<VideoPlayer | null>(null) const VideoPlayerContext = React.createContext<VideoPlayer | null>(null)
@ -16,6 +20,9 @@ export function VideoPlayerProvider({
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
const player = useExpoVideoPlayer(source, player => { const player = useExpoVideoPlayer(source, player => {
try { try {
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
PlatformInfo.setAudioActive(false)
player.loop = true player.loop = true
player.muted = true player.muted = true
player.play() player.play()