diff --git a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java index 1cb5f6c..eca1bda 100644 --- a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java +++ b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java @@ -96,7 +96,7 @@ public class AudioPlayer implements MethodCallHandler { final long drift = position - expectedPosition; // Update if we've drifted or just started observing if (updateTime == 0L) { - broadcastPlayerState(); + broadcastPlaybackEvent(); } else if (drift < -100) { System.out.println("time discontinuity detected: " + drift); transition(PlaybackState.buffering); @@ -152,7 +152,7 @@ public class AudioPlayer implements MethodCallHandler { } } - private void broadcastPlayerState() { + private void broadcastPlaybackEvent() { final ArrayList event = new ArrayList(); event.add(state.ordinal()); event.add(updatePosition = getCurrentPosition()); @@ -179,7 +179,7 @@ public class AudioPlayer implements MethodCallHandler { if (oldState != PlaybackState.playing && newState == PlaybackState.playing) { startObservingPosition(); } - broadcastPlayerState(); + broadcastPlaybackEvent(); } private void bgTransition(final PlaybackState newState) { @@ -319,7 +319,7 @@ public class AudioPlayer implements MethodCallHandler { if (sonic != null) { sonic.setSpeed(speed); } - broadcastPlayerState(); + broadcastPlaybackEvent(); } // TODO: Test whether this times out the MediaCodec on Ogg files. diff --git a/ios/Classes/AudioPlayer.m b/ios/Classes/AudioPlayer.m index 1f39101..1d0997e 100644 --- a/ios/Classes/AudioPlayer.m +++ b/ios/Classes/AudioPlayer.m @@ -99,7 +99,7 @@ long long drift = position - expectedPosition; // Update if we've drifted or just started observing if (_updateTime == 0L) { - [self broadcastPlayerState]; + [self broadcastPlaybackEvent]; } else if (drift < -100) { NSLog(@"time discontinuity detected: %lld", drift); [self setPlaybackState:buffering]; @@ -108,7 +108,7 @@ } } -- (void)broadcastPlayerState { +- (void)broadcastPlaybackEvent { long long now = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0); _updatePosition = [self getCurrentPosition]; _updateTime = now; @@ -140,7 +140,7 @@ /* if (oldState != playing && state == playing) { */ /* [self startObservingPosition]; */ /* } */ - [self broadcastPlayerState]; + [self broadcastPlaybackEvent]; } - (void)setUrl:(NSString*)url result:(FlutterResult)result { diff --git a/lib/just_audio.dart b/lib/just_audio.dart index e0489ee..25eb037 100644 --- a/lib/just_audio.dart +++ b/lib/just_audio.dart @@ -59,13 +59,13 @@ class AudioPlayer { final _durationSubject = BehaviorSubject(); - AudioPlayerState _audioPlayerState; + AudioPlaybackEvent _audioPlaybackEvent; - Stream _eventChannelStream; + Stream _eventChannelStream; - StreamSubscription _eventChannelStreamSubscription; + StreamSubscription _eventChannelStreamSubscription; - final _playerStateSubject = BehaviorSubject(); + final _playbackEventSubject = BehaviorSubject(); final _playbackStateSubject = BehaviorSubject(); @@ -80,18 +80,18 @@ class AudioPlayer { AudioPlayer._internal(this._id) : _channel = _init(_id) { _eventChannelStream = EventChannel('com.ryanheise.just_audio.events.$_id') .receiveBroadcastStream() - .map((data) => _audioPlayerState = AudioPlayerState( + .map((data) => _audioPlaybackEvent = AudioPlaybackEvent( state: AudioPlaybackState.values[data[0]], updatePosition: Duration(milliseconds: data[1]), updateTime: Duration(milliseconds: data[2]), speed: _speed, )); _eventChannelStreamSubscription = - _eventChannelStream.listen(_playerStateSubject.add); + _eventChannelStream.listen(_playbackEventSubject.add); _playbackStateSubject - .addStream(playerStateStream.map((state) => state.state).distinct()); + .addStream(playbackEventStream.map((state) => state.state).distinct()); - playerStateStream.listen((state) { + playbackEventStream.listen((state) { print("state: $state"); }); } @@ -103,21 +103,25 @@ class AudioPlayer { /// The duration of any media set via [setUrl], [setFilePath] or [setAsset]. Stream get durationStream => _durationSubject.stream; - /// The current [AudioPlayerState]. - AudioPlayerState get playerState => _audioPlayerState; + /// The latest [AudioPlaybackEvent]. + AudioPlaybackEvent get playbackEvent => _audioPlaybackEvent; - /// The current [AudioPlayerState]. - Stream get playerStateStream => _playerStateSubject.stream; + /// A stream of [AudioPlaybackEvent]s. + Stream get playbackEventStream => + _playbackEventSubject.stream; /// The current [AudioPlaybackState]. + AudioPlaybackState get playbackState => _audioPlaybackEvent.state; + + /// A stream of [AudioPlaybackState]s. Stream get playbackStateStream => _playbackStateSubject.stream; /// A stream periodically tracking the current position of this player. Stream getPositionStream( [final Duration period = const Duration(milliseconds: 200)]) => - Rx.combineLatest2( - playerStateStream, + Rx.combineLatest2( + playbackEventStream, // TODO: emit periodically only in playing state. Stream.periodic(period), (state, _) => state.position); @@ -223,7 +227,7 @@ class AudioPlayer { await _invokeMethod('dispose'); await _durationSubject.close(); await _eventChannelStreamSubscription.cancel(); - await _playerStateSubject.close(); + await _playbackEventSubject.close(); } Future _invokeMethod(String method, [dynamic args]) async => @@ -231,7 +235,7 @@ class AudioPlayer { } /// Encapsulates the playback state and current position of the player. -class AudioPlayerState { +class AudioPlaybackEvent { /// The current playback state. final AudioPlaybackState state; @@ -245,7 +249,7 @@ class AudioPlayerState { /// The playback speed. final double speed; - AudioPlayerState({ + AudioPlaybackEvent({ @required this.state, @required this.updateTime, @required this.updatePosition, @@ -261,7 +265,8 @@ class AudioPlayerState { : updatePosition; @override - String toString() => "{state=$state, updateTime=$updateTime, updatePosition=$updatePosition, speed=$speed}"; + String toString() => + "{state=$state, updateTime=$updateTime, updatePosition=$updatePosition, speed=$speed}"; } /// Enumerates the different playback states of a player.