我有一个使用 WebRTC 的 flutter 项目(iOS、Android)。我需要从摄像头发送视频(工作正常)并通过 WebRTC 进行屏幕捕获。如何使用flutter_webrtc包在WebRTC上共享屏幕?
您可以使用 flutter_webrtc 插件并按照此方法进行操作(使用 webRTC 中的 getDisplayMedia 方法来获取显示):
class ScreenSharing {
MediaStream? _localStream;
final RTCVideoRenderer _localRenderer = RTCVideoRenderer();
Future<void> _makeScreenSharing() async {
final mediaConstraints = <String, dynamic>{'audio': true, 'video': true};
try {
var stream = await navigator.mediaDevices.getDisplayMedia(mediaConstraints);
_localStream = stream;
_localRenderer.srcObject = _localStream;
} catch (e) {
print(e.toString());
}
}
}
我想完成@MojtabaGhiasi 的回答,因为我花了一些时间才弄清楚。 该代码工作正常,但它会使您的应用程序崩溃。 将
flutter_background
添加到您的应用程序中。
将
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
添加到您的 Android Manifest
。
添加
<service android:name="de.julianassmann.flutter_background.IsolateHolderService" android:foregroundServiceType="mediaProjection" android:enabled="true" android:exported="false"/>
也给你的Android Manifest
添加此函数并在main中调用它
Future<bool> startForegroundService() async {
final androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: 'Title of the notification',
notificationText: 'Text of the notification',
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: AndroidResource(
name: 'background_icon',
defType: 'drawable'), // Default is ic_launcher from folder mipmap
);
await FlutterBackground.initialize(androidConfig: androidConfig);
return FlutterBackground.enableBackgroundExecution();
}
main(){
if (WebRTC.platformIsAndroid) {
startForegroundService();
}
runApp(MyApp());
}