我使用provider与video_player管理的状态。
provider
video_player
VideoProvider具有:
videoPlayerController = VideoPlayerController.network(...);
当用户切换到新视频(同一屏幕)时,我经常更改此设置。如果我直接分配一个新的VideoPlayerController.network(...)进入videoPlayerController,旧的视频仍将发挥,你可以听到声音。解决方法是videoPlayerController.pause()随后分配一个新的VideoPlayerCOntroller.network。
VideoPlayerController.network(...)
videoPlayerController
videoPlayerController.pause()
VideoPlayerCOntroller.network
GC是否处置了先前的视频?这有任何性能问题吗?我想摆脱以前的视频和切换到新视频之前使用的资源。videoPlayerController.dispose()切换之前无法执行,因为它会导致错误。
videoPlayerController.dispose()
调用时dispose, VideoPlayer 小部件仍在使用您的控制器。首先,您需要确保不再使用它(将controller的状态设置为null),然后再调用dispose。
dispose
我不确定您是如何通过Provider进行状态管理的,但我将举例说明如何使用常规State进行此操作。
VideoPlayerController _controller; void _initController(String link) { _controller = VideoPlayerController.network(link) ..initialize().then((_) { setState(() {}); }); } Future<void> _onControllerChange(String link) async { if (_controller == null) { // If there was no controller, just create a new one _initController(link); } else { // If there was a controller, we need to dispose of the old one first final oldController = _controller; // Registering a callback for the end of next frame // to dispose of an old controller // (which won't be used anymore after calling setState) WidgetsBinding.instance.addPostFrameCallback((_) async { await oldController.dispose(); // Initing new controller _initController(link); }); // Making sure that controller is not used by setting it to null setState(() { _controller = null; }); } }