描述错误我以这里的例子为例https://pub.dev/packages/just_audio#-示例选项卡-我有一个实时流音频,一旦加载UI,应用程序就会一直在后台缓冲流。因此消耗数据。我们如何告诉玩家只有在按下播放按钮时才启动缓冲区。这是我的代码
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
void main() => runApp(RadioTest());
class RadioTest extends StatefulWidget {
@override
_RadioTestState createState() => _RadioTestState();
}
class _RadioTestState extends State<RadioTest> {
AudioPlayer audioPlayer;
String url = "http://canada1.reliastream.com:8052/live?type=.mp3";
@override
void initState() {
super.initState();
audioPlayer = AudioPlayer();
audioPlayer.setUrl(url).catchError((error){
print(error);
});
print("Radio Activated");
}
@override
void dispose() {
audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Radio Test"),
),
body: Container(
child: Row(
children: <Widget>[
Text("Hi"),
StreamBuilder<FullAudioPlaybackState>(
stream: audioPlayer.fullPlaybackStateStream,
builder: (context, snapshot) {
final fullState = snapshot.data;
final state = fullState?.state;
final buffering = fullState?.buffering;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
if (state == AudioPlaybackState.connecting ||
buffering == true)
Container(
margin: EdgeInsets.all(8.0),
width: 64.0,
height: 64.0,
child: CircularProgressIndicator(),
)
else if (state == AudioPlaybackState.playing)
IconButton(
icon: Icon(Icons.pause),
iconSize: 64.0,
onPressed: audioPlayer.pause,
)
else
IconButton(
icon: Icon(Icons.play_arrow),
iconSize: 64.0,
onPressed: audioPlayer.play,
),
IconButton(
icon: Icon(Icons.stop),
iconSize: 64.0,
onPressed: state == AudioPlaybackState.stopped ||
state == AudioPlaybackState.none
? null
: audioPlayer.stop,
),
],
);
},
),
],
),
),
),
);
}
}
预期行为在按下播放按钮之前,它不应该在应用程序打开时缓冲。
智能手机(请填写以下信息(:
设备:Iphone 11 pro
操作系统:iOS 13
颤振刮刀
[✓] Flutter (Channel stable, 3.0.5, on macOS 12.5 21G72 darwin-arm, locale en-IN)
• Flutter version 3.0.5 at /Users/aryanwathre/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision f1875d570e (6 weeks ago), 2022-07-13 11:24:16 -0700
• Engine revision e85ea0e79c
• Dart version 2.17.6
• DevTools version 2.12.2
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1)
• Android SDK at /Users/aryanwathre/Library/Android/sdk
• Platform android-32, build-tools 32.0.0-rc1
• Java binary at: /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java
• Java version Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] VS Code (version 1.70.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.46.0
[✓] VS Code (version 1.69.1)
• VS Code at /Users/aryanwathre/Desktop/bhavesh/Visual Studio Code.app/Contents
• Flutter extension version 3.46.0
[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-arm64 • macOS 12.5 21G72 darwin-arm
• Chrome (web) • chrome • web-javascript • Google Chrome 104.0.5112.101
! Error: Tirth Patel's iPhone is not connected. Xcode will continue when Tirth Patel's iPhone is connected. (code -13)
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
尝试使用setAudioSource
@override
void initState() {
super.initState();
audioPlayer = AudioPlayer();
audioPlayer.setAudioSource(AudioSource.uri(Uri(path: url)));
}
void main() => runApp(MaterialApp(home: RadioTest()));
class RadioTest extends StatefulWidget {
@override
_RadioTestState createState() => _RadioTestState();
}
class _RadioTestState extends State<RadioTest> {
late AudioPlayer audioPlayer;
String url = "http://canada1.reliastream.com:8052/live?type=.mp3";
@override
void initState() {
super.initState();
audioPlayer = AudioPlayer();
audioPlayer.setUrl(url).catchError((error) {
print(error);
});
}
@override
void dispose() {
audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
floatingActionButton: FloatingActionButton(onPressed: () {
audioPlayer.play();
}),
);
}
}
和播放呼叫await audioPlayer.play()