我使用以下代码制作了一个电台播放器应用。开启手机屏幕后,一切正常。但是,当我关闭手机屏幕时,收音机将在大约5-8分钟后停止播放。我有一些有关使用flutter audio_service的提示。 (https://pub.dev/packages/audio_service)但是我从应该从哪里开始感到困惑。我应该重新编码还是可以修改此代码。有人请帮助我。这将是一种恩典。谢谢你。
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
class Radio1 extends StatefulWidget {
@override
_Radio1State createState() => _Radio1State();
}
class _Radio1State extends State<Radio1> {
AudioPlayer audioPlayer = AudioPlayer();
@override
void initState() {
super.initState();
AudioPlayer.logEnabled = true;
}
bool _isPlaying = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
//new
SizedBox(
height: 50,
),
//Icon(
// Icons.arrow_drop_down,
//size: 40,
//),
//new
Container(
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 50),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Color(0x46000000),
offset: Offset(0, 20),
spreadRadius: 0,
blurRadius: 30,
),
BoxShadow(
color: Color(0x11000000),
offset: Offset(0, 10),
spreadRadius: 0,
blurRadius: 30,
),
],
),
//new
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image(
image: AssetImage("assets/radiologo.jpg"),
width: MediaQuery.of(context).size.width * 0.7,
height: MediaQuery.of(context).size.width * 0.7,
fit: BoxFit.cover,
),
),
),
Text(
"sample text",
style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500),
),
Text(
"(sample text)",
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
),
/* Slider(
value: 10,
onChanged: (v) {},
max: 170,
min: 0,
activeColor: Color(0xFF5E35B1),
), */
Text(
"sample text.",
style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: _isPlaying == false
? Icon(Icons.play_circle_outline)
: Icon(Icons.pause_circle_outline),
iconSize: 60.0,
onPressed: () {
getAudio();
},
),
IconButton(
icon: Icon(Icons.stop),
iconSize: 40,
onPressed: () {
stopAudio();
},
),
//new line
],
),
],
),
),
),
);
}
void getAudio() async {
var url = "http://ia802708.us.archive.org/3/items/count_monte_cristo_0711_librivox/count_of_monte_cristo_001_dumas.mp3";
if (_isPlaying) {
var res = await audioPlayer.pause();
if (res == 1) {
setState(() {
_isPlaying = false;
});
}
} else {
var res = await audioPlayer.play(url);
if (res == 1) {
setState(() {
_isPlaying = true;
});
}
}
}
void stopAudio() async {
int res = await audioPlayer.stop();
if (res == 1) {
setState(() {
_isPlaying = false;
});
}
}
void releaseAUdio() async {
await audioPlayer.stop();
await audioPlayer.release();
}
@override
void dispose() {
super.dispose();
releaseAUdio();
}
}
答案 0 :(得分:1)
因此,当您使用 audioplayers
包时,您需要实现 audio_service
包以实现您想要的(在后台播放音频)。实际上,audioplayers
包只负责播放音频文件,并不处理后台行为。
audio_service
旨在成为您应用程序中唯一的真实来源。因此,您需要重新构建代码以适应。
但是不要删除您的代码,您可能不需要对音频进行很多更改。
包装被分成多个部分。例如,一个用于后台任务,另一个用于 UI 告诉后台任务您想要做什么(播放、暂停、seekTo,...),因此您可能需要在代码中进行的唯一更改是调用这部分,称为 AudioService
(查看 API 参考以了解更多信息:https://pub.dev/documentation/audio_service/latest/audio_service/AudioService-class.html)。
一旦你这样做了,当然你必须实现你的后台任务来满足你的需求。
总结:
audio_service
包(或类似的包)来处理后台行为。audio_session
包,以处理您的应用与手机上不同音频交互之间的交互。 (例如,处理收到的通知并因此降低应用的音量)。希望这个回答对你有帮助,祝你好运:)