From 6a3d0ae296357e36ff06a5c0fda7db880fe84c07 Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Sat, 12 Oct 2024 15:53:25 +0200 Subject: [PATCH] fix: play states working --- app/(auth)/vlc-player.tsx | 12 ++-- modules/vlc-player/index.ts | 73 ++++++++++++++++------ modules/vlc-player/ios/VlcPlayerView.swift | 25 ++------ 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/app/(auth)/vlc-player.tsx b/app/(auth)/vlc-player.tsx index 22f03840..69fe83ec 100644 --- a/app/(auth)/vlc-player.tsx +++ b/app/(auth)/vlc-player.tsx @@ -155,12 +155,12 @@ export default function page() { const { currentTime, duration, isBuffering, isPlaying } = data.nativeEvent; - console.log("onProgress ~", { - currentTime, - duration, - isBuffering, - isPlaying, - }); + // console.log("onProgress ~", { + // currentTime, + // duration, + // isBuffering, + // isPlaying, + // }); progress.value = currentTime; diff --git a/modules/vlc-player/index.ts b/modules/vlc-player/index.ts index 30bf6931..6514a420 100644 --- a/modules/vlc-player/index.ts +++ b/modules/vlc-player/index.ts @@ -4,31 +4,68 @@ import { Subscription, } from "expo-modules-core"; -// Import the native module. On web, it will be resolved to VlcPlayer.web.ts -// and on native platforms to VlcPlayer.ts import VlcPlayerModule from "./src/VlcPlayerModule"; import VlcPlayerView from "./src/VlcPlayerView"; -import { ChangeEventPayload, VlcPlayerViewProps } from "./src/VlcPlayer.types"; - -// Get the native constant value. -export const PI = VlcPlayerModule.PI; - -export function hello(): string { - return VlcPlayerModule.hello(); -} - -export async function setValueAsync(value: string) { - return await VlcPlayerModule.setValueAsync(value); -} +import { + PlaybackStatePayload, + ProgressUpdatePayload, + VideoLoadStartPayload, + VideoStateChangePayload, + VideoProgressPayload, + VlcPlayerSource, + TrackInfo, + ChapterInfo, + VlcPlayerViewProps, + VlcPlayerViewRef, +} from "./src/VlcPlayer.types"; const emitter = new EventEmitter( VlcPlayerModule ?? NativeModulesProxy.VlcPlayer ); -export function addChangeListener( - listener: (event: ChangeEventPayload) => void +export function addPlaybackStateListener( + listener: (event: PlaybackStatePayload) => void ): Subscription { - return emitter.addListener("onChange", listener); + return emitter.addListener( + "onPlaybackStateChanged", + listener + ); } -export { VlcPlayerView, VlcPlayerViewProps, ChangeEventPayload }; +export function addVideoLoadStartListener( + listener: (event: VideoLoadStartPayload) => void +): Subscription { + return emitter.addListener( + "onVideoLoadStart", + listener + ); +} + +export function addVideoStateChangeListener( + listener: (event: VideoStateChangePayload) => void +): Subscription { + return emitter.addListener( + "onVideoStateChange", + listener + ); +} + +export function addVideoProgressListener( + listener: (event: VideoProgressPayload) => void +): Subscription { + return emitter.addListener("onVideoProgress", listener); +} + +export { + VlcPlayerView, + VlcPlayerViewProps, + VlcPlayerViewRef, + PlaybackStatePayload, + ProgressUpdatePayload, + VideoLoadStartPayload, + VideoStateChangePayload, + VideoProgressPayload, + VlcPlayerSource, + TrackInfo, + ChapterInfo, +}; diff --git a/modules/vlc-player/ios/VlcPlayerView.swift b/modules/vlc-player/ios/VlcPlayerView.swift index 5ff5f0a3..9d9bef4b 100644 --- a/modules/vlc-player/ios/VlcPlayerView.swift +++ b/modules/vlc-player/ios/VlcPlayerView.swift @@ -421,20 +421,20 @@ extension VlcPlayerView: VLCMediaPlayerDelegate { DispatchQueue.main.async { [weak self] in guard let self = self, let player = self.mediaPlayer else { return } let currentState = player.state - print("VLC Player State Changed: \(currentState.description)") var stateInfo: [String: Any] = [ "target": self.reactTag ?? NSNull(), "currentTime": player.time.intValue, "duration": player.media?.length.intValue ?? 0, - "isPlaying": currentState == .playing, - "isBuffering": currentState == .buffering, ] - if player.state == .playing { + if player.isPlaying { stateInfo["isPlaying"] = true stateInfo["isBuffering"] = false stateInfo["state"] = "Playing" + } else { + stateInfo["isPlaying"] = false + stateInfo["state"] = "Paused" } if player.state == .buffering { @@ -442,10 +442,8 @@ extension VlcPlayerView: VLCMediaPlayerDelegate { stateInfo["state"] = "Buffering" } - if player.state == .paused { - stateInfo["isPlaying"] = false - stateInfo["state"] = "Paused" - } + print("VLC Player State Changed: \(currentState.description)") + print("VLC Player State Changed: \(player.isPlaying)") // switch currentState { // case .opening: @@ -475,7 +473,6 @@ extension VlcPlayerView: VLCMediaPlayerDelegate { func mediaPlayerTimeChanged(_ aNotification: Notification) { DispatchQueue.main.async { [weak self] in self?.updateVideoProgress() - self?.updatePlayerState() } } @@ -487,20 +484,10 @@ extension VlcPlayerView: VLCMediaPlayerDelegate { let durationMs = player.media?.length.intValue ?? 0 if currentTimeMs >= 0 && currentTimeMs < durationMs { - let isPlaying = player.isPlaying - let isBuffering = player.state == .buffering - self.onVideoProgress?([ "currentTime": currentTimeMs, "duration": durationMs, - "isPlaying": isPlaying, - "isBuffering": isBuffering, ]) - - // Debug log - print( - "VLC Player State: \(player.state.description), isPlaying: \(isPlaying), isBuffering: \(isBuffering)" - ) } } }