diff --git a/lib/domain/entities/playback_event.dart b/lib/domain/entities/playback_event.dart index 80f6f46..44805dc 100644 --- a/lib/domain/entities/playback_event.dart +++ b/lib/domain/entities/playback_event.dart @@ -1,14 +1,14 @@ class PlaybackEvent { PlaybackEvent({ - this.duration, this.index, + this.playing, this.processingState, this.updatePosition, this.updateTime, }); - final Duration duration; final int index; + final bool playing; final ProcessingState processingState; final Duration updatePosition; final DateTime updateTime; diff --git a/lib/system/audio/audio_player_contract.dart b/lib/system/audio/audio_player_contract.dart index cfc0cad..82f6b1a 100644 --- a/lib/system/audio/audio_player_contract.dart +++ b/lib/system/audio/audio_player_contract.dart @@ -9,7 +9,7 @@ import '../models/song_model.dart'; abstract class AudioPlayer { ValueStream get currentIndexStream; ValueStream get currentSongStream; - ValueStream get playbackEventStream; + Stream get playbackEventStream; ValueStream get playingStream; ValueStream get positionStream; ValueStream> get queueStream; diff --git a/lib/system/audio/audio_player_impl.dart b/lib/system/audio/audio_player_impl.dart index de3bcc4..650dd1d 100644 --- a/lib/system/audio/audio_player_impl.dart +++ b/lib/system/audio/audio_player_impl.dart @@ -1,4 +1,5 @@ import 'package:just_audio/just_audio.dart' as ja; +import 'package:logging/logging.dart'; import 'package:rxdart/rxdart.dart'; import '../../domain/entities/loop_mode.dart'; @@ -14,16 +15,16 @@ import 'queue_generator.dart'; class AudioPlayerImpl implements AudioPlayer { AudioPlayerImpl(this._audioPlayer, this._queueGenerator) { _audioPlayer.currentIndexStream.listen((event) { + _log.info('currentIndex: $event'); _currentIndexSubject.add(event); if (_queueSubject.value != null) { _currentSongSubject.add(_queueSubject.value[event].song); } }); - _audioPlayer.playingStream.listen((event) => _playingSubject.add(event)); - - _audioPlayer.playbackEventStream.listen((event) { - _playbackEventSubject.add(PlaybackEventModel.fromJAPlaybackEvent(event)); + _audioPlayer.playingStream.listen((event) { + _log.info('playing: $event'); + _playingSubject.add(event); }); _audioPlayer.positionStream.listen((event) { @@ -31,6 +32,7 @@ class AudioPlayerImpl implements AudioPlayer { }); _audioPlayer.loopModeStream.listen((event) { + _log.info('loopMode: $event'); _loopModeSubject.add(event.toEntity()); }); @@ -39,6 +41,12 @@ class AudioPlayerImpl implements AudioPlayer { _currentSongSubject.add(event[_currentIndexSubject.value].song); } }); + + _playbackEventModelStream = Rx.combineLatest2( + _audioPlayer.playbackEventStream, + _audioPlayer.playingStream, + (a, b) => PlaybackEventModel.fromJAPlaybackEvent(a, b), + ).distinct(); } final ja.AudioPlayer _audioPlayer; @@ -48,6 +56,8 @@ class AudioPlayerImpl implements AudioPlayer { List _inputQueue; List _queue; + static final _log = Logger('AudioPlayer'); + final BehaviorSubject _currentIndexSubject = BehaviorSubject(); final BehaviorSubject _currentSongSubject = BehaviorSubject(); final BehaviorSubject _playbackEventSubject = BehaviorSubject(); @@ -57,6 +67,8 @@ class AudioPlayerImpl implements AudioPlayer { final BehaviorSubject _shuffleModeSubject = BehaviorSubject.seeded(ShuffleMode.none); final BehaviorSubject _loopModeSubject = BehaviorSubject(); + Stream _playbackEventModelStream; + @override ValueStream get currentIndexStream => _currentIndexSubject.stream; @@ -64,7 +76,7 @@ class AudioPlayerImpl implements AudioPlayer { ValueStream get currentSongStream => _currentSongSubject.stream; @override - ValueStream get playbackEventStream => _playbackEventSubject.stream; + Stream get playbackEventStream => _playbackEventModelStream; @override ValueStream get positionStream => _positionSubject.stream; diff --git a/lib/system/models/playback_event_model.dart b/lib/system/models/playback_event_model.dart index a566316..0c1e56a 100644 --- a/lib/system/models/playback_event_model.dart +++ b/lib/system/models/playback_event_model.dart @@ -4,46 +4,29 @@ import '../../domain/entities/playback_event.dart'; class PlaybackEventModel extends PlaybackEvent { PlaybackEventModel({ - Duration duration, int index, + bool playing, ProcessingState processingState, Duration updatePosition, DateTime updateTime, }) : super( - duration: duration, index: index, + playing: playing, processingState: processingState, updatePosition: updatePosition, updateTime: updateTime, ); - factory PlaybackEventModel.fromJAPlaybackEvent(ja.PlaybackEvent playbackEvent) => + factory PlaybackEventModel.fromJAPlaybackEvent(ja.PlaybackEvent playbackEvent, bool playing) => PlaybackEventModel( - duration: playbackEvent.duration, index: playbackEvent.currentIndex, + playing: playing, processingState: playbackEvent.processingState.toProcessingState(), updatePosition: playbackEvent.updatePosition, updateTime: playbackEvent.updateTime, ); } -// extension JAProcessingStateExt on ProcessingState { -// ProcessingState fromJAProcessingState(ja.ProcessingState processingState) { -// switch (processingState) { -// case ja.ProcessingState.loading: -// return ProcessingState.loading; -// case ja.ProcessingState.buffering: -// return ProcessingState.buffering; -// case ja.ProcessingState.ready: -// return ProcessingState.ready; -// case ja.ProcessingState.completed: -// return ProcessingState.completed; -// default: -// return ProcessingState.none; -// } -// } -// } - extension ProcessingStateExt on ja.ProcessingState { ProcessingState toProcessingState() { switch (this) { diff --git a/pubspec.yaml b/pubspec.yaml index 0dc0255..775d811 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: flutter_audio_query: ^0.3.5 flutter_mobx: ^1.1.0 get_it: ^5.0.0 - just_audio: ^0.6.2 + just_audio: 0.6.3 # git: # url: https://github.com/ryanheise/just_audio.git # path: just_audio