Skip to content

Commit

Permalink
Fix background play lag
Browse files Browse the repository at this point in the history
  • Loading branch information
arianneorpilla committed Jun 16, 2023
1 parent 3a51a7d commit 4cf4f91
Showing 1 changed file with 87 additions and 53 deletions.
140 changes: 87 additions & 53 deletions yuuna/lib/src/pages/implementations/player_source_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>

PlayerMediaSource get source => widget.source as PlayerMediaSource;

late final ag.MediaItem mediaItem;

/// Executed on dictionary dismiss.
@override
void onDictionaryDismiss() {
Expand Down Expand Up @@ -279,22 +277,48 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>
_currentSubtitle.value = null;
appModel.blockCreatorInitialMedia = true;

mediaItem = ag.MediaItem(
final mediaItem = ag.MediaItem(
id: widget.item!.uniqueKey,
title: widget.item!.title,
artist: widget.item!.author,
artUri: source is PlayerYoutubeSource
? Uri.parse(widget.item!.extraUrl ?? widget.item!.imageUrl ?? '')
: Uri.file(
source.getOverrideThumbnailFilename(
appModel: appModel,
item: widget.item!,
),
),
);

final playbackState = ag.PlaybackState(
controls: [
ag.MediaControl.rewind,
if (_playingNotifier.value)
ag.MediaControl.pause
else
ag.MediaControl.play,
ag.MediaControl.fastForward,
],
systemActions: const {
ag.MediaAction.seek,
ag.MediaAction.fastForward,
ag.MediaAction.rewind,
},
androidCompactActionIndices: const [1],
processingState: ag.AudioProcessingState.ready,
updatePosition: Duration(seconds: widget.item!.position),
);

if (appModel.playerBackgroundPlay) {
appModel.audioHandler?.mediaItem.add(mediaItem);
appModel.audioHandler?.playbackState.add(playbackState);

appModel.audioHandler?.mediaItem.add(mediaItem);
try {
appModel.audioHandler?.mediaItem.value?.copyWith.call(
artUri: source is PlayerYoutubeSource
? Uri.parse(widget.item!.extraUrl ?? widget.item!.imageUrl ?? '')
: Uri.file(
source.getOverrideThumbnailFilename(
appModel: appModel,
item: widget.item!,
),
),
);
} finally {}
}

_playerController.addOnInitListener(() async {
Expand Down Expand Up @@ -392,6 +416,7 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>
double _lastAspectRatio = 1;

Subtitle? _autoPauseMemory;
bool _lastPlayingState = true;

/// This is called each time the player ticks.
void listener() async {
Expand All @@ -400,54 +425,19 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>
}

if (_playerController.value.isInitialized) {
_positionNotifier.value = _playerController.value.position;
_durationNotifier.value = _playerController.value.duration;
_playingNotifier.value = _playerController.value.isPlaying;
_endedNotifier.value = _playerController.value.isEnded;

if (appModel.playerBackgroundPlay) {
if (_durationNotifier.value == Duration.zero) {
appModel.audioHandler?.mediaItem.add(
appModel.audioHandler?.mediaItem.value?.copyWith.call(
duration: _durationNotifier.value,
),
);

if (_currentSubtitle.value != null) {
appModel.audioHandler?.mediaItem.add(
appModel.audioHandler?.mediaItem.value?.copyWith.call(
artist: appModel.showSubtitlesInNotification
? _currentSubtitle.value?.data
: widget.item!.author,
),
);
}

appModel.audioHandler?.playbackState.add(
ag.PlaybackState(
controls: [
ag.MediaControl.rewind,
if (_playingNotifier.value)
ag.MediaControl.pause
else
ag.MediaControl.play,
ag.MediaControl.fastForward,
],
systemActions: const {
ag.MediaAction.seek,
ag.MediaAction.fastForward,
ag.MediaAction.rewind,
},
androidCompactActionIndices: const [1],
processingState: _bufferingNotifier.value
? ag.AudioProcessingState.buffering
: ag.AudioProcessingState.ready,
playing: _playingNotifier.value,
updatePosition: _positionNotifier.value,
bufferedPosition: _positionNotifier.value,
duration: _playerController.value.duration,
),
);
}

_positionNotifier.value = _playerController.value.position;
_durationNotifier.value = _playerController.value.duration;
_playingNotifier.value = _playerController.value.isPlaying;
_endedNotifier.value = _playerController.value.isEnded;

if (_playerController.value.aspectRatio != _lastAspectRatio) {
_lastAspectRatio = _playerController.value.aspectRatio;
setState(() {});
Expand Down Expand Up @@ -528,6 +518,17 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>
_currentSubtitle.value = newSubtitle;
// For remembering the last subtitle even if it has disappeared.
}

if (appModel.playerBackgroundPlay &&
appModel.showSubtitlesInNotification &&
!_sliderBeingDragged &&
_currentSubtitle.value != null) {
appModel.audioHandler?.mediaItem.add(
appModel.audioHandler?.mediaItem.value?.copyWith.call(
artist: _currentSubtitle.value?.data,
),
);
}
}

if (_shadowingSubtitle.value != null) {
Expand Down Expand Up @@ -569,6 +570,39 @@ class _PlayerSourcePageState extends BaseSourcePageState<PlayerSourcePage>
_positionNotifier.value != Duration.zero) {
updateHistory();
}

if (appModel.playerBackgroundPlay && !_sliderBeingDragged) {
if (_lastPlayingState != _playingNotifier.value) {
_lastPlayingState = _playingNotifier.value;

appModel.audioHandler?.playbackState.add(
appModel.audioHandler!.playbackState.value.copyWith(
controls: [
ag.MediaControl.rewind,
if (_playingNotifier.value)
ag.MediaControl.pause
else
ag.MediaControl.play,
ag.MediaControl.fastForward,
],
playing: _playingNotifier.value,
updatePosition: _positionNotifier.value,
),
);
} else {
final distance = (_positionNotifier.value.inSeconds -
appModel.audioHandler!.playbackState.value.position.inSeconds)
.abs();

if (distance > 3) {
appModel.audioHandler?.playbackState.add(
appModel.audioHandler!.playbackState.value.copyWith(
updatePosition: _positionNotifier.value,
),
);
}
}
}
}
}

Expand Down

0 comments on commit 4cf4f91

Please sign in to comment.