入门
-
安装软件包
Terminal window npm i @capgo/capacitor-screen-recorderTerminal window pnpm add @capgo/capacitor-screen-recorderTerminal window yarn add @capgo/capacitor-screen-recorderTerminal window bun add @capgo/capacitor-screen-recorder -
与原生项目同步
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
配置权限
将使用说明添加到您的
Info.plist中:<key>NSMicrophoneUsageDescription</key><string>To record audio with screen recording</string><key>NSPhotoLibraryUsageDescription</key><string>To save screen recordings</string>Android
Section titled “Android”向您的
AndroidManifest.xml添加权限:<uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
导入插件并使用其方法来录制屏幕:
import { ScreenRecorder } from '@capgo/capacitor-screen-recorder';
// Start recordingconst startRecording = async () => { try { await ScreenRecorder.start(); console.log('Recording started'); } catch (error) { console.error('Failed to start recording:', error); }};
// Stop recordingconst stopRecording = async () => { try { const result = await ScreenRecorder.stop(); console.log('Recording saved to:', result.videoUrl);
// You can now use the video URL // For example, share it or play it back } catch (error) { console.error('Failed to stop recording:', error); }};
// Check if recording is supportedconst checkSupport = async () => { const { value } = await ScreenRecorder.isSupported(); console.log('Screen recording supported:', value);};
// Check if currently recordingconst checkRecordingStatus = async () => { const { value } = await ScreenRecorder.isRecording(); console.log('Currently recording:', value);};API 参考
Section titled “API 参考”开始屏幕录制。
await ScreenRecorder.start();停止录制并返回视频文件路径。
interface RecordingResult { videoUrl: string;}
const result = await ScreenRecorder.stop();isSupported()
Section titled “isSupported()”检查设备是否支持屏幕录制。
const { value } = await ScreenRecorder.isSupported();// Returns: { value: boolean }isRecording()
Section titled “isRecording()”检查屏幕录制当前是否处于活动状态。
const { value } = await ScreenRecorder.isRecording();// Returns: { value: boolean }使用选项进行录制
Section titled “使用选项进行录制”// iOS-specific optionsconst startWithOptions = async () => { await ScreenRecorder.start({ // iOS only options recordAudio: true, microphoneAudio: true, showIOSNotification: true, notificationText: "Recording in progress..." });};完整的录音流程
Section titled “完整的录音流程”import { ScreenRecorder } from '@capgo/capacitor-screen-recorder';import { Share } from '@capacitor/share';
export class RecordingService { private isRecording = false;
async toggleRecording() { if (this.isRecording) { await this.stopRecording(); } else { await this.startRecording(); } }
private async startRecording() { try { // Check if supported const { value: isSupported } = await ScreenRecorder.isSupported(); if (!isSupported) { throw new Error('Screen recording not supported'); }
// Start recording await ScreenRecorder.start(); this.isRecording = true;
console.log('Recording started'); } catch (error) { console.error('Failed to start recording:', error); throw error; } }
private async stopRecording() { try { const result = await ScreenRecorder.stop(); this.isRecording = false;
console.log('Recording saved:', result.videoUrl);
// Option to share the recording await this.shareRecording(result.videoUrl); } catch (error) { console.error('Failed to stop recording:', error); throw error; } }
private async shareRecording(videoUrl: string) { try { await Share.share({ title: 'Screen Recording', text: 'Check out my screen recording!', url: videoUrl, dialogTitle: 'Share Recording' }); } catch (error) { console.error('Failed to share recording:', error); } }}-
使用前检查支持
const { value } = await ScreenRecorder.isSupported();if (!value) {// Hide recording feature or show alternative} -
正确处理权限 在iOS上,系统会自动提示权限。在 Android 上,确保您请求必要的权限。
-
提供用户反馈 录音处于活动状态时显示清晰的指示符:
let recordingInterval: any;const startRecording = async () => {await ScreenRecorder.start();// Show recording indicatorrecordingInterval = setInterval(() => {// Update UI to show recording duration}, 1000);};const stopRecording = async () => {clearInterval(recordingInterval);await ScreenRecorder.stop();}; -
处理中断
import { App } from '@capacitor/app';App.addListener('appStateChange', async ({ isActive }) => {if (!isActive) {// Consider stopping recording when app goes to backgroundconst { value } = await ScreenRecorder.isRecording();if (value) {await ScreenRecorder.stop();}}});
- 需要 iOS 11.0+
- 使用
ReplayKit框架 - 系统在状态栏中显示录音指示器
- 用户必须确认录音开始
Android
Section titled “Android”- 需要 Android 5.0 (API 21)+
- 使用
MediaProjectionAPI - 录制期间显示通知
- 某些设备可能具有制造商特定的限制
- 不支持网络平台
- 将返回
isSupported: false
-
录音无法开始
- 确保授予所有权限
- 检查另一个应用程序是否已经在录制
- 验证设备支持屏幕录制
-
录音中没有声音
- 检查麦克风权限
- 确保启用
recordAudio选项 - 部分设备可能不支持系统录音
-
找不到视频文件
- 检查文件权限
- 确保足够的存储空间
- 验证返回的视频URL是否有效