我的代码正在运行,但调试控制台显示警告:
class _VideoStreamState extends State<VideoStream> {
int index = 0;
double _position = 0;
double _buffer = 0;
bool _lock = true;
Map<String, VideoPlayerController> _controllers = {};
Map<int, VoidCallback> _listeners = {};
Set<String> _urls = {
'video/000.mp4#1',
'video/233.mp4#2',
'video/56.mp4#3',
'video/12.mp4#4',
'video/149_M.mp4#5',
};
@override
void initState() {
super.initState();
if (_urls.length > 0) {
_initController(0).then((_) {
_playController(0);
}).onError((error, stackTrace) {
print(error);
});
}
if (_urls.length > 1) {
_initController(1)
.whenComplete(() => _lock = false)
.onError((error, stackTrace) {
print(error);
});
}
}
VoidCallback _listenerSpawner(index) {
return () {
int dur = _controller(index).value.duration.inMilliseconds;
int pos = _controller(index).value.position.inMilliseconds;
int buf = _controller(index).value.buffered.last.end.inMilliseconds;
print(buf);
setState(() {
if (dur <= pos) {
_position = 0;
return;
}
_position = pos / dur;
_buffer = buf / dur;
});
if (dur - pos < 1) {
if (index < _urls.length - 1) {
_nextVideo();
}
}
};
}
VideoPlayerController _controller(int index) {
return _controllers[_urls.elementAt(index)];
}
Future<void> _initController(int index) async {
var controller = VideoPlayerController.asset(_urls.elementAt(index));
_controllers[_urls.elementAt(index)] = controller;
await controller.initialize();
}
void _removeController(int index) {
_controller(index).dispose();
_controllers.remove(_urls.elementAt(index));
_listeners.remove(index);
}
void _stopController(int index) {
_controller(index).removeListener(_listeners[index]);
_controller(index).pause();
_controller(index).seekTo(Duration(milliseconds: 0));
}
void _playController(int index) async {
if (!_listeners.keys.contains(index)) {
_listeners[index] = _listenerSpawner(index);
}
_controller(index).addListener(_listeners[index]);
await _controller(index).play();
setState(() {});
}
void _previousVideo() {
if (_lock || index == 0) {
return;
}
_lock = true;
_stopController(index);
if (index + 1 < _urls.length) {
_removeController(index + 1);
}
_playController(--index);
if (index == 0) {
_lock = false;
} else {
_initController(index - 1)
.whenComplete(() => _lock = false)
.onError((error, stackTrace) {
print(error);
});
}
}
void _nextVideo() async {
if (_lock || index == _urls.length - 1) {
return;
}
_lock = true;
_stopController(index);
if (index - 1 >= 0) {
_removeController(index - 1);
}
_playController(++index);
if (index == _urls.length - 1) {
_lock = false;
} else {
_initController(index + 1)
.whenComplete(() => _lock = false)
.onError((error, stackTrace) {
print(error);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Playing ${index + 1} of ${_urls.length}"),
),
body: Stack(
children: [
GestureDetector(
onLongPressStart: (_) => _controller(index).pause(),
onLongPressEnd: (_) => _controller(index).play(),
child: Center(
child: AspectRatio(
aspectRatio: _controller(index).value.aspectRatio,
child: Center(child: VideoPlayer(_controller(index))),
),
),
),
Positioned(
child: Container(
height: 10,
width: MediaQuery.of(context).size.width * _buffer,
color: Colors.grey,
),
),
Positioned(
child: Container(
height: 10,
width: MediaQuery.of(context).size.width * _position,
color: Colors.greenAccent,
),
),
],
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: _previousVideo,
heroTag: "back",
child: Icon(Icons.arrow_back)),
SizedBox(width: 24),
FloatingActionButton(
onPressed: _nextVideo,
heroTag: "forward",
child: Icon(Icons.arrow_forward)),
],
),
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_removeController(index);
}
}
和我的错误信息警告:
======== Exception caught by foundation library ====================================================
The following StateError was thrown while dispatching notifications for VideoPlayerController:
Bad state: No element
When the exception was thrown, this was the stack:
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49 throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 337:5 last]
packages/workour_honamik/VideoApp.dart 59:34 listener
packages/flutter/src/foundation/change_notifier.dart 243:16 notifyListeners
packages/flutter/src/foundation/change_notifier.dart 309:5 set value
...
The VideoPlayerController sending notification was: VideoPlayerController#ba32c(VideoPlayerValue(duration: 0:00:01.000000, size: Size(1080.0, 1080.0), position: 0:00:00.000000, caption: Caption(number: 0, start: 0:00:00.000000, end: 0:00:00.000000, text: ), buffered: [], isInitialized: true, isPlaying: true, isLooping: false, isBuffering: false, volume: 1, playbackSpeed: 1, errorDescription: null))
谁能帮我解决这个问题?