如何在flutter中同时播放多个音频文件
How to play multiple audio files simultaniously in flutter
我一直在尝试同时 运行 多个音频文件,每个文件都有一个单独的静音按钮。我试过 AudioPlayer Flutter 插件。
我已经尝试了以下代码,但它一个接一个地生成文件 运行,而不是同时生成。
请帮忙!!
Future play() async {
await audioPlayer.play(url);
await audioPlayer2.play(url2);
setState(() {
playerState = AudioPlayerState.PLAYING;
});
}
可能它们是更合适的解决方案,但这解决了我目前的问题,我使用了插件 audioplayers 并为每个音频文件创建了多个实例。
更新:例如,对于两个音频文件,它的完成方式如下:
enum PlayerState { stopped, playing, paused }
enum PlayerState1 { stopped1, playing1, paused1 }
class AudioScreen extends StatefulWidget {
@override
_AudioScreenState createState() =>_AudioScreenState();
}
class _AudioScreenState extends State<AudioScreen> {
//for first audio
AudioPlayer audioPlayer;
PlayerState playerState = PlayerState.stopped;
get isPlaying => playerState == PlayerState.playing;
//for second audio
AudioPlayer audioPlayer1;
PlayerState1 playerState1 = PlayerState1.stopped1;
get isPlaying1 => playerState1 == PlayerState1.playing1;
@override
void initState() {
super.initState();
audioPlayer = new AudioPlayer();
audioPlayer1 = new AudioPlayer();
}
Future play() async {
await audioPlayer.play(url);
setState(() {
playerState = PlayerState.playing;
});
await audioPlayer1.play(url1);
setState(() {
playerState1 = PlayerState1.playing1;
});
}
Future stop() async {
await audioPlayer.stop();
setState(() {
playerState = PlayerState.stopped;
});
await audioPlayer1.stop();
setState(() {
playerState1 = PlayerState1.stopped1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:Column(
children:[
IconButton(
onPressed:() play(),
iconSize: 70.0,
icon:Icon(Icons.play_circle_outline, size: 55),
),
IconButton(
onPressed: stop(),
iconSize: 55.0,
icon: Icon(Icons.stop),
),
]
),
);
}
}
通过这种方式,您可以创建多个实例或只使用循环为每个音频创建多个实例。
我一直在尝试同时 运行 多个音频文件,每个文件都有一个单独的静音按钮。我试过 AudioPlayer Flutter 插件。
请帮忙!!
Future play() async {
await audioPlayer.play(url);
await audioPlayer2.play(url2);
setState(() {
playerState = AudioPlayerState.PLAYING;
});
}
可能它们是更合适的解决方案,但这解决了我目前的问题,我使用了插件 audioplayers 并为每个音频文件创建了多个实例。
更新:例如,对于两个音频文件,它的完成方式如下:
enum PlayerState { stopped, playing, paused }
enum PlayerState1 { stopped1, playing1, paused1 }
class AudioScreen extends StatefulWidget {
@override
_AudioScreenState createState() =>_AudioScreenState();
}
class _AudioScreenState extends State<AudioScreen> {
//for first audio
AudioPlayer audioPlayer;
PlayerState playerState = PlayerState.stopped;
get isPlaying => playerState == PlayerState.playing;
//for second audio
AudioPlayer audioPlayer1;
PlayerState1 playerState1 = PlayerState1.stopped1;
get isPlaying1 => playerState1 == PlayerState1.playing1;
@override
void initState() {
super.initState();
audioPlayer = new AudioPlayer();
audioPlayer1 = new AudioPlayer();
}
Future play() async {
await audioPlayer.play(url);
setState(() {
playerState = PlayerState.playing;
});
await audioPlayer1.play(url1);
setState(() {
playerState1 = PlayerState1.playing1;
});
}
Future stop() async {
await audioPlayer.stop();
setState(() {
playerState = PlayerState.stopped;
});
await audioPlayer1.stop();
setState(() {
playerState1 = PlayerState1.stopped1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:Column(
children:[
IconButton(
onPressed:() play(),
iconSize: 70.0,
icon:Icon(Icons.play_circle_outline, size: 55),
),
IconButton(
onPressed: stop(),
iconSize: 55.0,
icon: Icon(Icons.stop),
),
]
),
);
}
}
通过这种方式,您可以创建多个实例或只使用循环为每个音频创建多个实例。