У меня есть пример из пакета exoplayer musi c player, и когда я запустил musi c и вышел из виджета, мои musi c остановились, я попытался удалить dispose (), да, musi c играет, но я не могу остановить или приостановить ее, и когда я go вернусь, мой сеанс не сохраняется ... как я могу увидеть мой текущий сеанс (это как длительность, воспроизведение / пауза)? это пример кода:
class PlayerWidget extends StatefulWidget {
final String url;
final List<String> urls;
PlayerWidget({this.url, this.urls});
@override
State<StatefulWidget> createState() {
return _PlayerWidgetState(url, urls);
}
}
class _PlayerWidgetState extends State<PlayerWidget> {
String url;
List<String> urls;
AudioPlayer _audioPlayer;
Duration _duration;
Duration _position;
int _currentIndex = 0;
PlayerState _playerState = PlayerState.RELEASED;
StreamSubscription _durationSubscription;
StreamSubscription _positionSubscription;
StreamSubscription _playerCompleteSubscription;
StreamSubscription _playerErrorSubscription;
StreamSubscription _playerStateSubscription;
StreamSubscription _playerIndexSubscription;
StreamSubscription _playerAudioSessionIdSubscription;
StreamSubscription _notificationActionCallbackSubscription;
get _isPlaying => _playerState == PlayerState.PLAYING;
get _durationText => _duration?.toString()?.split('.')?.first ?? '';
get _positionText => _position?.toString()?.split('.')?.first ?? '';
_PlayerWidgetState(this.url, this.urls);
@override
void initState() {
super.initState();
_initAudioPlayer();
}
@override
void dispose() {
_audioPlayer.dispose();
_durationSubscription?.cancel();
_positionSubscription?.cancel();
_playerCompleteSubscription?.cancel();
_playerErrorSubscription?.cancel();
_playerStateSubscription?.cancel();
_playerIndexSubscription?.cancel();
_playerAudioSessionIdSubscription?.cancel();
_notificationActionCallbackSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(),
child: Row(
children: <Widget>[
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: _isPlaying
? () => _pause()
: () => _play(),
iconSize: 45.0,
icon: _isPlaying
? Icon(Icons.pause)
: Icon(Icons.play_arrow),
),
],
),
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_position != null
? '${_positionText ?? ''} / ${_durationText ?? ''}'
: _duration != null
? _durationText : '',
style: TextStyle(fontSize: 20.0),
),
],
),
SizedBox(
width: 400,
child: SliderTheme(
data: SliderThemeData(
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 5),
trackHeight: 3,
thumbColor: Colors.pink,
inactiveTrackColor: Colors.grey,
activeTrackColor: Colors.pink,
overlayColor: Colors.transparent,
),
child: Slider(
value:
_position != null ? _position.inMilliseconds.toDouble() : 0.0,
min: 0.0,
max:
_duration != null ? _duration.inMilliseconds.toDouble() : 0.0,
onChanged: (double value) async {
final Result result = await _audioPlayer
.seekPosition(Duration(milliseconds: value.toInt()));
if (result == Result.FAIL) {
print(
"you tried to call audio conrolling methods on released audio player :(");
} else if (result == Result.ERROR) {
print("something went wrong in seek :(");
}
_position = Duration(milliseconds: value.toInt());
},
),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 15),
child: Container(
height: 2,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black,
),
),
),
),
),
],
);
}
void _initAudioPlayer() {
_audioPlayer = AudioPlayer();
_durationSubscription = _audioPlayer.onDurationChanged.listen((duration) {
setState(() {
_duration = duration;
});
});
_positionSubscription = _audioPlayer.onAudioPositionChanged.listen((pos) {
setState(() {
_position = pos;
});
});
_playerStateSubscription =
_audioPlayer.onPlayerStateChanged.listen((playerState) {
setState(() {
_playerState = playerState;
print(_playerState);
});
});
_playerIndexSubscription =
_audioPlayer.onCurrentAudioIndexChanged.listen((index) {
setState(() {
_position = Duration(milliseconds: 0);
_currentIndex = index;
});
});
_playerAudioSessionIdSubscription =
_audioPlayer.onAudioSessionIdChange.listen((audioSessionId) {
print("audio Session Id: $audioSessionId");
});
_notificationActionCallbackSubscription = _audioPlayer
.onNotificationActionCallback
.listen((notificationActionName) {
//do something
});
_playerCompleteSubscription = _audioPlayer.onPlayerCompletion.listen((a) {
_position = Duration(milliseconds: 0);
});
}
Future<void> _play() async {
final playPosition = (_position != null &&
_duration != null &&
_position.inMilliseconds > 0 &&
_position.inMilliseconds < _duration.inMilliseconds)
? _position
: null;
await _audioPlayer.play(
url,
position: playPosition,
respectAudioFocus: false,
playerMode: PlayerMode.BACKGROUND,
);
}
Future<void> _pause() async {
final Result result = await _audioPlayer.pause();
if (result == Result.FAIL) {
print(
"you tried to call audio conrolling methods on released audio player :(");
} else if (result == Result.ERROR) {
print("something went wrong in pause :(");
}
}
}