我正在使用 Github 上的 streaming_audio_flutter_demo
项目。 https://github.com/suragch/streaming_audio_flutter_demo
它有一个类,为我的应用程序提供 ValueListenableBuilder
和一个 slider 以及播放和暂停控件。
唯一的问题是,我想将示例 URL 更改为我自己的;
static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';
但我不确定如何将它从我的应用程序的主页传递给类。这是 PageManager 类的代码;
class PageManager {
final progressNotifier = ValueNotifier<ProgressBarState>(
ProgressBarState(
current: Duration.zero,
buffered: Duration.zero,
total: Duration.zero,
),
);
final buttonNotifier = ValueNotifier<ButtonState>(ButtonState.paused);
late AudioPlayer _audioPlayer;
static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';
PageManager() {
_init();
}
void _init() async {
// initialize the song
_audioPlayer = AudioPlayer();
await _audioPlayer.setUrl(url);
}
}
我需要传递的字符串如下所示;
_current?.path
那么我怎样才能访问
static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';
从我的应用程序的主页?
最佳答案
替换
static const url = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3';
PageManager() {
_init();
}
与
String url; // don't use const variable!
PageManager({this.url="You can set a default URL here"}){
_init();
}
现在您可以在 main.dart
中使用它,如下所示:
_pageManager = PageManager(url: "YOUR URL");
让我知道结果。
关于flutter - 将字符串传递给 Flutter 中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68055971/