fix: play states working

This commit is contained in:
Fredrik Burmester
2024-10-12 15:53:25 +02:00
parent ba6322bb1f
commit 6a3d0ae296
3 changed files with 67 additions and 43 deletions

View File

@@ -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<ChangeEventPayload>("onChange", listener);
return emitter.addListener<PlaybackStatePayload>(
"onPlaybackStateChanged",
listener
);
}
export { VlcPlayerView, VlcPlayerViewProps, ChangeEventPayload };
export function addVideoLoadStartListener(
listener: (event: VideoLoadStartPayload) => void
): Subscription {
return emitter.addListener<VideoLoadStartPayload>(
"onVideoLoadStart",
listener
);
}
export function addVideoStateChangeListener(
listener: (event: VideoStateChangePayload) => void
): Subscription {
return emitter.addListener<VideoStateChangePayload>(
"onVideoStateChange",
listener
);
}
export function addVideoProgressListener(
listener: (event: VideoProgressPayload) => void
): Subscription {
return emitter.addListener<VideoProgressPayload>("onVideoProgress", listener);
}
export {
VlcPlayerView,
VlcPlayerViewProps,
VlcPlayerViewRef,
PlaybackStatePayload,
ProgressUpdatePayload,
VideoLoadStartPayload,
VideoStateChangePayload,
VideoProgressPayload,
VlcPlayerSource,
TrackInfo,
ChapterInfo,
};

View File

@@ -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)"
)
}
}
}