admin管理员组文章数量:1391934
I am using Flutter video_player package to play video on Android, in recent updates of Flutter or package, the app stops playing back video when I send app to background, meanwhile there are some crashes and errors in log, here is my code, logs at end of question :
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with WidgetsBindingObserver {
int _counter = 0;
late VideoPlayerController _controller;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void initState() {
super.initState();
_controller = VideoPlayerControllerworkUrl(Uri.parse(
'.mp4')
,videoPlayerOptions: VideoPlayerOptions(
allowBackgroundPlayback: true, mixWithOthers: true),)
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
WidgetsBinding.instance.addObserver(this);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: _controller.value.isInitialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer (_controller),
)
: Container(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
});
},
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
print("AppLifecycleState.paused");
}
}
}
And here is log of device:
2025-03-13 10:04:21.783 8186-8186 flutter com.example.untitled4 I AppLifecycleState.paused
2025-03-13 10:04:21.784 8186-8186 SurfaceView@6b4b083 com.example.untitled4 D updateSurface: surface is not valid
2025-03-13 10:04:21.863 14716-15805 WindowManager system_server E win=Window{fd23590 u0 com.example.untitled4/com.example.untitled4.MainActivity} destroySurfaces: appStopped=true cleanupOnResume=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8 caller=com.android.server.wm.ActivityRecord.destroySurfaces:7033 com.android.server.wm.ActivityRecord.destroySurfaces:7014 com.android.server.wm.ActivityRecord.activityStopped:7747 com.android.server.wm.ActivityClientController.activityStopped:321 android.app.IActivityClientController$Stub.onTransact:702 com.android.server.wm.ActivityClientController.onTransact:186 android.os.Binder.execTransactInternal:1380
2025-03-13 10:04:21.863 14716-15805 WindowManager system_server I Destroying surface Surface(name=com.example.untitled4/com.example.untitled4.MainActivity$_8186)/@0x709f3e3 called by com.android.server.wm.WindowStateAnimator.destroySurface:811 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:478 com.android.server.wm.WindowState.destroySurfaceUnchecked:4219 com.android.server.wm.WindowState.destroySurface:4193 com.android.server.wm.ActivityRecord.destroySurfaces:7033 com.android.server.wm.ActivityRecord.destroySurfaces:7014 com.android.server.wm.ActivityRecord.activityStopped:7747 com.android.server.wm.ActivityClientController.activityStopped:321
本文标签:
版权声明:本文标题:Video playback stopped in Flutter when app goes to background, some crashes in log - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744716777a2621451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论