diff --git a/lib/state/audio_state.dart b/lib/state/audio_state.dart index dca5800..03bba74 100644 --- a/lib/state/audio_state.dart +++ b/lib/state/audio_state.dart @@ -444,17 +444,21 @@ class AudioPlayerNotifier extends ChangeNotifier { _lastPostion = 0; notifyListeners(); await positionStorage.saveInt(_lastPostion); - final history = PlayHistory(_episode.title, _episode.enclosureUrl, - backgroundAudioPosition ~/ 1000, seekSliderValue); - await dbHelper.saveHistory(history); + if (_lastPostion == 0) { + final history = PlayHistory(_episode.title, _episode.enclosureUrl, + _backgroundAudioPosition ~/ 1000, _seekSliderValue); + await dbHelper.saveHistory(history); + } } if (event is Map && event['playerRunning'] == false) { if (_playerRunning) { _playerRunning = false; notifyListeners(); - final history = PlayHistory(_episode.title, _episode.enclosureUrl, - _lastPostion ~/ 1000, _seekSliderValue); - await dbHelper.saveHistory(history); + if (_lastPostion > 0) { + final history = PlayHistory(_episode.title, _episode.enclosureUrl, + _lastPostion ~/ 1000, _seekSliderValue); + await dbHelper.saveHistory(history); + } } } }); @@ -496,14 +500,14 @@ class AudioPlayerNotifier extends ChangeNotifier { }); } - playNext() async { + Future playNext() async { _remoteErrorMessage = null; await AudioService.skipToNext(); _queueUpdate = !_queueUpdate; notifyListeners(); } - addToPlaylist(EpisodeBrief episode) async { + Future addToPlaylist(EpisodeBrief episode) async { var episodeNew = await dbHelper.getRssItemWithUrl(episode.enclosureUrl); if (!_queue.playlist.contains(episodeNew)) { if (playerRunning) { @@ -515,7 +519,7 @@ class AudioPlayerNotifier extends ChangeNotifier { } } - addToPlaylistAt(EpisodeBrief episode, int index) async { + Future addToPlaylistAt(EpisodeBrief episode, int index) async { var episodeNew = await dbHelper.getRssItemWithUrl(episode.enclosureUrl); if (playerRunning) { await AudioService.addQueueItemAt(episodeNew.toMediaItem(), index); @@ -525,7 +529,7 @@ class AudioPlayerNotifier extends ChangeNotifier { notifyListeners(); } - addNewEpisode(List group) async { + Future addNewEpisode(List group) async { var newEpisodes = []; if (group.first == 'All') { newEpisodes = await dbHelper.getRecentNewRssItem(); @@ -610,27 +614,27 @@ class AudioPlayerNotifier extends ChangeNotifier { _audioState != AudioProcessingState.none) AudioService.play(); } - forwardAudio(int s) { + Future forwardAudio(int s) async { var pos = _backgroundAudioPosition + s * 1000; - AudioService.seekTo(Duration(milliseconds: pos)); + await AudioService.seekTo(Duration(milliseconds: pos)); } - fastForward() async { + Future fastForward() async { await AudioService.fastForward(); } - rewind() async { + Future rewind() async { await AudioService.rewind(); } - seekTo(int position) async { + Future seekTo(int position) async { if (_audioState != AudioProcessingState.connecting && _audioState != AudioProcessingState.none) { await AudioService.seekTo(Duration(milliseconds: position)); } } - sliderSeek(double val) async { + Future sliderSeek(double val) async { if (_audioState != AudioProcessingState.connecting && _audioState != AudioProcessingState.none) { _noSlide = false; @@ -643,21 +647,21 @@ class AudioPlayerNotifier extends ChangeNotifier { } /// Set player speed. - setSpeed(double speed) async { + Future setSpeed(double speed) async { await AudioService.customAction('setSpeed', speed); _currentSpeed = speed; await speedStorage.saveDouble(_currentSpeed); notifyListeners(); } - setSkipSilence({@required bool skipSilence}) async { + Future setSkipSilence({@required bool skipSilence}) async { await AudioService.customAction('setSkipSilence', skipSilence); _skipSilence = skipSilence; await skipSilenceStorage.saveBool(_skipSilence); notifyListeners(); } - setBoostVolume({@required bool boostVolume, int gain}) async { + Future setBoostVolume({@required bool boostVolume, int gain}) async { await AudioService.customAction( 'setBoostVolume', [boostVolume, _volumeGain]); _boostVolume = boostVolume; @@ -685,7 +689,9 @@ class AudioPlayerNotifier extends ChangeNotifier { _stopOnComplete = false; _startSleepTimer = false; _switchValue = 0; - AudioService.stop(); + if (_playerRunning) { + AudioService.stop(); + } notifyListeners(); // AudioService.disconnect(); }); @@ -717,9 +723,7 @@ class AudioPlayerNotifier extends ChangeNotifier { @override void dispose() async { - // await AudioService.stop(); await AudioService.disconnect(); - //_playerRunning = false; super.dispose(); } } @@ -950,19 +954,26 @@ class AudioPlayerTask extends BackgroundAudioTask { @override Future onClick(MediaButton button) async { - if (button == MediaButton.media) { - await playPause(); - } else if (button == MediaButton.next) { - await _seekRelative(fastForwardInterval); - } else if (button == MediaButton.previous) { - await _seekRelative(-rewindInterval); + switch (button) { + case MediaButton.media: + if (AudioServiceBackground.state?.playing == true) { + await onPause(); + } else { + await onPlay(); + } + break; + case MediaButton.next: + await onFastForward(); + break; + case MediaButton.previous: + await onRewind(); + break; } } Future _seekRelative(Duration offset) async { var newPosition = _audioPlayer.playbackEvent.position + offset; - // if (newPosition < Duration.zero) newPosition = Duration.zero; - // if (newPosition > mediaItem.duration) newPosition = mediaItem.duration; + if (newPosition < Duration.zero) newPosition = Duration.zero; onSeekTo(newPosition); } @@ -1021,48 +1032,6 @@ class AudioPlayerTask extends BackgroundAudioTask { await _seekRelative(-rewindInterval); } -// @override -// Future onAudioFocusLost(AudioInterruption interruption) { -// if (_playing) _interrupted = true; -// switch (interruption) { -// case AudioInterruption.pause: -// case AudioInterruption.temporaryPause: -// case AudioInterruption.unknownPause: -// onPause(); -// break; -// case AudioInterruption.temporaryDuck: -// _audioPlayer.setVolume(0.5); -// break; -// } -// } -// -// @override -// Future onAudioBecomingNoisy() async{ -// if (_skipState == null) { -// if (_playing == null) { -// } else if (_audioPlayer.playbackEvent.state == -// AudioPlaybackState.playing) { -// _playing = false; -// _audioPlayer.pause(); -// } -// } -// } -// -// @override -// void onAudioFocusGained(AudioInterruption interruption) { -// switch (interruption) { -// case AudioInterruption.temporaryPause: -// if (!_playing && _interrupted) onPlay(); -// break; -// case AudioInterruption.temporaryDuck: -// _audioPlayer.setVolume(1.0); -// break; -// default: -// break; -// } -// _interrupted = false; -// } -// @override Future onCustomAction(funtion, argument) async { switch (funtion) { @@ -1104,7 +1073,7 @@ class AudioPlayerTask extends BackgroundAudioTask { } final index = await layoutStorage.getInt(defaultValue: 0); await AudioServiceBackground.setState( - controls: getControls(index), + controls: _getControls(index), systemActions: [ MediaAction.seekTo, MediaAction.seekForward, @@ -1119,7 +1088,7 @@ class AudioPlayerTask extends BackgroundAudioTask { ); } - List getControls(int index) { + List _getControls(int index) { switch (index) { case 0: if (_playing) { diff --git a/pubspec.yaml b/pubspec.yaml index 37cb71b..01f0615 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: flutter_localizations: sdk: flutter auto_animated: ^2.1.0 - audio_service: ^0.14.0 audio_session: ^0.0.3 cached_network_image: ^2.2.0+1 color_thief_flutter: ^1.0.2 @@ -50,6 +49,9 @@ dependencies: xml: ^4.2.0 workmanager: ^0.2.3 wc_flutter_share: ^0.2.2 + audio_service: + git: + url: https://github.com/stonega/audio_service.git just_audio: git: url: https://github.com/stonega/just_audio.git