英文:
Webcam only works when I lock and unlock smartphone
问题
我正在使用Flutter在[flutter_webrtc](https://pub.dev/packages/flutter_webrtc)插件上做一个简单的演示应用,遵循[这个](https://www.100ms.live/blog/flutter-webrtc#resources)教程。当我在Chrome上测试时,一切正常,在模拟器上相机图像从未显示,当我在我的Android智能手机上测试时,只有在我锁定(电源按钮)并解锁(再次按电源按钮)设备时相机图像才会出现。
我使用的是Windows 11,Android Studio 2022.1.1 Patch 1,Flutter 3.7.5,Dart 2.19.1 OpenJDK 19.0.2和flutter_webrtc 0.9.23。
现在我的代码如下:
```dart
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _localVideoRenderer = RTCVideoRenderer();
void initRenderers() async {
await _localVideoRenderer.initialize();
}
_getUserMedia() async {
final Map<String, dynamic> mediaConstraints = {
'audio': true,
'video': true,
};
MediaStream stream =
await navigator.mediaDevices.getUserMedia(mediaConstraints);
_localVideoRenderer.srcObject = stream;
_localVideoRenderer.srcObject = stream;
return stream;
}
@override
void initState() {
initRenderers();
_getUserMedia();
super.initState();
}
@override
void dispose() async {
await _localVideoRenderer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final ButtonStyle style =
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: [
Positioned(
top: 0.0,
right: 0.0,
left: 0.0,
bottom: 0.0,
child: RTCVideoView(_localVideoRenderer))
],
),
);
}
}
英文:
I'm doing a simple demo app in Flutter using the flutter_webrtc plugin following this tutorial. When I test using Chrome its works just fine, in emulator the camera image never shows, and when I test on my Android smartphone the camera image only appears when I lock (power button) and unlock (power button again) the device.
I am using Windows 11, Android Studio 2022.1.1 Patch 1, Flutter 3.7.5, Dart 2.19.1 OpenJDK 19.0.2 and flutter_webrtc 0.9.23.
Right now my code is the following:
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _localVideoRenderer = RTCVideoRenderer();
void initRenderers() async {
await _localVideoRenderer.initialize();
}
_getUserMedia() async {
final Map<String, dynamic> mediaConstraints = {
'audio': true,
'video': true,
};
MediaStream stream =
await navigator.mediaDevices.getUserMedia(mediaConstraints);
_localVideoRenderer.srcObject = stream;
_localVideoRenderer.srcObject = stream;
return stream;
}
@override
void initState() {
initRenderers();
_getUserMedia();
super.initState();
}
@override
void dispose() async {
await _localVideoRenderer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final ButtonStyle style =
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: [
Positioned(
top: 0.0,
right: 0.0,
left: 0.0,
bottom: 0.0,
child: RTCVideoView(_localVideoRenderer))
],
),
);
}
}
I already tried to test on multiple platforms, already tried to invoke the method to show the camera image on a button and aready tried to use another tutorials.
答案1
得分: 0
这似乎与Flutter状态管理有关,我添加了:
setState(() {
});
在 srcObject = stream
之后,它就起作用了!
英文:
This seems to be related to flutter state management, I added:
setState(() {
});
After srcObject = stream
and it works!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论