basic audiobook player

This commit is contained in:
Dr-Blank 2024-05-14 06:13:16 -04:00
parent 097caf8ec2
commit 610d9a2aa0
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
26 changed files with 458 additions and 110 deletions

View file

@ -0,0 +1,72 @@
/// a wrapper around the audioplayers package to manage the audio player instance
///
/// this is needed as audiobook can be a list of audio files instead of a single file
library;
import 'package:audioplayers/audioplayers.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
/// will manage the audio player instance
class AudiobookPlayer extends AudioPlayer {
// constructor which takes in the BookExpanded object
AudiobookPlayer(this.token, this.baseUrl) : super() {
// set the source of the player to the first track in the book
}
/// the [BookExpanded] being played
BookExpanded? _book;
/// the [BookExpanded] being played
///
/// to set the book, use [setSourceAudioBook]
BookExpanded? get book => _book;
/// the authentication token to access the [AudioTrack.contentUrl]
final String token;
/// the base url for the audio files
final Uri baseUrl;
// the current index of the audio file in the [book]
final int _currentIndex = 0;
/// sets the current [AudioTrack] as the source of the player
Future<void> setSourceAudioBook(BookExpanded book) async {
// see if the book is the same as the current book
if (_book == book) {
// if the book is the same, do nothing
return;
}
var track = book.tracks[_currentIndex];
var url = '$baseUrl${track.contentUrl}?token=$token';
await setSourceUrl(
url,
// '${track.contentUrl}?token=$token',
mimeType: track.mimeType,
);
_book = book;
}
/// toggles the player between play and pause
Future<void> togglePlayPause() {
// check if book is set
if (_book == null) {
throw StateError('No book is set');
}
return switch (state) {
PlayerState.playing => pause(),
PlayerState.paused ||
PlayerState.stopped ||
PlayerState.completed =>
resume(),
// do nothing if the player is disposed
PlayerState.disposed => throw StateError('Player is disposed'),
};
}
}
void main(List<String> args) {
final AudiobookPlayer abPlayer = AudiobookPlayer('', Uri.parse(''));
print(abPlayer.resume());
}

View file

@ -0,0 +1,85 @@
import 'package:shelfsdk/audiobookshelf_api.dart';
/// will manage the playlist of items
///
/// you are responsible for updating the current index and sub index
class AudiobookPlaylist {
/// list of items in the playlist
final List<BookExpanded> books;
/// current index of the item in the playlist
int _currentIndex;
/// current index of the audio file in the current item
int _subCurrentIndex;
// wrappers for adding and removing items
void add(BookExpanded item) => books.add(item);
void remove(BookExpanded item) => books.remove(item);
void clear() {
books.clear();
_currentIndex = 0;
_subCurrentIndex = 0;
}
// move an item from one index to another
void move(int from, int to) {
final item = books.removeAt(from);
books.insert(to, item);
}
/// the book being played
BookExpanded? get currentBook {
if (_currentIndex >= books.length || _currentIndex < 0 || books.isEmpty) {
return null;
}
return books[_currentIndex];
}
/// of the book in the playlist
int get currentIndex => _currentIndex;
// every time current index changes, we need to update the sub index
set currentIndex(int index) {
// if the index is the same, do nothing
if (_currentIndex == index) {
return;
}
_currentIndex = index;
subCurrentIndex = 0;
}
/// of the audio file in the current book
int get subCurrentIndex => _subCurrentIndex;
set subCurrentIndex(int index) {
if (index < 0) {
index = 0;
}
_subCurrentIndex = index;
}
AudiobookPlaylist({
this.books = const [],
currentIndex = 0,
subCurrentIndex = 0,
}) : _currentIndex = currentIndex,
_subCurrentIndex = subCurrentIndex;
// most important method, gets the audio file to play
// this is needed as a library item is a list of audio files
AudioTrack? getAudioTrack() {
final book = currentBook;
if (book == null) {
return null;
}
if (subCurrentIndex > book.tracks.length || book.tracks.isEmpty) {
return null;
}
return book.tracks[subCurrentIndex];
}
bool get isBookFinished => subCurrentIndex >= currentBook!.tracks.length;
// a method to get the next audio file and advance the sub index
}

View file

@ -0,0 +1,18 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
import 'package:whispering_pages/features/player/playlist.dart';
part 'playlist_provider.g.dart';
@riverpod
class Playlist extends _$Playlist {
@override
AudiobookPlaylist build() {
return AudiobookPlaylist();
}
void add(BookExpanded item) {
state.add(item);
ref.notifyListeners();
}
}

View file

@ -0,0 +1,25 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'playlist_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$playlistHash() => r'bed4642e4c2de829e4d0630cb5bf92bffeeb1f60';
/// See also [Playlist].
@ProviderFor(Playlist)
final playlistProvider =
AutoDisposeNotifierProvider<Playlist, AudiobookPlaylist>.internal(
Playlist.new,
name: r'playlistProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$playlistHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$Playlist = AutoDisposeNotifier<AudiobookPlaylist>;
// 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

@ -0,0 +1,34 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:whispering_pages/api/api_provider.dart';
import 'package:whispering_pages/features/player/audiobook_payer.dart' as abp;
part 'audiobook_player_provider.g.dart';
// @Riverpod(keepAlive: true)
// abp.AudiobookPlayer audiobookPlayer(
// AudiobookPlayerRef ref,
// ) {
// final api = ref.watch(authenticatedApiProvider);
// final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
// ref.onDispose(player.dispose);
// return player;
// }
@Riverpod(keepAlive: true)
class AudiobookPlayer extends _$AudiobookPlayer {
@override
abp.AudiobookPlayer build() {
final api = ref.watch(authenticatedApiProvider);
final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
ref.onDispose(player.dispose);
return player;
}
void notifyListeners() {
ref.notifyListeners();
}
}

View file

@ -0,0 +1,26 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'audiobook_player_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$audiobookPlayerHash() => r'8cbadcb264382300e63b3dbaf167a3bea1638a6e';
/// See also [AudiobookPlayer].
@ProviderFor(AudiobookPlayer)
final audiobookPlayerProvider =
NotifierProvider<AudiobookPlayer, abp.AudiobookPlayer>.internal(
AudiobookPlayer.new,
name: r'audiobookPlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$audiobookPlayerHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$AudiobookPlayer = Notifier<abp.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