UNPKG

@bezlepkin/nativescript-ar

Version:

NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.

91 lines (90 loc) 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARVideo = void 0; const core_1 = require("@nativescript/core"); const arcommon_1 = require("./arcommon"); class ARVideo extends arcommon_1.ARCommonNode { isPlaying() { return this.videoPlayer && this.videoPlayer.timeControlStatus === 2; } play() { if (this.videoPlayer) { this.videoPlayer.play(); } } pause() { if (this.videoPlayer) { this.videoPlayer.pause(); } } static create(options, renderer) { const video = options.video; let dimensions = options.dimensions; if (!options.dimensions) { dimensions = { x: .96, y: .56 }; } const materialPlane = SCNPlane.planeWithWidthHeight(dimensions.x, dimensions.y); let nativeUrl; let videoPlayer; if (typeof video === "string") { if (video.indexOf("://") >= 0) { nativeUrl = NSURL.URLWithString(video); } if (!nativeUrl) { try { let parts = video.split('/'); let name = parts.pop(); let dir = parts.join('/'); let nameParts = name.split('.'); let ext = nameParts.pop(); name = nameParts.join('.'); nativeUrl = NSBundle.mainBundle.URLForResourceWithExtensionSubdirectory(name, ext, dir); } catch (e) { console.error(e); } } if (!nativeUrl) { try { nativeUrl = NSURL.fileURLWithPath(video); } catch (e) { console.error(e); } } if (!nativeUrl) { throw "Unable to resolve file/url: " + nativeUrl; } videoPlayer = AVPlayer.playerWithURL(nativeUrl); } else { if (video instanceof AVPlayer) { videoPlayer = video; } else if (video.ios && video.ios instanceof AVPlayer) { videoPlayer = video.ios; } } materialPlane.firstMaterial.diffuse.contents = videoPlayer; materialPlane.firstMaterial.doubleSided = true; if (options.loop !== false) { const AVPlayerItemDidPlayToEndTimeNotificationObserver = core_1.Application.ios.addNotificationObserver(AVPlayerItemDidPlayToEndTimeNotification, (notification) => { if (videoPlayer.currentItem && videoPlayer.currentItem === notification.object) { videoPlayer.seekToTime(CMTimeMake(5, 100)); videoPlayer.play(); } }); } if (options.play !== false) { videoPlayer.play(); } const node = SCNNode.nodeWithGeometry(materialPlane); const arVideo = new ARVideo(options, node, renderer); arVideo.videoPlayer = videoPlayer; return arVideo; } } exports.ARVideo = ARVideo;