refactor: Do only instantiate AudioPlayer() object when in use
This commit is contained in:
parent
c24295580b
commit
ab23ba6c2c
1 changed files with 6 additions and 5 deletions
|
|
@ -29,7 +29,7 @@ enum AudioPlayerStatus { notDownloaded, downloading, downloaded }
|
||||||
|
|
||||||
class AudioPlayerState extends State<AudioPlayerWidget> {
|
class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
|
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
|
||||||
final AudioPlayer audioPlayer = AudioPlayer();
|
AudioPlayer? audioPlayer;
|
||||||
|
|
||||||
StreamSubscription? onAudioPositionChanged;
|
StreamSubscription? onAudioPositionChanged;
|
||||||
StreamSubscription? onDurationChanged;
|
StreamSubscription? onDurationChanged;
|
||||||
|
|
@ -44,8 +44,8 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
if (audioPlayer.playerState.playing) {
|
if (audioPlayer?.playerState.playing == true) {
|
||||||
audioPlayer.stop();
|
audioPlayer?.stop();
|
||||||
}
|
}
|
||||||
onAudioPositionChanged?.cancel();
|
onAudioPositionChanged?.cancel();
|
||||||
onDurationChanged?.cancel();
|
onDurationChanged?.cancel();
|
||||||
|
|
@ -76,6 +76,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _playAction() async {
|
void _playAction() async {
|
||||||
|
final audioPlayer = this.audioPlayer ??= AudioPlayer();
|
||||||
if (AudioPlayerWidget.currentId != widget.event.eventId) {
|
if (AudioPlayerWidget.currentId != widget.event.eventId) {
|
||||||
if (AudioPlayerWidget.currentId != null) {
|
if (AudioPlayerWidget.currentId != null) {
|
||||||
if (audioPlayer.playerState.playing) {
|
if (audioPlayer.playerState.playing) {
|
||||||
|
|
@ -180,7 +181,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
color: widget.color.withAlpha(64),
|
color: widget.color.withAlpha(64),
|
||||||
borderRadius: BorderRadius.circular(64),
|
borderRadius: BorderRadius.circular(64),
|
||||||
child: Icon(
|
child: Icon(
|
||||||
audioPlayer.playerState.playing
|
audioPlayer?.playerState.playing == true
|
||||||
? Icons.pause_outlined
|
? Icons.pause_outlined
|
||||||
: Icons.play_arrow_outlined,
|
: Icons.play_arrow_outlined,
|
||||||
color: widget.color,
|
color: widget.color,
|
||||||
|
|
@ -203,7 +204,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
for (var i = 0; i < AudioPlayerWidget.wavesCount; i++)
|
for (var i = 0; i < AudioPlayerWidget.wavesCount; i++)
|
||||||
Expanded(
|
Expanded(
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () => audioPlayer.seek(Duration(
|
onTap: () => audioPlayer?.seek(Duration(
|
||||||
milliseconds:
|
milliseconds:
|
||||||
(maxPosition / AudioPlayerWidget.wavesCount)
|
(maxPosition / AudioPlayerWidget.wavesCount)
|
||||||
.round() *
|
.round() *
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue