persist mute between videos & unmute for autoplay disabled
This commit is contained in:
@@ -2,12 +2,19 @@ import React from 'react'
|
|||||||
import {useVideoPlayer, VideoPlayer} from 'expo-video'
|
import {useVideoPlayer, VideoPlayer} from 'expo-video'
|
||||||
|
|
||||||
import {isAndroid, isNative} from '#/platform/detection'
|
import {isAndroid, isNative} from '#/platform/detection'
|
||||||
|
import {useAutoplayDisabled} from '#/state/preferences'
|
||||||
|
import {
|
||||||
|
AudioCategory,
|
||||||
|
PlatformInfo,
|
||||||
|
} from '../../../../../modules/expo-bluesky-swiss-army'
|
||||||
|
|
||||||
const Context = React.createContext<{
|
const Context = React.createContext<{
|
||||||
activeSource: string
|
activeSource: string
|
||||||
activeViewId: string | undefined
|
activeViewId: string | undefined
|
||||||
setActiveSource: (src: string | null, viewId: string | null) => void
|
setActiveSource: (src: string | null, viewId: string | null) => void
|
||||||
player: VideoPlayer
|
player: VideoPlayer
|
||||||
|
mutedInFeed: boolean
|
||||||
|
setMutedInFeed: (muted: boolean) => void
|
||||||
} | null>(null)
|
} | null>(null)
|
||||||
|
|
||||||
export function Provider({children}: {children: React.ReactNode}) {
|
export function Provider({children}: {children: React.ReactNode}) {
|
||||||
@@ -17,9 +24,17 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
|
|
||||||
const [activeSource, setActiveSource] = React.useState('')
|
const [activeSource, setActiveSource] = React.useState('')
|
||||||
const [activeViewId, setActiveViewId] = React.useState<string>()
|
const [activeViewId, setActiveViewId] = React.useState<string>()
|
||||||
|
const autoplayDisabled = useAutoplayDisabled()
|
||||||
|
const [mutedInFeed, setMutedInFeed] = React.useState(!autoplayDisabled)
|
||||||
|
|
||||||
const player = useVideoPlayer(activeSource, p => {
|
const player = useVideoPlayer(activeSource, p => {
|
||||||
p.muted = true
|
const mix = !mutedInFeed
|
||||||
|
const category = mutedInFeed
|
||||||
|
? AudioCategory.Ambient
|
||||||
|
: AudioCategory.Playback
|
||||||
|
PlatformInfo.setAudioCategory(category)
|
||||||
|
PlatformInfo.setAudioActive(mix)
|
||||||
|
p.muted = mutedInFeed
|
||||||
p.loop = true
|
p.loop = true
|
||||||
// We want to immediately call `play` so we get the loading state
|
// We want to immediately call `play` so we get the loading state
|
||||||
p.play()
|
p.play()
|
||||||
@@ -48,6 +63,8 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
setActiveSource: setActiveSourceOuter,
|
setActiveSource: setActiveSourceOuter,
|
||||||
activeViewId,
|
activeViewId,
|
||||||
player,
|
player,
|
||||||
|
mutedInFeed,
|
||||||
|
setMutedInFeed,
|
||||||
}}>
|
}}>
|
||||||
{children}
|
{children}
|
||||||
</Context.Provider>
|
</Context.Provider>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, {useCallback, useRef} from 'react'
|
import React, {useCallback, useRef} from 'react'
|
||||||
import {Pressable, View} from 'react-native'
|
import {Pressable, View} from 'react-native'
|
||||||
import Animated, {FadeInDown} from 'react-native-reanimated'
|
import Animated, {FadeInDown} from 'react-native-reanimated'
|
||||||
import {VideoPlayer, VideoView} from 'expo-video'
|
import {VideoView} from 'expo-video'
|
||||||
import {AppBskyEmbedVideo} from '@atproto/api'
|
import {AppBskyEmbedVideo} from '@atproto/api'
|
||||||
import {msg} from '@lingui/macro'
|
import {msg} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
@@ -33,7 +33,7 @@ export function VideoEmbedInnerNative({
|
|||||||
isMuted: boolean
|
isMuted: boolean
|
||||||
}) {
|
}) {
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {player} = useActiveVideoNative()
|
const {player, mutedInFeed} = useActiveVideoNative()
|
||||||
const ref = useRef<VideoView>(null)
|
const ref = useRef<VideoView>(null)
|
||||||
|
|
||||||
const enterFullscreen = useCallback(() => {
|
const enterFullscreen = useCallback(() => {
|
||||||
@@ -69,7 +69,7 @@ export function VideoEmbedInnerNative({
|
|||||||
onFullscreenExit={() => {
|
onFullscreenExit={() => {
|
||||||
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
|
PlatformInfo.setAudioCategory(AudioCategory.Ambient)
|
||||||
PlatformInfo.setAudioActive(false)
|
PlatformInfo.setAudioActive(false)
|
||||||
player.muted = true
|
player.muted = mutedInFeed
|
||||||
player.playbackRate = 1
|
player.playbackRate = 1
|
||||||
setIsFullscreen(false)
|
setIsFullscreen(false)
|
||||||
}}
|
}}
|
||||||
@@ -79,7 +79,6 @@ export function VideoEmbedInnerNative({
|
|||||||
accessibilityHint=""
|
accessibilityHint=""
|
||||||
/>
|
/>
|
||||||
<VideoControls
|
<VideoControls
|
||||||
player={player}
|
|
||||||
enterFullscreen={enterFullscreen}
|
enterFullscreen={enterFullscreen}
|
||||||
isMuted={isMuted}
|
isMuted={isMuted}
|
||||||
timeRemaining={timeRemaining}
|
timeRemaining={timeRemaining}
|
||||||
@@ -89,18 +88,17 @@ export function VideoEmbedInnerNative({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function VideoControls({
|
function VideoControls({
|
||||||
player,
|
|
||||||
enterFullscreen,
|
enterFullscreen,
|
||||||
timeRemaining,
|
timeRemaining,
|
||||||
isMuted,
|
isMuted,
|
||||||
}: {
|
}: {
|
||||||
player: VideoPlayer
|
|
||||||
enterFullscreen: () => void
|
enterFullscreen: () => void
|
||||||
timeRemaining: number
|
timeRemaining: number
|
||||||
isMuted: boolean
|
isMuted: boolean
|
||||||
}) {
|
}) {
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
|
const {player, setMutedInFeed} = useActiveVideoNative()
|
||||||
|
|
||||||
const onPressFullscreen = useCallback(() => {
|
const onPressFullscreen = useCallback(() => {
|
||||||
switch (player.status) {
|
switch (player.status) {
|
||||||
@@ -128,7 +126,8 @@ function VideoControls({
|
|||||||
PlatformInfo.setAudioCategory(category)
|
PlatformInfo.setAudioCategory(category)
|
||||||
PlatformInfo.setAudioActive(mix)
|
PlatformInfo.setAudioActive(mix)
|
||||||
player.muted = muted
|
player.muted = muted
|
||||||
}, [player])
|
setMutedInFeed(muted)
|
||||||
|
}, [player, setMutedInFeed])
|
||||||
|
|
||||||
// show countdown when:
|
// show countdown when:
|
||||||
// 1. timeRemaining is a number - was seeing NaNs
|
// 1. timeRemaining is a number - was seeing NaNs
|
||||||
|
|||||||
Reference in New Issue
Block a user