Flutter屏幕录制插件ed_screen_recorder的使用
Flutter屏幕录制插件ed_screen_recorder的使用
概述
ed_screen_recorder
是一个用于Flutter应用的屏幕录制插件,支持iOS和Android设备。需要注意的是,该插件在iOS模拟器上无法工作。
插件链接
配置步骤
Android配置
android/app/build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.github.HBiSoft:HBRecorder:3.0.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
}
android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
</manifest>
iOS配置
在Info.plist
中添加权限描述:
<key>NSPhotoLibraryUsageDescription</key>
<string>Save video in gallery</string>
<key>NSMicrophoneUsageDescription</key>
<string>Save audio in video</string>
使用示例
示例代码
以下是一个完整的示例,展示如何使用ed_screen_recorder
进行屏幕录制。
import 'dart:async';
import 'dart:io';
import 'package:ed_screen_recorder/ed_screen_recorder.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
EdScreenRecorder? screenRecorder;
RecordOutput? _response;
bool inProgress = false;
@override
void initState() {
super.initState();
screenRecorder = EdScreenRecorder();
}
Future<void> startRecord({required String fileName, required int width, required int height}) async {
Directory? tempDir = await getApplicationDocumentsDirectory();
String? tempPath = tempDir.path;
try {
var startResponse = await screenRecorder?.startRecordScreen(
fileName: fileName,
dirPathToSave: tempPath,
audioEnable: true,
width: width,
height: height,
);
setState(() {
_response = startResponse;
});
} on PlatformException catch (e) {
print("Error: An error occurred while starting the recording! $e");
}
}
Future<void> stopRecord() async {
try {
var stopResponse = await screenRecorder?.stopRecord();
setState(() {
_response = stopResponse;
});
} on PlatformException catch (e) {
print("Error: An error occurred while stopping recording. $e");
}
}
Future<void> pauseRecord() async {
try {
await screenRecorder?.pauseRecord();
} on PlatformException catch (e) {
print("Error: An error occurred while pause recording. $e");
}
}
Future<void> resumeRecord() async {
try {
await screenRecorder?.resumeRecord();
} on PlatformException catch (e) {
print("Error: An error occurred while resume recording. $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Screen Recording Debug"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("File: ${_response?.file.path}"),
Text("Status: ${_response?.success.toString()}"),
Text("Event: ${_response?.eventName}"),
Text("Progress: ${_response?.isProgress.toString()}"),
Text("Message: ${_response?.message}"),
Text("Video Hash: ${_response?.videoHash}"),
Text("Start Date: ${(_response?.startDate).toString()}"),
Text("End Date: ${(_response?.endDate).toString()}"),
ElevatedButton(
onPressed: () => startRecord(
fileName: "eren",
width: MediaQuery.of(context).size.width.toInt(),
height: MediaQuery.of(context).size.height.toInt(),
),
child: const Text('START RECORD'),
),
ElevatedButton(onPressed: () => resumeRecord(), child: const Text('RESUME RECORD')),
ElevatedButton(onPressed: () => pauseRecord(), child: const Text('PAUSE RECORD')),
ElevatedButton(onPressed: () => stopRecord(), child: const Text('STOP RECORD')),
],
),
),
);
}
}
功能特性
- 支持iOS操作系统。
- 自定义文件路径和目录。
- 自定义音频录制。
- 自定义视频帧率。
- 代码优化。
反馈
如果有任何反馈,请联系作者:erndemir.1@gmail.com
更多关于Flutter屏幕录制插件ed_screen_recorder的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter屏幕录制插件ed_screen_recorder的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用ed_screen_recorder
插件来进行屏幕录制的代码示例。这个插件允许你捕获屏幕内容并将其保存为视频文件。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加ed_screen_recorder
依赖:
dependencies:
flutter:
sdk: flutter
ed_screen_recorder: ^最新版本号 # 请替换为实际最新版本号
然后运行flutter pub get
来安装依赖。
2. 导入插件
在你的Dart文件中导入ed_screen_recorder
插件:
import 'package:ed_screen_recorder/ed_screen_recorder.dart';
3. 初始化插件并开始录制
下面是一个完整的示例,展示如何初始化插件并开始录制屏幕:
import 'package:flutter/material.dart';
import 'package:ed_screen_recorder/ed_screen_recorder.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ScreenRecorderDemo(),
);
}
}
class ScreenRecorderDemo extends StatefulWidget {
@override
_ScreenRecorderDemoState createState() => _ScreenRecorderDemoState();
}
class _ScreenRecorderDemoState extends State<ScreenRecorderDemo> {
late ScreenRecorderController _screenRecorderController;
bool _isRecording = false;
@override
void initState() {
super.initState();
_screenRecorderController = ScreenRecorderController(
onStatusChanged: (status) {
print('Screen Recorder Status: $status');
},
onError: (error) {
print('Screen Recorder Error: $error');
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen Recorder Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
if (!_isRecording) {
// 开始录制
bool hasPermission = await _screenRecorderController.requestPermissions();
if (hasPermission) {
await _screenRecorderController.startRecording(
outputPath: '/storage/emulated/0/DCIM/Camera/screen_record_${DateTime.now().millisecondsSinceEpoch}.mp4',
);
setState(() {
_isRecording = true;
});
} else {
// 处理权限被拒绝的情况
print('Permissions denied');
}
} else {
// 停止录制
await _screenRecorderController.stopRecording();
setState(() {
_isRecording = false;
});
}
},
child: Text(_isRecording ? 'Stop Recording' : 'Start Recording'),
),
],
),
),
);
}
@override
void dispose() {
_screenRecorderController.dispose();
super.dispose();
}
}
4. 注意事项
- 权限处理:在实际应用中,你需要处理存储权限。这个示例代码中已经包含了请求权限的逻辑。
- 路径问题:确保提供的输出路径是设备可写的路径。
- 错误处理:在生产环境中,你可能需要更详细的错误处理和用户反馈。
这个示例代码展示了如何使用ed_screen_recorder
插件在Flutter应用中实现屏幕录制功能。你可以根据需求进一步定制和扩展这个示例。