mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-02-04 03:36:29 +08:00
- Add playback rate state and actions to player store - Create SpeedSelectionModal with 7 speed options (0.5x - 2x) - Add speed control button with Gauge icon to PlayerControls - Integrate rate prop with Expo AV Video component - Extend PlayerSettings storage to persist playback rate per video - Support speed control across TV, mobile, and tablet platforms
214 lines
6.6 KiB
TypeScript
214 lines
6.6 KiB
TypeScript
import React, { useEffect, useRef } from "react";
|
||
import { StyleSheet, TouchableOpacity, BackHandler, AppState, AppStateStatus, View } from "react-native";
|
||
import { useLocalSearchParams, useRouter } from "expo-router";
|
||
import { Video, ResizeMode } from "expo-av";
|
||
import { useKeepAwake } from "expo-keep-awake";
|
||
import { ThemedView } from "@/components/ThemedView";
|
||
import { PlayerControls } from "@/components/PlayerControls";
|
||
import { EpisodeSelectionModal } from "@/components/EpisodeSelectionModal";
|
||
import { SourceSelectionModal } from "@/components/SourceSelectionModal";
|
||
import { SpeedSelectionModal } from "@/components/SpeedSelectionModal";
|
||
import { SeekingBar } from "@/components/SeekingBar";
|
||
// import { NextEpisodeOverlay } from "@/components/NextEpisodeOverlay";
|
||
import VideoLoadingAnimation from "@/components/VideoLoadingAnimation";
|
||
import useDetailStore from "@/stores/detailStore";
|
||
import { useTVRemoteHandler } from "@/hooks/useTVRemoteHandler";
|
||
import Toast from "react-native-toast-message";
|
||
import usePlayerStore, { selectCurrentEpisode } from "@/stores/playerStore";
|
||
import { useResponsiveLayout } from "@/hooks/useResponsiveLayout";
|
||
|
||
export default function PlayScreen() {
|
||
const videoRef = useRef<Video>(null);
|
||
const router = useRouter();
|
||
useKeepAwake();
|
||
|
||
// 响应式布局配置
|
||
const { deviceType } = useResponsiveLayout();
|
||
|
||
const {
|
||
episodeIndex: episodeIndexStr,
|
||
position: positionStr,
|
||
source: sourceStr,
|
||
id: videoId,
|
||
title: videoTitle,
|
||
} = useLocalSearchParams<{
|
||
episodeIndex: string;
|
||
position?: string;
|
||
source?: string;
|
||
id?: string;
|
||
title?: string;
|
||
}>();
|
||
const episodeIndex = parseInt(episodeIndexStr || "0", 10);
|
||
const position = positionStr ? parseInt(positionStr, 10) : undefined;
|
||
|
||
const { detail } = useDetailStore();
|
||
const source = sourceStr || detail?.source;
|
||
const id = videoId || detail?.id.toString();
|
||
const title = videoTitle || detail?.title;
|
||
const {
|
||
isLoading,
|
||
showControls,
|
||
// showNextEpisodeOverlay,
|
||
initialPosition,
|
||
introEndTime,
|
||
playbackRate,
|
||
setVideoRef,
|
||
handlePlaybackStatusUpdate,
|
||
setShowControls,
|
||
// setShowNextEpisodeOverlay,
|
||
reset,
|
||
loadVideo,
|
||
} = usePlayerStore();
|
||
const currentEpisode = usePlayerStore(selectCurrentEpisode);
|
||
|
||
useEffect(() => {
|
||
setVideoRef(videoRef);
|
||
if (source && id && title) {
|
||
loadVideo({ source, id, episodeIndex, position, title });
|
||
}
|
||
|
||
return () => {
|
||
reset(); // Reset state when component unmounts
|
||
};
|
||
}, [episodeIndex, source, position, setVideoRef, reset, loadVideo, id, title]);
|
||
|
||
useEffect(() => {
|
||
const handleAppStateChange = (nextAppState: AppStateStatus) => {
|
||
if (nextAppState === "background" || nextAppState === "inactive") {
|
||
videoRef.current?.pauseAsync();
|
||
}
|
||
};
|
||
|
||
const subscription = AppState.addEventListener("change", handleAppStateChange);
|
||
|
||
return () => {
|
||
subscription.remove();
|
||
};
|
||
}, []);
|
||
|
||
// TV遥控器处理 - 总是调用hook,但根据设备类型决定是否使用结果
|
||
const tvRemoteHandler = useTVRemoteHandler();
|
||
|
||
// 根据设备类型使用不同的交互处理
|
||
const onScreenPress = deviceType === 'tv'
|
||
? tvRemoteHandler.onScreenPress
|
||
: () => setShowControls(!showControls);
|
||
|
||
useEffect(() => {
|
||
const backAction = () => {
|
||
if (showControls) {
|
||
setShowControls(false);
|
||
return true;
|
||
}
|
||
router.back();
|
||
return true;
|
||
};
|
||
|
||
const backHandler = BackHandler.addEventListener("hardwareBackPress", backAction);
|
||
|
||
return () => backHandler.remove();
|
||
}, [showControls, setShowControls, router]);
|
||
|
||
useEffect(() => {
|
||
let timeoutId: NodeJS.Timeout | null = null;
|
||
|
||
if (isLoading) {
|
||
timeoutId = setTimeout(() => {
|
||
if (usePlayerStore.getState().isLoading) {
|
||
usePlayerStore.setState({ isLoading: false });
|
||
Toast.show({ type: "error", text1: "播放超时,请重试" });
|
||
}
|
||
}, 60000); // 1 minute
|
||
}
|
||
|
||
return () => {
|
||
if (timeoutId) {
|
||
clearTimeout(timeoutId);
|
||
}
|
||
};
|
||
}, [isLoading]);
|
||
|
||
if (!detail) {
|
||
return <VideoLoadingAnimation showProgressBar />;
|
||
}
|
||
|
||
// 动态样式
|
||
const dynamicStyles = createResponsiveStyles(deviceType);
|
||
|
||
return (
|
||
<ThemedView focusable style={dynamicStyles.container}>
|
||
<TouchableOpacity
|
||
activeOpacity={1}
|
||
style={dynamicStyles.videoContainer}
|
||
onPress={onScreenPress}
|
||
disabled={deviceType !== 'tv' && showControls} // 移动端和平板端在显示控制条时禁用触摸
|
||
>
|
||
<Video
|
||
ref={videoRef}
|
||
style={dynamicStyles.videoPlayer}
|
||
source={{ uri: currentEpisode?.url || "" }}
|
||
posterSource={{ uri: detail?.poster ?? "" }}
|
||
resizeMode={ResizeMode.CONTAIN}
|
||
rate={playbackRate}
|
||
onPlaybackStatusUpdate={handlePlaybackStatusUpdate}
|
||
onLoad={() => {
|
||
const jumpPosition = initialPosition || introEndTime || 0;
|
||
if (jumpPosition > 0) {
|
||
videoRef.current?.setPositionAsync(jumpPosition);
|
||
}
|
||
usePlayerStore.setState({ isLoading: false });
|
||
}}
|
||
onLoadStart={() => usePlayerStore.setState({ isLoading: true })}
|
||
useNativeControls={deviceType !== 'tv'}
|
||
shouldPlay
|
||
/>
|
||
|
||
{showControls && deviceType === 'tv' && <PlayerControls showControls={showControls} setShowControls={setShowControls} />}
|
||
|
||
<SeekingBar />
|
||
|
||
{isLoading && (
|
||
<View style={dynamicStyles.loadingContainer}>
|
||
<VideoLoadingAnimation showProgressBar />
|
||
</View>
|
||
)}
|
||
|
||
{/* <NextEpisodeOverlay visible={showNextEpisodeOverlay} onCancel={() => setShowNextEpisodeOverlay(false)} /> */}
|
||
</TouchableOpacity>
|
||
|
||
<EpisodeSelectionModal />
|
||
<SourceSelectionModal />
|
||
<SpeedSelectionModal />
|
||
</ThemedView>
|
||
);
|
||
}
|
||
|
||
const createResponsiveStyles = (deviceType: string) => {
|
||
const isMobile = deviceType === 'mobile';
|
||
const isTablet = deviceType === 'tablet';
|
||
|
||
return StyleSheet.create({
|
||
container: {
|
||
flex: 1,
|
||
backgroundColor: "black",
|
||
// 移动端和平板端可能需要状态栏处理
|
||
...(isMobile || isTablet ? { paddingTop: 0 } : {}),
|
||
},
|
||
videoContainer: {
|
||
...StyleSheet.absoluteFillObject,
|
||
// 为触摸设备添加更多的交互区域
|
||
...(isMobile || isTablet ? { zIndex: 1 } : {}),
|
||
},
|
||
videoPlayer: {
|
||
...StyleSheet.absoluteFillObject,
|
||
},
|
||
loadingContainer: {
|
||
...StyleSheet.absoluteFillObject,
|
||
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
||
justifyContent: 'center',
|
||
alignItems: 'center',
|
||
zIndex: 10,
|
||
},
|
||
});
|
||
};
|