我需要在 PageViewer 中使用 JustAudio Player。每次我滑动到不同的页面时,我都需要一个不同的 URL 才能在 JustAudio Player 中播放。我正在使用 Suragch 的 PageManager 来处理 Flutter 的 JustAudio PLayer 的滑块。
https://github.com/suragch/streaming_audio_flutter_demo/blob/master/lib/page_manager.dart
这是 PageView 生成器;
class _StageBuilderState extends State<StageBuilder> {
late PageManager _pageManager;
@override
void initState() {
super.initState();
_pageManager = PageManager();
}
@override
void dispose() {
_pageManager.dispose();
super.dispose();
}
@override
Widget build(context) {
return
PageView.builder(
itemCount: widget.speakcrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(widget.speakcrafts[currentIndex], context, currentIndex, widget.speakcrafts.length );
},
);
}
Widget createViewItem(AudioContent speakcraft, BuildContext context, int currentIndex, int currentlength) {
String kUrl = speakcraft.audioname!;
_pageManager = PageManager(url:kUrl);
return Column(
children: [
ValueListenableBuilder<ButtonState>(
valueListenable: _pageManager.buttonNotifier,
builder: (_, value, __) {
switch (value) {
case ButtonState.loading:
return Container(
margin: EdgeInsets.all(8.0),
width: 160.0,
height: 160.0,
child: CircularProgressIndicator(),
);
case ButtonState.paused:
return IconButton(
icon: Icon(Icons.play_arrow),
onPressed: _pageManager.play,
);
case ButtonState.playing:
return IconButton(
icon: Icon(Icons.pause),
onPressed: _pageManager.pause,
);
}
},
),
ValueListenableBuilder<ProgressBarState>(
valueListenable: _pageManager.progressNotifier,
builder: (_, value, __) {
return ProgressBar(
progress: value.current,
buffered: value.buffered,
total: value.total,
onSeek: _pageManager.seek
);
},
),
],
);
}
}
但是 Flutter 抱怨 Player 已经被初始化了;
LateInitializationError: Field '_pageManager@102518713' has already been initialized.
我试着改变这个;
_pageManager = PageManager(url:kUrl);
对此;
PageManager(url:kUrl);
但它没有播放。
那么如何将不同的 URL 传递给 PageView 中的播放器呢?