@osobh/reactar
Version:
AR Library for React Native and Expo
139 lines • 5.28 kB
JavaScript
;
// src/media/MediaCapture.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaCapture = void 0;
const react_native_view_shot_1 = require("react-native-view-shot");
const FileSystem = __importStar(require("expo-file-system"));
const events_1 = require("events");
class MediaCapture extends events_1.EventEmitter {
constructor() {
super();
this.isRecording = false;
this.recordingStartTime = 0;
this.mediaDirectory = `${FileSystem.documentDirectory}ar_media/`;
this.ensureMediaDirectory();
}
async ensureMediaDirectory() {
const dirInfo = await FileSystem.getInfoAsync(this.mediaDirectory);
if (!dirInfo.exists) {
await FileSystem.makeDirectoryAsync(this.mediaDirectory, { intermediates: true });
}
}
/**
* Captures a screenshot of the AR view
* @param viewRef React ref to the view to capture
* @param options Capture options
* @returns Promise with the path to the saved image
*/
async captureScreenshot(viewRef, options = {}) {
try {
const timestamp = Date.now();
const fileName = `ar_screenshot_${timestamp}.${options.format || 'jpg'}`;
const filePath = `${this.mediaDirectory}${fileName}`;
const uri = await (0, react_native_view_shot_1.captureRef)(viewRef, {
quality: options.quality || 0.8,
format: options.format || 'jpg',
result: 'tmpfile'
});
await FileSystem.moveAsync({
from: uri,
to: filePath
});
this.emit('screenshot-captured', filePath);
return filePath;
}
catch (error) {
this.emit('error', error);
throw error;
}
}
/**
* Starts recording the AR session
* @param viewRef React ref to the view to record
* @param options Recording options
*/
startRecording(viewRef, options = {}) {
if (this.isRecording) {
throw new Error('Recording is already in progress');
}
this.isRecording = true;
this.recordingStartTime = Date.now();
this.emit('recording-started');
// TODO: Implement actual video recording using react-native-view-shot
// This will require frame-by-frame capture and composition
}
/**
* Stops the current recording
* @returns Promise with the path to the saved video
*/
async stopRecording() {
if (!this.isRecording) {
throw new Error('No recording in progress');
}
const timestamp = this.recordingStartTime;
const fileName = `ar_recording_${timestamp}.mp4`;
const filePath = `${this.mediaDirectory}${fileName}`;
this.isRecording = false;
this.recordingStartTime = 0;
// TODO: Implement actual video recording stop and save logic
this.emit('recording-stopped', filePath);
return filePath;
}
/**
* Cleans up old media files
* @param maxAge Maximum age of files to keep (in milliseconds)
*/
async cleanupOldFiles(maxAge) {
try {
const files = await FileSystem.readDirectoryAsync(this.mediaDirectory);
const now = Date.now();
for (const file of files) {
const filePath = `${this.mediaDirectory}${file}`;
const fileInfo = await FileSystem.getInfoAsync(filePath);
const modTime = fileInfo.modificationTime;
if (modTime && (now - modTime > maxAge)) {
await FileSystem.deleteAsync(filePath);
}
}
}
catch (error) {
this.emit('error', error);
throw error;
}
}
}
exports.MediaCapture = MediaCapture;
//# sourceMappingURL=MediaCapture.js.map