index.uts 2.5 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9
import { CreateVideoContext, VideoContext, RequestFullScreenOptions, Danmu } from '../interface.uts';
import { UniVideoTimeUpdateEvent, UniVideoTimeUpdateEventDetail, UniVideoFullScreenChangeEvent, UniVideoFullScreenChangeEventDetail, UniVideoErrorEvent, VideoError, UniVideoProgressEvent, UniVideoProgressEventDetail, UniVideoFullScreenClickEvent, UniVideoFullScreenClickEventDetail, UniVideoControlsToggleEvent, UniVideoControlsToggleEventDetail } from '../interface.uts';
import { UniVideoElement } from './index.vue';

export const createVideoContext : CreateVideoContext = function (videoId : string.VideoIdString, component ?: ComponentPublicInstance | null) : VideoContext | null {
	let videoElement : UniElement | null = null;
	if (component == null) {
		const pages = getCurrentPages();
		if (pages.length > 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
10
			videoElement = pages[pages.length - 1].vm!.$el?.parentNode?.querySelector('#' + videoId);
DCloud-yyl's avatar
DCloud-yyl 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		}
	} else {
		videoElement = component.$el?.parentNode?.querySelector('#' + videoId);
	}
	if (videoElement == null) return null;
	return new VideoContextImpl(videoElement as UniVideoElement);
}

class VideoContextImpl implements VideoContext {

	private videoElement : UniVideoElement;

	constructor(videoElement : UniVideoElement) {
		this.videoElement = videoElement;
	}

	play() {
		this.videoElement.play();
	}

	pause() {
		this.videoElement.pause();
	}

	seek(position : number) {
		this.videoElement.seek(position);
	}

	stop() {
		this.videoElement.stop();
	}

	sendDanmu(danmu : Danmu) {
		this.videoElement.sendDanmu(danmu);
	}

	playbackRate(rate : number) {
		this.videoElement.playbackRate(rate);
	}

	requestFullScreen(direction ?: RequestFullScreenOptions | null) {
		this.videoElement.requestFullScreen(direction);
	}

	exitFullScreen() {
		this.videoElement.exitFullScreen();
	}
}

export type VideoTimeUpdateEvent = UniVideoTimeUpdateEvent
export type VideoTimeUpdateEventDetail = UniVideoTimeUpdateEventDetail
export type VideoFullScreenChangeEvent = UniVideoFullScreenChangeEvent
export type VideoFullScreenChangeEventDetail = UniVideoFullScreenChangeEventDetail
export type VideoErrorEvent = UniVideoErrorEvent
export type VideoProgressEvent = UniVideoProgressEvent
export type VideoProgressEventDetail = UniVideoProgressEventDetail
export type VideoFullScreenClickEvent = UniVideoFullScreenClickEvent
export type VideoFullScreenClickEventDetail = UniVideoFullScreenClickEventDetail
export type VideoControlsToggleEvent = UniVideoControlsToggleEvent
export type VideoControlsToggleEventDetail = UniVideoControlsToggleEventDetail