diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index bfa7fe2..92d03ae 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -8,7 +8,7 @@
-
+
diff --git a/lib/home/audioplayer.dart b/lib/home/audioplayer.dart
index 07b867d..8f57041 100644
--- a/lib/home/audioplayer.dart
+++ b/lib/home/audioplayer.dart
@@ -303,9 +303,8 @@ class LastPosition extends StatelessWidget {
.onSurface
.withOpacity(0.12))),
textColor: data ? Colors.white : null,
- onPressed: () => {}
- //audio.setSkipSilence(skipSilence: !data)
- )),
+ onPressed: () =>
+ audio.setSkipSilence(skipSilence: !data))),
SizedBox(width: 10),
Selector(
selector: (_, audio) => audio.boostVolume,
@@ -329,9 +328,8 @@ class LastPosition extends StatelessWidget {
.onSurface
.withOpacity(0.12))),
textColor: data ? Colors.white : null,
- onPressed: () => {}
- // audio.setBoostVolume(boostVolume: !data)
- )),
+ onPressed: () =>
+ audio.setBoostVolume(boostVolume: !data))),
SizedBox(width: 10),
FutureBuilder(
future: getPosition(episode),
diff --git a/lib/state/audio_state.dart b/lib/state/audio_state.dart
index 9e6d845..5d98695 100644
--- a/lib/state/audio_state.dart
+++ b/lib/state/audio_state.dart
@@ -75,6 +75,7 @@ class AudioPlayerNotifier extends ChangeNotifier {
KeyValueStorage(markListenedAfterSkipKey);
final _playlistsStorgae = KeyValueStorage(playlistsAllKey);
final _playerStateStorage = KeyValueStorage(playerStateKey);
+ final cacheStorage = KeyValueStorage(cacheMaxKey);
/// Playing episdoe.
EpisodeBrief _episode;
@@ -175,13 +176,18 @@ class AudioPlayerNotifier extends ChangeNotifier {
List _playFromSearchList = [];
AudioHandler _audioHandler;
- AudioSession _session;
+
+ StreamSubscription _mediaitemSubscription;
+ StreamSubscription _playbackStateSubscription;
+ StreamSubscription _customEventSubscription;
@override
void addListener(VoidCallback listener) async {
await _initAudioData();
- final _audioHandler = await AudioService.init(
- builder: () => CustomAudioHandler(),
+ final cacheMax =
+ await cacheStorage.getInt(defaultValue: (1024 * 1024 * 200).toInt());
+ _audioHandler = await AudioService.init(
+ builder: () => CustomAudioHandler(cacheMax),
config: AudioServiceConfig(
androidNotificationChannelName: 'Tsacdop',
androidNotificationIcon: 'drawable/ic_notification',
@@ -194,6 +200,14 @@ class AudioPlayerNotifier extends ChangeNotifier {
super.addListener(listener);
}
+ @override
+ void dispose() {
+ _mediaitemSubscription?.cancel();
+ _playbackStateSubscription?.cancel();
+ _customEventSubscription?.cancel();
+ super.dispose();
+ }
+
/// Audio playing state.
AudioProcessingState get audioState => _audioState;
int get backgroundAudioDuration => _backgroundAudioDuration;
@@ -482,18 +496,20 @@ class AudioPlayerNotifier extends ChangeNotifier {
/// Set slipsilence.
if (_skipSilence) {
- // await _audioHandler.customAction('setSkipSilence', {'': skipSilence});
+ await _audioHandler
+ .customAction('setSkipSilence', {'skipSilence': skipSilence});
}
/// Set boostValome.
- // if (_boostVolume) {
- // await _audioHandler.customAction(
- // 'setBoostVolume', [_boostVolume, _volumeGain]);
- // }
+ if (_boostVolume) {
+ await _audioHandler.customAction(
+ 'setBoostVolume', {'boostVolume': _boostVolume, 'gain': _volumeGain});
+ }
_audioHandler.play();
- _audioHandler.mediaItem.where((event) => event != null).listen(
+ _mediaitemSubscription =
+ _audioHandler.mediaItem.where((event) => event != null).listen(
(item) async {
var episode = await _dbHelper.getRssItemWithMediaId(item.id);
if (episode == null) {
@@ -502,7 +518,7 @@ class AudioPlayerNotifier extends ChangeNotifier {
}
if (episode != null) {
_episode = episode;
- _backgroundAudioDuration = item.duration.inMilliseconds ?? 0;
+ _backgroundAudioDuration = item.duration?.inMilliseconds ?? 0;
if (position > 0 &&
_backgroundAudioDuration > 0 &&
_episode.enclosureUrl == _playlist.episodeList[index]) {
@@ -516,10 +532,9 @@ class AudioPlayerNotifier extends ChangeNotifier {
},
);
- _audioHandler.playbackState
+ _playbackStateSubscription = _audioHandler.playbackState
.where((event) => event != null)
.listen((event) async {
- print(event.toString());
_current = DateTime.now();
_audioState = event.processingState;
_playing = event?.playing;
@@ -541,11 +556,12 @@ class AudioPlayerNotifier extends ChangeNotifier {
notifyListeners();
});
- _audioHandler.customEvent.distinct().listen((event) async {
- if (event is String) {
+ _customEventSubscription =
+ _audioHandler.customEvent.distinct().listen((event) async {
+ if (event is Map && event['removePlayed'] != null) {
if (_playlist.isQueue &&
_queue.isNotEmpty &&
- _queue.episodes.first.title == event) {
+ _queue.episodes.first.title == event['removePlayed']) {
_queue.delFromPlaylist(_episode);
updatePlaylist(_queue, updateEpisodes: false);
}
@@ -574,10 +590,6 @@ class AudioPlayerNotifier extends ChangeNotifier {
}
//_episode = null;
}
- if (event is Map && event['duration'] != null) {
- _backgroundAudioDuration = event['duration'].inMilliseconds;
- notifyListeners();
- }
if (event is Map && event['position'] != null) {
_backgroundAudioPosition = event['position'].inMilliseconds;
if (_backgroundAudioDuration != null &&
@@ -591,43 +603,6 @@ class AudioPlayerNotifier extends ChangeNotifier {
notifyListeners();
}
});
-
- //double s = _currentSpeed ?? 1.0;
- // var getPosition = 0;
- // Timer.periodic(Duration(milliseconds: 500), (timer) {
- // var s = _currentSpeed ?? 1.0;
- // if (_noSlide) {
- // if (_playing && !buffering) {
- // getPosition = _currentPosition +
- // ((DateTime.now().difference(_current).inMilliseconds) * s)
- // .toInt();
- // _backgroundAudioPosition =
- // math.min(getPosition, _backgroundAudioDuration);
- // } else {
- // _backgroundAudioPosition = _currentPosition ?? 0;
- // }
-
- // if (_backgroundAudioDuration != null &&
- // _backgroundAudioDuration != 0 &&
- // _backgroundAudioPosition != null) {
- // _seekSliderValue =
- // _backgroundAudioPosition / _backgroundAudioDuration ?? 0;
- // } else {
- // _seekSliderValue = 0;
- // }
-
- // if (_backgroundAudioPosition > 0 &&
- // _backgroundAudioPosition < _backgroundAudioDuration) {
- // _lastPosition = _backgroundAudioPosition;
- // _playerStateStorage.savePlayerState(
- // _playlist.id, _episode.enclosureUrl, _lastPosition);
- // }
- // notifyListeners();
- // }
- // if (_audioState == AudioProcessingState.completed) {
- // timer.cancel();
- // }
- // });
}
/// Queue management.
@@ -904,30 +879,31 @@ class AudioPlayerNotifier extends ChangeNotifier {
notifyListeners();
}
- /// Set skip silence.
- // Future setSkipSilence({@required bool skipSilence}) async {
- // await AudioService.customAction('setSkipSilence', skipSilence);
- // _skipSilence = skipSilence;
- // await _skipSilenceStorage.saveBool(_skipSilence);
- // notifyListeners();
- // }
+ // Set skip silence.
+ Future setSkipSilence({@required bool skipSilence}) async {
+ await _audioHandler
+ .customAction('setSkipSilence', {'skipSilence': skipSilence});
+ _skipSilence = skipSilence;
+ await _skipSilenceStorage.saveBool(_skipSilence);
+ notifyListeners();
+ }
set setVolumeGain(int volumeGain) {
_volumeGain = volumeGain;
if (_playerRunning && _boostVolume) {
- // setBoostVolume(boostVolume: _boostVolume, gain: _volumeGain);
+ setBoostVolume(boostVolume: _boostVolume, gain: _volumeGain);
}
notifyListeners();
_volumeGainStorage.saveInt(volumeGain);
}
- // Future setBoostVolume({@required bool boostVolume, int gain}) async {
- // await _audioHandler.customAction(
- // 'setBoostVolume', [boostVolume, _volumeGain]);
- // _boostVolume = boostVolume;
- // notifyListeners();
- // await _boostVolumeStorage.saveBool(boostVolume);
- // }
+ Future setBoostVolume({@required bool boostVolume, int gain}) async {
+ await _audioHandler.customAction(
+ 'setBoostVolume', {'boostVolume': boostVolume, 'gain': _volumeGain});
+ _boostVolume = boostVolume;
+ notifyListeners();
+ await _boostVolumeStorage.saveBool(boostVolume);
+ }
//Set sleep timer
void sleepTimer(int mins) {
@@ -994,25 +970,29 @@ class CustomAudioHandler extends BaseAudioHandler
final AudioPlayer _player = AudioPlayer();
bool _interrupted = false;
int _layoutIndex;
- bool _stopAtEnd;
- int _cacheMax;
+ bool _stopAtEnd = false;
bool _isQueue = false;
bool get hasNext => queue.value.length > 0;
MediaItem get currentMediaItem => mediaItem.value;
bool get playing => playbackState.value.playing;
- // MediaItem get _currentMediaItem => hasNext ? _queue[_index] : null;
BehaviorSubject