downloads and offline playback

This commit is contained in:
Dr-Blank 2024-08-20 08:36:39 -04:00
parent 1c95d1e4bb
commit c24541f1cd
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
38 changed files with 1590 additions and 109 deletions

View file

@ -3,11 +3,14 @@
/// this is needed as audiobook can be a list of audio files instead of a single file
library;
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:just_audio/just_audio.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:logging/logging.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
final _logger = Logger('AudiobookPlayer');
/// returns the sum of the duration of all the previous tracks before the [index]
Duration sumOfTracks(BookExpanded book, int? index) {
// return 0 if index is less than 0
@ -54,7 +57,7 @@ class AudiobookPlayer extends AudioPlayer {
/// the [BookExpanded] being played
///
/// to set the book, use [setSourceAudioBook]
/// to set the book, use [setSourceAudiobook]
BookExpanded? get book => _book;
/// the authentication token to access the [AudioTrack.contentUrl]
@ -70,21 +73,24 @@ class AudiobookPlayer extends AudioPlayer {
int? get availableTracks => _book?.tracks.length;
/// sets the current [AudioTrack] as the source of the player
Future<void> setSourceAudioBook(
Future<void> setSourceAudiobook(
BookExpanded? book, {
bool preload = true,
// int? initialIndex,
Duration? initialPosition,
List<Uri>? downloadedUris,
Uri? artworkUri,
}) async {
// if the book is null, stop the player
if (book == null) {
_book = null;
_logger.info('Book is null, stopping player');
return stop();
}
// see if the book is the same as the current book
if (_book == book) {
// if the book is the same, do nothing
_logger.info('Book is the same, doing nothing');
return;
}
// first stop the player and clear the source
@ -111,23 +117,29 @@ class AudiobookPlayer extends AudioPlayer {
ConcatenatingAudioSource(
useLazyPreparation: true,
children: book.tracks.map((track) {
final retrievedUri =
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token);
_logger.fine(
'Setting source for track: ${track.title}, URI: $retrievedUri',
);
return AudioSource.uri(
Uri.parse('$baseUrl${track.contentUrl}?token=$token'),
retrievedUri,
tag: MediaItem(
// Specify a unique ID for each media item:
id: book.libraryItemId + track.index.toString(),
// Metadata to display in the notification:
album: book.metadata.title,
title: book.metadata.title ?? track.title,
artUri: Uri.parse(
'$baseUrl/api/items/${book.libraryItemId}/cover?token=$token&width=800',
),
artUri: artworkUri ??
Uri.parse(
'$baseUrl/api/items/${book.libraryItemId}/cover?token=$token&width=800',
),
),
);
}).toList(),
),
).catchError((error) {
debugPrint('AudiobookPlayer Error: $error');
_logger.shout('Error: $error');
});
}
@ -176,7 +188,8 @@ class AudiobookPlayer extends AudioPlayer {
if (_book == null) {
return Duration.zero;
}
return bufferedPosition + _book!.tracks[sequenceState!.currentIndex].startOffset;
return bufferedPosition +
_book!.tracks[sequenceState!.currentIndex].startOffset;
}
/// streams to override to suit the book instead of the current track
@ -237,3 +250,20 @@ class AudiobookPlayer extends AudioPlayer {
);
}
}
Uri _getUri(
AudioTrack track,
List<Uri>? downloadedUris, {
required Uri baseUrl,
required String token,
}) {
// check if the track is in the downloadedUris
final uri = downloadedUris?.firstWhereOrNull(
(element) {
return element.pathSegments.last == track.metadata?.filename;
},
);
return uri ??
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
}

View file

@ -1,16 +1,16 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:whispering_pages/api/api_provider.dart';
import 'package:whispering_pages/features/player/core/audiobook_player.dart'
as abp;
as core;
part 'audiobook_player.g.dart';
// @Riverpod(keepAlive: true)
// abp.AudiobookPlayer audiobookPlayer(
// core.AudiobookPlayer audiobookPlayer(
// AudiobookPlayerRef ref,
// ) {
// final api = ref.watch(authenticatedApiProvider);
// final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
// final player = core.AudiobookPlayer(api.token!, api.baseUrl);
// ref.onDispose(player.dispose);
@ -24,9 +24,12 @@ const playerId = 'audiobook_player';
@Riverpod(keepAlive: true)
class SimpleAudiobookPlayer extends _$SimpleAudiobookPlayer {
@override
abp.AudiobookPlayer build() {
core.AudiobookPlayer build() {
final api = ref.watch(authenticatedApiProvider);
final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
final player = core.AudiobookPlayer(
api.token!,
api.baseUrl,
);
ref.onDispose(player.dispose);
@ -37,7 +40,7 @@ class SimpleAudiobookPlayer extends _$SimpleAudiobookPlayer {
@Riverpod(keepAlive: true)
class AudiobookPlayer extends _$AudiobookPlayer {
@override
abp.AudiobookPlayer build() {
core.AudiobookPlayer build() {
final player = ref.watch(simpleAudiobookPlayerProvider);
ref.onDispose(player.dispose);

View file

@ -7,7 +7,7 @@ part of 'audiobook_player.dart';
// **************************************************************************
String _$simpleAudiobookPlayerHash() =>
r'b65e6d779476a2c1fa38f617771bf997acb4f5b8';
r'9e11ed2791d35e308f8cbe61a79a45cf51466ebb';
/// Simple because it doesn't rebuild when the player state changes
/// it only rebuilds when the token changes
@ -15,7 +15,7 @@ String _$simpleAudiobookPlayerHash() =>
/// Copied from [SimpleAudiobookPlayer].
@ProviderFor(SimpleAudiobookPlayer)
final simpleAudiobookPlayerProvider =
NotifierProvider<SimpleAudiobookPlayer, abp.AudiobookPlayer>.internal(
NotifierProvider<SimpleAudiobookPlayer, core.AudiobookPlayer>.internal(
SimpleAudiobookPlayer.new,
name: r'simpleAudiobookPlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@ -25,13 +25,13 @@ final simpleAudiobookPlayerProvider =
allTransitiveDependencies: null,
);
typedef _$SimpleAudiobookPlayer = Notifier<abp.AudiobookPlayer>;
String _$audiobookPlayerHash() => r'38042d0c93034e6907677fdb614a9af1b9d636af';
typedef _$SimpleAudiobookPlayer = Notifier<core.AudiobookPlayer>;
String _$audiobookPlayerHash() => r'44394b1dbbf85eb19ef1f693717e8cbc15b768e5';
/// See also [AudiobookPlayer].
@ProviderFor(AudiobookPlayer)
final audiobookPlayerProvider =
NotifierProvider<AudiobookPlayer, abp.AudiobookPlayer>.internal(
NotifierProvider<AudiobookPlayer, core.AudiobookPlayer>.internal(
AudiobookPlayer.new,
name: r'audiobookPlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@ -41,6 +41,6 @@ final audiobookPlayerProvider =
allTransitiveDependencies: null,
);
typedef _$AudiobookPlayer = Notifier<abp.AudiobookPlayer>;
typedef _$AudiobookPlayer = Notifier<core.AudiobookPlayer>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

View file

@ -9,6 +9,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'player_form.g.dart';
/// The height of the player when it is minimized
const double playerMinHeight = 70;
// const miniplayerPercentageDeclaration = 0.2;

View file

@ -98,7 +98,7 @@ class AudiobookPlayer extends HookConsumerWidget {
// add a delay before closing the player
// to allow the user to see the player closing
Future.delayed(const Duration(milliseconds: 300), () {
player.setSourceAudioBook(null);
player.setSourceAudiobook(null);
});
},
curve: Curves.easeOut,

View file

@ -17,6 +17,8 @@ import 'widgets/audiobook_player_seek_button.dart';
import 'widgets/audiobook_player_seek_chapter_button.dart';
import 'widgets/player_speed_adjust_button.dart';
var pendingPlayerModals = 0;
class PlayerWhenExpanded extends HookConsumerWidget {
const PlayerWhenExpanded({
super.key,
@ -270,6 +272,7 @@ class SleepTimerButton extends HookConsumerWidget {
message: 'Sleep Timer',
child: InkWell(
onTap: () async {
pendingPlayerModals++;
// show the sleep timer dialog
final resultingDuration = await showDurationPicker(
context: context,
@ -279,6 +282,7 @@ class SleepTimerButton extends HookConsumerWidget {
.sleepTimerSettings
.defaultDuration,
);
pendingPlayerModals--;
if (resultingDuration != null) {
// if 0 is selected, cancel the timer
if (resultingDuration.inSeconds == 0) {

View file

@ -1,8 +1,12 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:whispering_pages/features/player/providers/audiobook_player.dart';
import 'package:whispering_pages/features/player/view/player_when_expanded.dart';
import 'package:whispering_pages/features/player/view/widgets/speed_selector.dart';
final _logger = Logger('PlayerSpeedAdjustButton');
class PlayerSpeedAdjustButton extends HookConsumerWidget {
const PlayerSpeedAdjustButton({
super.key,
@ -14,8 +18,10 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
final notifier = ref.watch(audiobookPlayerProvider.notifier);
return TextButton(
child: Text('${player.speed}x'),
onPressed: () {
showModalBottomSheet(
onPressed: () async {
pendingPlayerModals++;
_logger.fine('opening speed selector');
await showModalBottomSheet<bool>(
context: context,
barrierLabel: 'Select Speed',
constraints: const BoxConstraints(
@ -29,6 +35,8 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
);
},
);
pendingPlayerModals--;
_logger.fine('Closing speed selector');
},
);
}