UNPKG

nativescript-ar

Version:

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

154 lines (153 loc) 6.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var application = require("tns-core-modules/application"); var DEFAULT_FRAMERATE = 30; var DEFAULT_BITRATE = 10000000; var FALLBACK_QUALITY_LEVELS = [ android.media.CamcorderProfile.QUALITY_HIGH, android.media.CamcorderProfile.QUALITY_2160P, android.media.CamcorderProfile.QUALITY_1080P, android.media.CamcorderProfile.QUALITY_720P, android.media.CamcorderProfile.QUALITY_480P ]; var VideoRecorder = (function () { function VideoRecorder() { this.recordingVideoFlag = false; this.bitRate = DEFAULT_BITRATE; this.frameRate = DEFAULT_FRAMERATE; this.recordingVideoFlag = false; } VideoRecorder.fromFragment = function (fragment) { var videoRecorder = new VideoRecorder(); videoRecorder.setSceneView(fragment.getArSceneView()); return videoRecorder; }; VideoRecorder.prototype.setSceneView = function (sceneView) { this.sceneView = sceneView; }; VideoRecorder.prototype.getVideoPath = function () { return this.videoPath; }; VideoRecorder.prototype.setBitRate = function (bitRate) { this.bitRate = bitRate; }; VideoRecorder.prototype.setFrameRate = function (frameRate) { this.frameRate = frameRate; }; VideoRecorder.prototype.onToggleRecord = function () { if (this.recordingVideoFlag) { this.stopRecordingVideo(); } else { this.startRecordingVideo(); } return this.recordingVideoFlag; }; VideoRecorder.prototype.startRecordingVideo = function () { if (this.recordingVideoFlag) { throw "already recording"; } if (this.mediaRecorder == null) { this.mediaRecorder = new android.media.MediaRecorder(); } this.videoPath = null; try { this.buildFilename(); this.setUpMediaRecorder(); } catch (e) { console.error(e); throw "Exception setting up recorder"; } this.encoderSurface = this.mediaRecorder.getSurface(); this.sceneView.startMirroringToSurface(this.encoderSurface, 0, 0, this.videoSize.getWidth(), this.videoSize.getHeight()); this.recordingVideoFlag = true; }; VideoRecorder.prototype.buildFilename = function () { if (this.videoDirectory == null) { this.videoDirectory = new java.io.File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES) + "/ARVideos"); } if (this.videoBaseName == null) { this.videoBaseName = "Sample"; } this.videoPath = new java.io.File(this.videoDirectory, this.videoBaseName + java.lang.Long.toHexString(java.lang.System.currentTimeMillis()) + ".mp4"); var dir = this.videoPath.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } }; VideoRecorder.prototype.stopRecordingVideo = function () { if (!this.recordingVideoFlag) { throw "not recording"; } this.recordingVideoFlag = false; if (this.encoderSurface != null) { this.sceneView.stopMirroringToSurface(this.encoderSurface); this.encoderSurface = null; } this.mediaRecorder.stop(); this.mediaRecorder.reset(); }; VideoRecorder.prototype.setUpMediaRecorder = function () { this.mediaRecorder.setVideoSource(android.media.MediaRecorder.VideoSource.SURFACE); this.mediaRecorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.MPEG_4); this.mediaRecorder.setOutputFile(this.videoPath.getAbsolutePath()); this.mediaRecorder.setVideoEncodingBitRate(this.bitRate); this.mediaRecorder.setVideoFrameRate(this.frameRate); this.mediaRecorder.setVideoSize(this.videoSize.getWidth(), this.videoSize.getHeight()); this.mediaRecorder.setVideoEncoder(this.videoCodec); this.mediaRecorder.prepare(); try { this.mediaRecorder.start(); } catch (e) { console.error("Exception starting capture: " + e.getMessage()); } }; VideoRecorder.prototype.setVideoSize = function (width, height) { this.videoSize = new android.util.Size(width, height); }; VideoRecorder.prototype.setVideoQualityAuto = function () { var orientation = application.android.context.getResources().getConfiguration().orientation; this.setVideoQuality(android.media.CamcorderProfile.QUALITY_2160P, orientation); }; VideoRecorder.prototype.setVideoQuality = function (quality, orientation) { var profile = null; if (android.media.CamcorderProfile.hasProfile(quality)) { profile = android.media.CamcorderProfile.get(quality); } if (profile == null) { for (var _i = 0, FALLBACK_QUALITY_LEVELS_1 = FALLBACK_QUALITY_LEVELS; _i < FALLBACK_QUALITY_LEVELS_1.length; _i++) { var level = FALLBACK_QUALITY_LEVELS_1[_i]; if (android.media.CamcorderProfile.hasProfile(level)) { profile = android.media.CamcorderProfile.get(level); console.log(">> profile found: " + profile); break; } } } if (profile == null) { return; } if (orientation === android.content.res.Configuration.ORIENTATION_LANDSCAPE) { this.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); } else { this.setVideoSize(profile.videoFrameHeight, profile.videoFrameWidth); } this.setVideoCodec(profile.videoCodec); this.setBitRate(profile.videoBitRate); this.setFrameRate(profile.videoFrameRate); }; VideoRecorder.prototype.setVideoCodec = function (videoCodec) { this.videoCodec = videoCodec; }; VideoRecorder.prototype.isRecording = function () { return this.recordingVideoFlag; }; return VideoRecorder; }()); exports.VideoRecorder = VideoRecorder;