Flutter 上的 AudioPlayers:我不能一个接一个地播放 mp3
AudioPlayers on Flutter: I can't play mp3 one behind the other
我已经卡了好几天了,一直在寻找解决方案,但我找不到。我需要使用 AudioPlayers 一个接一个地播放 mp3 文件,这对我来说是不可能的。我正在使用唯一的工具(据我所知)为您提供执行此类操作的库,但我找不到如何执行此操作。感谢您的帮助。
playLocal(List list) async {
list.forEach((f) async {
final file = new File('${(await getTemporaryDirectory()).path}/voice.mp3');
await file.writeAsBytes((await loadAsset(f)).buffer.asUint8List());
bool finish = false;
if (player.state == null || player.state == AudioPlayerState.COMPLETED) {
await player.play(file.path, isLocal:true);
}
if (player.state != null && player.state != AudioPlayerState.COMPLETED) {
while(!finish){
player.onPlayerCompletion.listen((event){
finish = true;
});
if (finish) {await player.play(file.path, isLocal:true);}
}
}
});
}
您可以使用包 https://pub.dev/packages/assets_audio_player
在 assetsAudioPlayer.finished.listen
调用 _next()
代码片段
void _next() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistNext();
} else {
_currentAssetPosition++;
_open(_currentAssetPosition);
}
}
@override
void initState() {
super.initState();
_assetsAudioPlayer.finished.listen((finished) {
print("paly next");
_next();
});
}
在 song1.mp3
完成自动播放后工作演示 song2.mp3
完整代码
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:assets_audio_player_example/asset_audio_player_icons.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final assets = <String>[
"assets/audios/song1.mp3",
"assets/audios/song2.mp3",
"assets/audios/song3.mp3",
];
final AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();
var _currentAssetPosition = -1;
void _open(int assetIndex) {
_currentAssetPosition = assetIndex % assets.length;
_assetsAudioPlayer.open(assets[_currentAssetPosition]);
}
void _playPause() {
_assetsAudioPlayer.playOrPause();
}
void _next() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistNext();
} else {
_currentAssetPosition++;
_open(_currentAssetPosition);
}
}
void _prev() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistPrevious();
} else {
_currentAssetPosition--;
_open(_currentAssetPosition);
}
}
@override
void initState() {
super.initState();
_assetsAudioPlayer.finished.listen((finished) {
print("paly next");
_next();
});
}
@override
void dispose() {
_assetsAudioPlayer.stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Padding(
padding: const EdgeInsets.only(bottom: 48.0),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RaisedButton(
onPressed: () {
_assetsAudioPlayer
.openPlaylist(Playlist(assetAudioPaths: this.assets));
},
child: Text("Playlist test"),
),
Expanded(
child: StreamBuilder(
stream: _assetsAudioPlayer.current,
initialData: const PlayingAudio(),
builder: (BuildContext context,
AsyncSnapshot<PlayingAudio> snapshot) {
final PlayingAudio currentAudio = snapshot.data;
return ListView.builder(
itemBuilder: (context, position) {
return ListTile(
title: Text(assets[position].split("/").last,
style: TextStyle(
color: assets[position] ==
currentAudio.assetAudioPath
? Colors.blue
: Colors.black)),
onTap: () {
_open(position);
});
},
itemCount: assets.length,
);
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
StreamBuilder(
stream: _assetsAudioPlayer.isLooping,
initialData: false,
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
return RaisedButton(
child: Text(snapshot.data ? "Looping" : "Not looping"),
onPressed: () {
_assetsAudioPlayer.toggleLoop();
},
);
},
),
SizedBox(width: 20),
RaisedButton(
child: Text("Seek to 2:00"),
onPressed: () {
_assetsAudioPlayer.seek(Duration(minutes: 2));
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
StreamBuilder(
stream: _assetsAudioPlayer.currentPosition,
initialData: const Duration(),
builder: (BuildContext context,
AsyncSnapshot<Duration> snapshot) {
Duration duration = snapshot.data;
return Text(durationToString(duration));
},
),
Text(" - "),
StreamBuilder(
stream: _assetsAudioPlayer.current,
builder: (BuildContext context,
AsyncSnapshot<PlayingAudio> snapshot) {
Duration duration = Duration();
if (snapshot.hasData) {
duration = snapshot.data.duration;
}
return Text(durationToString(duration));
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
IconButton(
onPressed: _prev,
icon: Icon(AssetAudioPlayerIcons.to_start),
),
StreamBuilder(
stream: _assetsAudioPlayer.isPlaying,
initialData: false,
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
return IconButton(
onPressed: _playPause,
icon: Icon(snapshot.data
? AssetAudioPlayerIcons.pause
: AssetAudioPlayerIcons.play),
);
},
),
IconButton(
icon: Icon(AssetAudioPlayerIcons.to_end),
onPressed: _next,
),
],
),
],
),
),
),
);
}
}
String durationToString(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes =
twoDigits(duration.inMinutes.remainder(Duration.minutesPerHour));
String twoDigitSeconds =
twoDigits(duration.inSeconds.remainder(Duration.secondsPerMinute));
return "$twoDigitMinutes:$twoDigitSeconds";
}
我已经卡了好几天了,一直在寻找解决方案,但我找不到。我需要使用 AudioPlayers 一个接一个地播放 mp3 文件,这对我来说是不可能的。我正在使用唯一的工具(据我所知)为您提供执行此类操作的库,但我找不到如何执行此操作。感谢您的帮助。
playLocal(List list) async {
list.forEach((f) async {
final file = new File('${(await getTemporaryDirectory()).path}/voice.mp3');
await file.writeAsBytes((await loadAsset(f)).buffer.asUint8List());
bool finish = false;
if (player.state == null || player.state == AudioPlayerState.COMPLETED) {
await player.play(file.path, isLocal:true);
}
if (player.state != null && player.state != AudioPlayerState.COMPLETED) {
while(!finish){
player.onPlayerCompletion.listen((event){
finish = true;
});
if (finish) {await player.play(file.path, isLocal:true);}
}
}
});
}
您可以使用包 https://pub.dev/packages/assets_audio_player
在 assetsAudioPlayer.finished.listen
调用 _next()
代码片段
void _next() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistNext();
} else {
_currentAssetPosition++;
_open(_currentAssetPosition);
}
}
@override
void initState() {
super.initState();
_assetsAudioPlayer.finished.listen((finished) {
print("paly next");
_next();
});
}
在 song1.mp3
完成自动播放后工作演示 song2.mp3
完整代码
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:assets_audio_player_example/asset_audio_player_icons.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final assets = <String>[
"assets/audios/song1.mp3",
"assets/audios/song2.mp3",
"assets/audios/song3.mp3",
];
final AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();
var _currentAssetPosition = -1;
void _open(int assetIndex) {
_currentAssetPosition = assetIndex % assets.length;
_assetsAudioPlayer.open(assets[_currentAssetPosition]);
}
void _playPause() {
_assetsAudioPlayer.playOrPause();
}
void _next() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistNext();
} else {
_currentAssetPosition++;
_open(_currentAssetPosition);
}
}
void _prev() {
if (_assetsAudioPlayer.playlist != null) {
_assetsAudioPlayer.playlistPrevious();
} else {
_currentAssetPosition--;
_open(_currentAssetPosition);
}
}
@override
void initState() {
super.initState();
_assetsAudioPlayer.finished.listen((finished) {
print("paly next");
_next();
});
}
@override
void dispose() {
_assetsAudioPlayer.stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Padding(
padding: const EdgeInsets.only(bottom: 48.0),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RaisedButton(
onPressed: () {
_assetsAudioPlayer
.openPlaylist(Playlist(assetAudioPaths: this.assets));
},
child: Text("Playlist test"),
),
Expanded(
child: StreamBuilder(
stream: _assetsAudioPlayer.current,
initialData: const PlayingAudio(),
builder: (BuildContext context,
AsyncSnapshot<PlayingAudio> snapshot) {
final PlayingAudio currentAudio = snapshot.data;
return ListView.builder(
itemBuilder: (context, position) {
return ListTile(
title: Text(assets[position].split("/").last,
style: TextStyle(
color: assets[position] ==
currentAudio.assetAudioPath
? Colors.blue
: Colors.black)),
onTap: () {
_open(position);
});
},
itemCount: assets.length,
);
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
StreamBuilder(
stream: _assetsAudioPlayer.isLooping,
initialData: false,
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
return RaisedButton(
child: Text(snapshot.data ? "Looping" : "Not looping"),
onPressed: () {
_assetsAudioPlayer.toggleLoop();
},
);
},
),
SizedBox(width: 20),
RaisedButton(
child: Text("Seek to 2:00"),
onPressed: () {
_assetsAudioPlayer.seek(Duration(minutes: 2));
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
StreamBuilder(
stream: _assetsAudioPlayer.currentPosition,
initialData: const Duration(),
builder: (BuildContext context,
AsyncSnapshot<Duration> snapshot) {
Duration duration = snapshot.data;
return Text(durationToString(duration));
},
),
Text(" - "),
StreamBuilder(
stream: _assetsAudioPlayer.current,
builder: (BuildContext context,
AsyncSnapshot<PlayingAudio> snapshot) {
Duration duration = Duration();
if (snapshot.hasData) {
duration = snapshot.data.duration;
}
return Text(durationToString(duration));
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
IconButton(
onPressed: _prev,
icon: Icon(AssetAudioPlayerIcons.to_start),
),
StreamBuilder(
stream: _assetsAudioPlayer.isPlaying,
initialData: false,
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
return IconButton(
onPressed: _playPause,
icon: Icon(snapshot.data
? AssetAudioPlayerIcons.pause
: AssetAudioPlayerIcons.play),
);
},
),
IconButton(
icon: Icon(AssetAudioPlayerIcons.to_end),
onPressed: _next,
),
],
),
],
),
),
),
);
}
}
String durationToString(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes =
twoDigits(duration.inMinutes.remainder(Duration.minutesPerHour));
String twoDigitSeconds =
twoDigits(duration.inSeconds.remainder(Duration.secondsPerMinute));
return "$twoDigitMinutes:$twoDigitSeconds";
}