Skip to content

Commit

Permalink
[Video] Only allow one VideoView to be active at a time, regardless…
Browse files Browse the repository at this point in the history
… of source (#5131)
  • Loading branch information
haileyok authored Sep 4, 2024
1 parent 21e48bb commit dee28f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/view/com/util/post-embeds/ActiveVideoNativeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {useVideoPlayer, VideoPlayer} from 'expo-video'
import {isNative} from '#/platform/detection'

const Context = React.createContext<{
activeSource: string | null
setActiveSource: (src: string) => void
activeSource: string
activeViewId: string | undefined
setActiveSource: (src: string, viewId: string) => void
player: VideoPlayer
} | null>(null)

Expand All @@ -15,15 +16,27 @@ export function Provider({children}: {children: React.ReactNode}) {
}

const [activeSource, setActiveSource] = React.useState('')
const [activeViewId, setActiveViewId] = React.useState<string>()

const player = useVideoPlayer(activeSource, p => {
p.muted = true
p.loop = true
p.play()
})

const setActiveSourceOuter = (src: string, viewId: string) => {
setActiveSource(src)
setActiveViewId(viewId)
}

return (
<Context.Provider value={{activeSource, setActiveSource, player}}>
<Context.Provider
value={{
activeSource,
setActiveSource: setActiveSourceOuter,
activeViewId,
player,
}}>
{children}
</Context.Provider>
)
Expand Down
15 changes: 9 additions & 6 deletions src/view/com/util/post-embeds/VideoEmbed.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useState} from 'react'
import React, {useCallback, useId, useState} from 'react'
import {View} from 'react-native'
import {Image} from 'expo-image'
import {AppBskyEmbedVideo} from '@atproto/api'
Expand All @@ -17,11 +17,14 @@ import {useActiveVideoNative} from './ActiveVideoNativeContext'
import * as VideoFallback from './VideoEmbedInner/VideoFallback'

export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) {
const {_} = useLingui()
const t = useTheme()
const {activeSource, setActiveSource, player} = useActiveVideoNative()
const {activeSource, activeViewId, setActiveSource, player} =
useActiveVideoNative()
const viewId = useId()

const [isFullscreen, setIsFullscreen] = React.useState(false)
const isActive = embed.playlist === activeSource
const {_} = useLingui()
const isActive = embed.playlist === activeSource && activeViewId === viewId

const [key, setKey] = useState(0)
const renderError = useCallback(
Expand All @@ -34,7 +37,7 @@ export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) {

const onChangeStatus = (isVisible: boolean) => {
if (isVisible) {
setActiveSource(embed.playlist)
setActiveSource(embed.playlist, viewId)
if (!player.playing) {
player.play()
}
Expand Down Expand Up @@ -88,7 +91,7 @@ export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) {
<Button
style={[a.absolute, a.inset_0]}
onPress={() => {
setActiveSource(embed.playlist)
setActiveSource(embed.playlist, viewId)
}}
label={_(msg`Play video`)}
color="secondary">
Expand Down

0 comments on commit dee28f3

Please sign in to comment.