nativescript-ar
Version:
NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.
95 lines (94 loc) • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var application = require("tns-core-modules/application");
var arcommon_1 = require("./arcommon");
var ARVideo = (function (_super) {
__extends(ARVideo, _super);
function ARVideo() {
return _super !== null && _super.apply(this, arguments) || this;
}
ARVideo.prototype.isPlaying = function () {
return this.videoPlayer && this.videoPlayer.timeControlStatus === 2;
};
ARVideo.prototype.play = function () {
if (this.videoPlayer) {
this.videoPlayer.play();
}
};
ARVideo.prototype.pause = function () {
if (this.videoPlayer) {
this.videoPlayer.pause();
}
};
ARVideo.create = function (options, renderer) {
var video = options.video;
var dimensions = options.dimensions;
if (!options.dimensions) {
dimensions = {
x: .96,
y: .56
};
}
var materialPlane = SCNPlane.planeWithWidthHeight(dimensions.x, dimensions.y);
var nativeUrl;
var videoPlayer;
if (typeof video === "string") {
if (video.indexOf("://") >= 0) {
nativeUrl = NSURL.URLWithString(video);
}
if (!nativeUrl) {
try {
var parts = video.split('/');
var name_1 = parts.pop();
var dir = parts.join('/');
var nameParts = name_1.split('.');
var ext = nameParts.pop();
name_1 = nameParts.join('.');
nativeUrl = NSBundle.mainBundle.URLForResourceWithExtensionSubdirectory(name_1, 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) {
var AVPlayerItemDidPlayToEndTimeNotificationObserver = application.ios.addNotificationObserver(AVPlayerItemDidPlayToEndTimeNotification, function (notification) {
if (videoPlayer.currentItem && videoPlayer.currentItem === notification.object) {
videoPlayer.seekToTime(CMTimeMake(5, 100));
videoPlayer.play();
}
});
}
if (options.play !== false) {
videoPlayer.play();
}
var node = SCNNode.nodeWithGeometry(materialPlane);
var arVideo = new ARVideo(options, node, renderer);
arVideo.videoPlayer = videoPlayer;
return arVideo;
};
return ARVideo;
}(arcommon_1.ARCommonNode));
exports.ARVideo = ARVideo;