freezer/lib/api/player.dart

701 lines
21 KiB
Dart
Raw Normal View History

import 'dart:math';
2020-06-23 21:23:12 +02:00
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
2020-06-23 21:23:12 +02:00
import 'package:fluttertoast/fluttertoast.dart';
import 'package:freezer/api/cache.dart';
2020-06-23 21:23:12 +02:00
import 'package:freezer/api/deezer.dart';
import 'package:freezer/ui/android_auto.dart';
2020-06-23 21:23:12 +02:00
import 'package:just_audio/just_audio.dart';
import 'package:connectivity/connectivity.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:freezer/translations.i18n.dart';
2020-06-23 21:23:12 +02:00
import 'definitions.dart';
import '../settings.dart';
import 'dart:io';
import 'dart:async';
import 'dart:convert';
PlayerHelper playerHelper = PlayerHelper();
class PlayerHelper {
StreamSubscription _customEventSubscription;
StreamSubscription _mediaItemSubscription;
2020-06-23 21:23:12 +02:00
StreamSubscription _playbackStateStreamSubscription;
QueueSource queueSource;
LoopMode repeatType = LoopMode.off;
bool shuffle = false;
2020-06-23 21:23:12 +02:00
//Find queue index by id
int get queueIndex => AudioService.queue.indexWhere((mi) => mi.id == AudioService.currentMediaItem?.id??'Random string so it returns -1');
Future start() async {
//Subscribe to custom events
_customEventSubscription = AudioService.customEventStream.listen((event) async {
if (!(event is Map)) return;
if (event['action'] == 'onLoad') {
//After audio_service is loaded, load queue, set quality
await settings.updateAudioServiceQuality();
await AudioService.customAction('load');
return;
}
if (event['action'] == 'onRestore') {
//Load queueSource from isolate
this.queueSource = QueueSource.fromJson(event['queueSource']);
}
if (event['action'] == 'queueEnd') {
//If last song is played, load more queue
onQueueEnd();
2020-06-23 21:23:12 +02:00
this.queueSource = QueueSource.fromJson(event['queueSource']);
return;
}
//Android auto get screen
if (event['action'] == 'screenAndroidAuto') {
AndroidAuto androidAuto = AndroidAuto();
List<MediaItem> data = await androidAuto.getScreen(event['id']);
await AudioService.customAction('screenAndroidAuto', jsonEncode(data));
}
//Android auto play list
if (event['action'] == 'tracksAndroidAuto') {
AndroidAuto androidAuto = AndroidAuto();
await androidAuto.playItem(event['id']);
}
2020-06-23 21:23:12 +02:00
});
_playbackStateStreamSubscription = AudioService.playbackStateStream.listen((event) {
//Log song (if allowed)
if (event == null) return;
if (event.processingState == AudioProcessingState.ready && event.playing) {
if (settings.logListen) {
//Check if duplicate
if (cache.loggedTrackId == AudioService.currentMediaItem.id) return;
cache.loggedTrackId = AudioService.currentMediaItem.id;
deezerAPI.logListen(AudioService.currentMediaItem.id);
}
2020-06-23 21:23:12 +02:00
}
});
_mediaItemSubscription = AudioService.currentMediaItemStream.listen((event) {
//Save queue
AudioService.customAction('saveQueue');
//Add to history
if (event == null) return;
if (cache.history == null) cache.history = [];
if (cache.history.length > 0 && cache.history.last.id == event.id) return;
cache.history.add(Track.fromMediaItem(event));
cache.save();
});
2020-06-23 21:23:12 +02:00
//Start audio_service
startService();
2020-06-23 21:23:12 +02:00
}
Future startService() async {
2020-06-23 21:23:12 +02:00
if (AudioService.running) return;
await AudioService.start(
backgroundTaskEntrypoint: backgroundTaskEntrypoint,
androidEnableQueue: true,
2020-08-19 22:36:39 +02:00
androidStopForegroundOnPause: false,
2020-06-23 21:23:12 +02:00
androidNotificationOngoing: false,
androidNotificationClickStartsActivity: false,
2020-06-23 21:23:12 +02:00
androidNotificationChannelDescription: 'Freezer',
androidNotificationChannelName: 'Freezer',
androidNotificationIcon: 'drawable/ic_logo',
2020-06-23 21:23:12 +02:00
);
}
Future toggleShuffle() async {
this.shuffle = !this.shuffle;
await AudioService.customAction('shuffle', this.shuffle);
}
2020-06-23 21:23:12 +02:00
//Repeat toggle
Future changeRepeat() async {
//Change to next repeat type
switch (repeatType) {
case LoopMode.one:
repeatType = LoopMode.off; break;
case LoopMode.all:
repeatType = LoopMode.one; break;
2020-06-23 21:23:12 +02:00
default:
repeatType = LoopMode.all; break;
2020-06-23 21:23:12 +02:00
}
//Set repeat type
await AudioService.customAction("repeatType", LoopMode.values.indexOf(repeatType));
2020-06-23 21:23:12 +02:00
}
//Executed before exit
Future onExit() async {
_customEventSubscription.cancel();
_playbackStateStreamSubscription.cancel();
_mediaItemSubscription.cancel();
2020-06-23 21:23:12 +02:00
}
//Replace queue, play specified track id
Future _loadQueuePlay(List<MediaItem> queue, String trackId) async {
await startService();
2020-06-23 21:23:12 +02:00
await settings.updateAudioServiceQuality();
await AudioService.updateQueue(queue);
await AudioService.skipToQueueItem(trackId);
2020-06-23 21:23:12 +02:00
}
//Called when queue ends to load more tracks
Future onQueueEnd() async {
//Flow
if (queueSource == null) return;
if (queueSource.id == 'flow') {
List<Track> tracks = await deezerAPI.flow();
List<MediaItem> mi = tracks.map<MediaItem>((t) => t.toMediaItem()).toList();
await AudioService.addQueueItems(mi);
AudioService.skipToNext();
return;
}
//SmartRadio/Artist radio
if (queueSource.source == 'smartradio') {
List<Track> tracks = await deezerAPI.smartRadio(queueSource.id);
List<MediaItem> mi = tracks.map<MediaItem>((t) => t.toMediaItem()).toList();
await AudioService.addQueueItems(mi);
AudioService.skipToNext();
return;
}
print(queueSource.toJson());
}
2020-06-23 21:23:12 +02:00
//Play track from album
Future playFromAlbum(Album album, String trackId) async {
await playFromTrackList(album.tracks, trackId, QueueSource(
id: album.id,
text: album.title,
source: 'album'
));
}
//Play from artist top tracks
Future playFromTopTracks(List<Track> tracks, String trackId, Artist artist) async {
await playFromTrackList(tracks, trackId, QueueSource(
id: artist.id,
text: 'Top ${artist.name}',
source: 'topTracks'
));
}
Future playFromPlaylist(Playlist playlist, String trackId) async {
await playFromTrackList(playlist.tracks, trackId, QueueSource(
id: playlist.id,
text: playlist.title,
source: 'playlist'
));
}
//Load tracks as queue, play track id, set queue source
Future playFromTrackList(List<Track> tracks, String trackId, QueueSource queueSource) async {
await startService();
2020-06-23 21:23:12 +02:00
List<MediaItem> queue = tracks.map<MediaItem>((track) => track.toMediaItem()).toList();
await setQueueSource(queueSource);
await _loadQueuePlay(queue, trackId);
}
//Load smart track list as queue, start from beginning
Future playFromSmartTrackList(SmartTrackList stl) async {
//Load from API if no tracks
if (stl.tracks == null || stl.tracks.length == 0) {
if (settings.offlineMode) {
Fluttertoast.showToast(
msg: "Offline mode, can't play flow or smart track lists.".i18n,
2020-06-23 21:23:12 +02:00
gravity: ToastGravity.BOTTOM,
toastLength: Toast.LENGTH_SHORT
);
return;
}
//Flow songs cannot be accessed by smart track list call
if (stl.id == 'flow') {
stl.tracks = await deezerAPI.flow();
} else {
stl = await deezerAPI.smartTrackList(stl.id);
}
}
QueueSource queueSource = QueueSource(
id: stl.id,
source: (stl.id == 'flow')?'flow':'smarttracklist',
text: stl.title
);
await playFromTrackList(stl.tracks, stl.tracks[0].id, queueSource);
}
Future setQueueSource(QueueSource queueSource) async {
await startService();
2020-06-23 21:23:12 +02:00
this.queueSource = queueSource;
await AudioService.customAction('queueSource', queueSource.toJson());
}
}
void backgroundTaskEntrypoint() async {
AudioServiceBackground.run(() => AudioPlayerTask());
}
class AudioPlayerTask extends BackgroundAudioTask {
AudioPlayer _player = AudioPlayer();
2020-06-23 21:23:12 +02:00
//Queue
2020-06-23 21:23:12 +02:00
List<MediaItem> _queue = <MediaItem>[];
int _queueIndex = 0;
ConcatenatingAudioSource _audioSource;
2020-06-23 21:23:12 +02:00
AudioProcessingState _skipState;
Seeker _seeker;
2020-06-23 21:23:12 +02:00
//Stream subscriptions
2020-06-23 21:23:12 +02:00
StreamSubscription _eventSub;
//Loaded from file/frontend
int mobileQuality;
int wifiQuality;
2020-06-23 21:23:12 +02:00
QueueSource queueSource;
Duration _lastPosition;
2020-06-23 21:23:12 +02:00
Completer _androidAutoCallback;
2020-06-23 21:23:12 +02:00
MediaItem get mediaItem => _queue[_queueIndex];
@override
Future onStart(Map<String, dynamic> params) async {
final session = await AudioSession.instance;
session.configure(AudioSessionConfiguration.music());
2020-06-23 21:23:12 +02:00
//Update track index
_player.currentIndexStream.listen((index) {
if (index != null) {
_queueIndex = index;
AudioServiceBackground.setMediaItem(mediaItem);
2020-06-23 21:23:12 +02:00
}
});
//Update state on all clients on change
_eventSub = _player.playbackEventStream.listen((event) {
//Quality string
if (_queueIndex != -1 && _queueIndex < _queue.length) {
Map extras = mediaItem.extras;
extras['qualityString'] = event.qualityString??'';
_queue[_queueIndex] = mediaItem.copyWith(extras: extras);
}
//Update
_broadcastState();
});
_player.processingStateStream.listen((state) {
switch(state) {
case ProcessingState.completed:
//Player ended, get more songs
AudioServiceBackground.sendCustomEvent({
'action': 'queueEnd',
'queueSource': (queueSource??QueueSource()).toJson()
});
break;
case ProcessingState.ready:
//Ready to play
_skipState = null;
break;
default:
break;
}
});
2020-06-23 21:23:12 +02:00
//Load queue
2020-06-23 21:23:12 +02:00
AudioServiceBackground.setQueue(_queue);
AudioServiceBackground.sendCustomEvent({'action': 'onLoad'});
}
@override
Future onSkipToQueueItem(String mediaId) async {
_lastPosition = null;
2020-06-23 21:23:12 +02:00
//Calculate new index
final newIndex = _queue.indexWhere((i) => i.id == mediaId);
if (newIndex == -1) return;
2020-06-23 21:23:12 +02:00
//Update buffering state
_skipState = newIndex > _queueIndex
? AudioProcessingState.skippingToNext
: AudioProcessingState.skippingToPrevious;
2020-06-23 21:23:12 +02:00
//Skip in player
await _player.seek(Duration.zero, index: newIndex);
_queueIndex = newIndex;
_skipState = null;
onPlay();
2020-06-23 21:23:12 +02:00
}
@override
Future onPlay() {
_player.play();
//Restore position on play
if (_lastPosition != null) {
onSeekTo(_lastPosition);
}
2020-06-23 21:23:12 +02:00
}
@override
Future onPause() => _player.pause();
2020-06-23 21:23:12 +02:00
@override
Future onSeekTo(Duration pos) => _player.seek(pos);
2020-06-23 21:23:12 +02:00
@override
Future<void> onFastForward() => _seekRelative(fastForwardInterval);
2020-06-23 21:23:12 +02:00
@override
Future<void> onRewind() => _seekRelative(-rewindInterval);
2020-06-23 21:23:12 +02:00
@override
Future<void> onSeekForward(bool begin) async => _seekContinuously(begin, 1);
2020-06-23 21:23:12 +02:00
@override
Future<void> onSeekBackward(bool begin) async => _seekContinuously(begin, -1);
2020-06-23 21:23:12 +02:00
@override
Future<void> onSkipToNext() async {
//Shuffle
if (_player.shuffleModeEnabled??false) {
int newIndex = Random().nextInt(_queue.length)-1;
//Update state
_skipState = newIndex > _queueIndex
? AudioProcessingState.skippingToNext
: AudioProcessingState.skippingToPrevious;
_queueIndex = newIndex;
await _player.seek(Duration.zero, index: _queueIndex);
_skipState = null;
return;
}
//Update buffering state
_skipState = AudioProcessingState.skippingToNext;
_queueIndex++;
await _player.seekToNext();
_skipState = null;
await _broadcastState();
}
@override
Future<void> onSkipToPrevious() async {
if (_queueIndex == 0) return;
//Update buffering state
_skipState = AudioProcessingState.skippingToPrevious;
_queueIndex--;
await _player.seekToPrevious();
_skipState = null;
}
@override
Future<List<MediaItem>> onLoadChildren(String parentMediaId) async {
AudioServiceBackground.sendCustomEvent({
'action': 'screenAndroidAuto',
'id': parentMediaId
});
//Wait for data from main thread
_androidAutoCallback = Completer();
List<MediaItem> data = (await _androidAutoCallback.future) as List<MediaItem>;
_androidAutoCallback = null;
return data;
}
//While seeking, jump 10s every 1s
void _seekContinuously(bool begin, int direction) {
_seeker?.stop();
if (begin) {
_seeker = Seeker(_player, Duration(seconds: 10 * direction), Duration(seconds: 1), mediaItem)..start();
}
2020-06-23 21:23:12 +02:00
}
//Relative seek
2020-06-23 21:23:12 +02:00
Future _seekRelative(Duration offset) async {
Duration newPos = _player.position + offset;
//Out of bounds check
2020-06-23 21:23:12 +02:00
if (newPos < Duration.zero) newPos = Duration.zero;
if (newPos > mediaItem.duration) newPos = mediaItem.duration;
await _player.seek(newPos);
}
//Update state on all clients
Future _broadcastState() async {
await AudioServiceBackground.setState(
controls: [
MediaControl.skipToPrevious,
if (_player.playing) MediaControl.pause else MediaControl.play,
MediaControl.skipToNext,
//Stop
MediaControl(
androidIcon: 'drawable/ic_action_stop',
label: 'stop',
action: MediaAction.stop
)
],
systemActions: [
MediaAction.seekTo,
MediaAction.seekForward,
MediaAction.seekBackward,
//MediaAction.stop
],
processingState: _getProcessingState(),
playing: _player.playing,
position: _player.position,
bufferedPosition: _player.bufferedPosition,
speed: _player.speed
);
}
//just_audio state -> audio_service state. If skipping, use _skipState
AudioProcessingState _getProcessingState() {
if (_skipState != null) return _skipState;
//SRC: audio_service example
switch (_player.processingState) {
case ProcessingState.none:
return AudioProcessingState.stopped;
case ProcessingState.loading:
return AudioProcessingState.connecting;
case ProcessingState.buffering:
return AudioProcessingState.buffering;
case ProcessingState.ready:
return AudioProcessingState.ready;
case ProcessingState.completed:
return AudioProcessingState.completed;
default:
throw Exception("Invalid state: ${_player.processingState}");
2020-06-23 21:23:12 +02:00
}
}
//Replace current queue
2020-06-23 21:23:12 +02:00
@override
Future onUpdateQueue(List<MediaItem> q) async {
//just_audio
_player.stop();
if (_audioSource != null) _audioSource.clear();
//audio_service
this._queue = q;
AudioServiceBackground.setQueue(_queue);
//Load
_queueIndex = 0;
await _loadQueue();
//await _player.seek(Duration.zero, index: 0);
}
//Load queue to just_audio
Future _loadQueue() async {
//Don't reset queue index by starting player
int qi = _queueIndex;
List<AudioSource> sources = [];
for(int i=0; i<_queue.length; i++) {
sources.add(await _mediaItemToAudioSource(_queue[i]));
2020-06-23 21:23:12 +02:00
}
_audioSource = ConcatenatingAudioSource(children: sources);
//Load in just_audio
try {
await _player.load(_audioSource);
await _player.seek(Duration.zero, index: qi);
} catch (e) {
//Error loading tracks
}
_queueIndex = qi;
AudioServiceBackground.setMediaItem(mediaItem);
2020-06-23 21:23:12 +02:00
}
Future<AudioSource> _mediaItemToAudioSource(MediaItem mi) async {
String url = await _getTrackUrl(mi);
if (url.startsWith('http')) return ProgressiveAudioSource(Uri.parse(url));
return AudioSource.uri(Uri.parse(url));
2020-06-23 21:23:12 +02:00
}
Future _getTrackUrl(MediaItem mediaItem, {int quality}) async {
//Check if offline
String _offlinePath = p.join((await getExternalStorageDirectory()).path, 'offline/');
File f = File(p.join(_offlinePath, mediaItem.id));
if (await f.exists()) {
return f.path;
}
//Due to current limitations of just_audio, quality fallback moved to DeezerDataSource in ExoPlayer
//This just returns fake url that contains metadata
List playbackDetails = jsonDecode(mediaItem.extras['playbackDetails']);
//Quality
ConnectivityResult conn = await Connectivity().checkConnectivity();
quality = mobileQuality;
if (conn == ConnectivityResult.wifi) quality = wifiQuality;
String url = 'https://dzcdn.net/?md5=${playbackDetails[0]}&mv=${playbackDetails[1]}&q=${quality.toString()}#${mediaItem.id}';
return url;
}
2020-06-23 21:23:12 +02:00
//Custom actions
2020-06-23 21:23:12 +02:00
@override
Future onCustomAction(String name, dynamic args) async {
if (name == 'updateQuality') {
//Pass wifi & mobile quality by custom action
//Isolate can't access globals
this.wifiQuality = args['wifiQuality'];
this.mobileQuality = args['mobileQuality'];
}
//Change queue source
if (name == 'queueSource') {
this.queueSource = QueueSource.fromJson(Map<String, dynamic>.from(args));
}
//Looping
2020-06-23 21:23:12 +02:00
if (name == 'repeatType') {
_player.setLoopMode(LoopMode.values[args]);
2020-06-23 21:23:12 +02:00
}
if (name == 'saveQueue') await this._saveQueue();
//Load queue after some initialization in frontend
if (name == 'load') await this._loadQueueFile();
//Shuffle
2020-09-22 19:13:54 +02:00
if (name == 'shuffle') {
await _player.setShuffleModeEnabled(args);
}
//Android auto callback
if (name == 'screenAndroidAuto' && _androidAutoCallback != null) {
_androidAutoCallback.complete(jsonDecode(args).map<MediaItem>((m) => MediaItem.fromJson(m)).toList());
2020-06-23 21:23:12 +02:00
}
return true;
2020-06-23 21:23:12 +02:00
}
@override
Future onTaskRemoved() async {
await onStop();
2020-06-23 21:23:12 +02:00
}
@override
Future onClose() async {
print('onClose');
2020-06-23 21:23:12 +02:00
await onStop();
}
Future onStop() async {
await _saveQueue();
_player.stop();
if (_eventSub != null) _eventSub.cancel();
2020-06-23 21:23:12 +02:00
await super.onStop();
2020-06-23 21:23:12 +02:00
}
//Get queue save file path
2020-06-23 21:23:12 +02:00
Future<String> _getQueuePath() async {
Directory dir = await getApplicationDocumentsDirectory();
return p.join(dir.path, 'playback.json');
2020-06-23 21:23:12 +02:00
}
//Export queue to JSON
Future _saveQueue() async {
if (_queueIndex == 0 && _queue.length == 0) return;
String path = await _getQueuePath();
File f = File(path);
//Create if doesn't exist
if (! await File(path).exists()) {
f = await f.create();
}
Map data = {
2020-06-23 21:23:12 +02:00
'index': _queueIndex,
'queue': _queue.map<Map<String, dynamic>>((mi) => mi.toJson()).toList(),
'position': _player.position.inMilliseconds,
2020-06-23 21:23:12 +02:00
'queueSource': (queueSource??QueueSource()).toJson(),
};
await f.writeAsString(jsonEncode(data));
2020-06-23 21:23:12 +02:00
}
//Restore queue & playback info from path
Future _loadQueueFile() async {
2020-06-23 21:23:12 +02:00
File f = File(await _getQueuePath());
if (await f.exists()) {
Map<String, dynamic> json = jsonDecode(await f.readAsString());
this._queue = (json['queue']??[]).map<MediaItem>((mi) => MediaItem.fromJson(mi)).toList();
this._queueIndex = json['index'] ?? 0;
2020-06-23 21:23:12 +02:00
this._lastPosition = Duration(milliseconds: json['position']??0);
this.queueSource = QueueSource.fromJson(json['queueSource']??{});
//Restore queue
2020-06-23 21:23:12 +02:00
if (_queue != null) {
await AudioServiceBackground.setQueue(_queue);
await _loadQueue();
await AudioServiceBackground.setMediaItem(mediaItem);
2020-06-23 21:23:12 +02:00
}
}
//Send restored queue source to ui
AudioServiceBackground.sendCustomEvent({
'action': 'onRestore',
'queueSource': (queueSource??QueueSource()).toJson()
});
return true;
2020-06-23 21:23:12 +02:00
}
@override
Future onAddQueueItemAt(MediaItem mi, int index) async {
//-1 == play next
if (index == -1) index = _queueIndex + 1;
_queue.insert(index, mi);
await AudioServiceBackground.setQueue(_queue);
await _audioSource.insert(index, await _mediaItemToAudioSource(mi));
_saveQueue();
}
//Add at end of queue
@override
Future onAddQueueItem(MediaItem mi) async {
_queue.add(mi);
await AudioServiceBackground.setQueue(_queue);
await _audioSource.add(await _mediaItemToAudioSource(mi));
_saveQueue();
}
@override
Future onPlayFromMediaId(String mediaId) async {
//Android auto load tracks
if (mediaId.startsWith(AndroidAuto.prefix)) {
AudioServiceBackground.sendCustomEvent({
'action': 'tracksAndroidAuto',
'id': mediaId.replaceFirst(AndroidAuto.prefix, '')
});
return;
}
//Does the same thing
await this.onSkipToQueueItem(mediaId);
}
}
//Seeker from audio_service example (why reinvent the wheel?)
//While holding seek button, will continuously seek
class Seeker {
final AudioPlayer player;
final Duration positionInterval;
final Duration stepInterval;
final MediaItem mediaItem;
bool _running = false;
Seeker(this.player, this.positionInterval, this.stepInterval, this.mediaItem);
Future start() async {
_running = true;
while (_running) {
Duration newPosition = player.position + positionInterval;
if (newPosition < Duration.zero) newPosition = Duration.zero;
if (newPosition > mediaItem.duration) newPosition = mediaItem.duration;
player.seek(newPosition);
await Future.delayed(stepInterval);
}
}
void stop() {
_running = false;
}
2020-09-01 16:41:15 +02:00
}