Skip to content

Commit

Permalink
Feature request: Retain volume level between application launches (#656)
Browse files Browse the repository at this point in the history
Fixes #655
  • Loading branch information
Feichtmeier authored Apr 15, 2024
1 parent c0f9960 commit 06ac7ff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/src/player/player_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PlayerModel extends SafeChangeNotifier {
bool get shuffle => service.shuffle;
void setShuffle(bool value) => service.setShuffle(value);

double get volume => service.volume;
double? get volume => service.volume;
Future<void> setVolume(double value) async => await service.setVolume(value);

double get rate => service.rate;
Expand Down
11 changes: 6 additions & 5 deletions lib/src/player/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ class PlayerService {

final _volumeController = StreamController<bool>.broadcast();
Stream<bool> get volumeChanged => _volumeController.stream;
double _volume = 100.0;
double get volume => _volume;
double? _volume;
double? get volume => _volume;
Future<void> setVolume(double value) async {
if (value == _volume) return;
await _player.setVolume(value);
Expand Down Expand Up @@ -209,11 +209,12 @@ class PlayerService {
_player.state.tracks;
});
if (newPosition != null && _audio!.audioType != AudioType.radio) {
final tempVol = _volume;
_player.setVolume(0).then(
(_) => Future.delayed(const Duration(seconds: 3)).then(
(_) => _player
.seek(newPosition)
.then((_) => _player.setVolume(100.0)),
.then((_) => _player.setVolume(tempVol ?? 100.0)),
),
);
}
Expand Down Expand Up @@ -247,8 +248,6 @@ class PlayerService {
Future<void> init() async {
await _initMediaControl();

await _readPlayerState();

_isPlayingSub = _player.stream.playing.listen((value) {
setIsPlaying(value);
});
Expand Down Expand Up @@ -294,6 +293,8 @@ class PlayerService {
}
}
});

await _readPlayerState();
}

Future<void> playNext() async {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/player/volume_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class VolumeSliderPopup extends StatelessWidget with WatchItMixin {
final volume = watchPropertyValue((PlayerModel m) => m.volume);
final setVolume = playerModel.setVolume;
IconData iconData;
if (volume <= 0) {
if (volume != null && volume <= 0) {
iconData = Iconz().speakerMutedFilled;
} else if (volume <= 20) {
} else if (volume != null && volume <= 20) {
iconData = Iconz().speakerLowFilled;
} else if (volume <= 50 && volume > 20) {
} else if (volume != null && volume <= 50 && volume > 20) {
iconData = Iconz().speakerMediumFilled;
} else {
iconData = Iconz().speakerHighFilled;
Expand Down Expand Up @@ -66,7 +66,7 @@ class _Slider extends StatelessWidget with WatchItMixin {
return RotatedBox(
quarterTurns: 3,
child: Slider(
value: volume,
value: volume ?? 100.0,
onChanged: setVolume,
max: 100,
min: 0,
Expand Down

0 comments on commit 06ac7ff

Please sign in to comment.