我正在使用flutter assets_audio_player包,它有一个错误。它正在播放多个音频,而不是替换现有音频。我尝试了许多方法来停止并在initState()方法中播放,但没有工作。我不想处理它,因为我希望能够导航到其他屏幕,同时在后台播放音频。
如果我不处理音频示例,它在后台播放很好,但当我再次加载相同的音频时,它不会停止音频播放,而是开始播放多个音频。
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/material.dart';
class AudioPlayer6Page extends StatefulWidget {
const AudioPlayer6Page({super.key});
@override
State<AudioPlayer6Page> createState() => _AudioPlayer6PageState();
}
class _AudioPlayer6PageState extends State<AudioPlayer6Page> {
late AssetsAudioPlayer assetsAudioPlayer;
@override
void initState() {
// TODO: implement initState
super.initState();
assetsAudioPlayer = AssetsAudioPlayer.newPlayer();
loadMedia();
}
Future<void> loadMedia() async {
try {
await assetsAudioPlayer.open(
Audio.network(
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
autoStart: false,
showNotification: true);
} catch (t) {
//mp3 unreachable
}
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
print('dispose');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text('Another audio player'),
),
StreamBuilder(
stream: assetsAudioPlayer.isPlaying,
builder: (context, asyncSnapshot) {
final bool? isPlaying = asyncSnapshot.data;
return isPlaying == true
? Center(
child: IconButton(
onPressed: () {
assetsAudioPlayer.pause();
},
icon: Icon(
Icons.pause,
size: 50,
)))
: Center(
child: IconButton(
onPressed: () {
assetsAudioPlayer.play();
},
icon: Icon(
Icons.play_arrow,
size: 50,
)),
);
}),
],
),
),
),
),
);
}
}
救命啊!
1条答案
按热度按时间ycl3bljg1#
我从来没有使用过assets_audio_player,但是我推荐使用https://pub.dev/packages/audio_service在后台播放音频。