我是flutter的新手...我想从暂停的地方播放音频文件,而不是从 audioplayers
flutter包的开头开始播放音频文件...
在此示例中只有: 从头开始玩 2-暂停 3停
暂停和停止它们具有相同的功能,因为暂停后我无法从暂停处播放...
所以我想要的是从我暂停的地方播放,并在同一按钮上播放!
我使用的 audioplayers
包
我的代码...
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
typedef void OnError(Exception exception);
void main() {
runApp(new MaterialApp(debugShowCheckedModeBanner: false,home: LocalAudio()));
}
class LocalAudio extends StatefulWidget {
@override
_LocalAudio createState() => _LocalAudio();
}
class _LocalAudio extends State<LocalAudio> {
Duration _duration = new Duration();
Duration _position = new Duration();
AudioPlayer advancedPlayer;
AudioCache audioCache;
@override
void initState() {
super.initState();
initPlayer();
}
void initPlayer() {
advancedPlayer = new AudioPlayer();
audioCache = new AudioCache(fixedPlayer: advancedPlayer);
advancedPlayer.durationHandler = (d) => setState(() {
_duration = d;
});
advancedPlayer.positionHandler = (p) => setState(() {
_position = p;
});
}
String localFilePath;
Widget _tab(List<Widget> children) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: children
.map((w) => Container(child: w, padding: EdgeInsets.all(6.0)))
.toList(),
),
),
],
);
}
Widget _btn(String txt, VoidCallback onPressed) {
return ButtonTheme(
minWidth: 48.0,
child: Container(
width: 150,
height: 45,
child: RaisedButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
child: Text(txt),
color: Colors.pink[900],
textColor: Colors.white,
onPressed: onPressed),
),
);
}
Widget slider() {
return Slider(
activeColor: Colors.black,
inactiveColor: Colors.pink,
value: _position.inSeconds.toDouble(),
min: 0.0,
max: _duration.inSeconds.toDouble(),
onChanged: (double value) {
setState(() {
seekToSecond(value.toInt());
value = value;
});
});
}
Widget LocalAudio() {
return _tab([
_btn('Play', () => audioCache.play('disco.mp3')),
_btn('Pause', () => advancedPlayer.pause()),
_btn('Stop', () => advancedPlayer.stop()),
slider()
]);
}
void seekToSecond(int second) {
Duration newDuration = Duration(seconds: second);
advancedPlayer.seek(newDuration);
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 1,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 1.0,
backgroundColor: Colors.teal,
title: Center(child: Text('LOCAL AUDIO')),
),
body: TabBarView(
children: [LocalAudio()],
),
),
);
}
}
答案 0 :(得分:0)
您可以使用resume方法从暂停的位置播放音频。
await audioPlayer.resume();
仅在audioPlayer状态为“暂停”时有效。
从包装文档中获取:
此外,您可以继续播放(如播放,但没有新参数):
int result = await audioPlayer.resume();
答案 1 :(得分:0)
class AudioPlayer
中有resume
的功能
在class AudioPlayer
中看起来像这样
Future<int> resume() async {
final int result = await _invokeMethod('resume');
if (result == 1) {
state = AudioPlayerState.PLAYING;
}
return result;
}
使用它添加
_btn('resume', () => advancedPlayer.resume()),
下面的行
_btn('Pause', () => advancedPlayer.pause()),