2020-02-25 10:57:12 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:just_audio/just_audio.dart';
|
2020-04-11 19:23:12 +02:00
|
|
|
import 'package:rxdart/rxdart.dart';
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../type/episodebrief.dart';
|
|
|
|
import '../local_storage/key_value_storage.dart';
|
|
|
|
import '../local_storage/sqflite_localpodcast.dart';
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
MediaControl playControl = MediaControl(
|
|
|
|
androidIcon: 'drawable/ic_stat_play_circle_filled',
|
|
|
|
label: 'Play',
|
|
|
|
action: MediaAction.play,
|
|
|
|
);
|
|
|
|
MediaControl pauseControl = MediaControl(
|
|
|
|
androidIcon: 'drawable/ic_stat_pause_circle_filled',
|
|
|
|
label: 'Pause',
|
|
|
|
action: MediaAction.pause,
|
|
|
|
);
|
|
|
|
MediaControl skipToNextControl = MediaControl(
|
|
|
|
androidIcon: 'drawable/baseline_skip_next_white_24',
|
|
|
|
label: 'Next',
|
|
|
|
action: MediaAction.skipToNext,
|
|
|
|
);
|
|
|
|
MediaControl skipToPreviousControl = MediaControl(
|
|
|
|
androidIcon: 'drawable/ic_action_skip_previous',
|
|
|
|
label: 'Previous',
|
|
|
|
action: MediaAction.skipToPrevious,
|
|
|
|
);
|
|
|
|
MediaControl stopControl = MediaControl(
|
|
|
|
androidIcon: 'drawable/baseline_close_white_24',
|
|
|
|
label: 'Stop',
|
|
|
|
action: MediaAction.stop,
|
|
|
|
);
|
|
|
|
MediaControl forward30 = MediaControl(
|
|
|
|
androidIcon: 'drawable/ic_stat_forward_30',
|
|
|
|
label: 'forward30',
|
|
|
|
action: MediaAction.fastForward,
|
|
|
|
);
|
|
|
|
|
|
|
|
void _audioPlayerTaskEntrypoint() async {
|
|
|
|
AudioServiceBackground.run(() => AudioPlayerTask());
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
|
|
|
class PlayHistory {
|
2020-03-01 13:17:06 +01:00
|
|
|
DBHelper dbHelper = DBHelper();
|
2020-02-25 10:57:12 +01:00
|
|
|
String title;
|
|
|
|
String url;
|
|
|
|
double seconds;
|
|
|
|
double seekValue;
|
2020-03-14 04:14:24 +01:00
|
|
|
DateTime playdate;
|
|
|
|
PlayHistory(this.title, this.url, this.seconds, this.seekValue,
|
|
|
|
{this.playdate});
|
2020-03-01 13:17:06 +01:00
|
|
|
EpisodeBrief _episode;
|
|
|
|
EpisodeBrief get episode => _episode;
|
|
|
|
|
|
|
|
getEpisode() async {
|
|
|
|
_episode = await dbHelper.getRssItemWithUrl(url);
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
|
2020-04-11 19:23:12 +02:00
|
|
|
class Playlist {
|
2020-02-25 10:57:12 +01:00
|
|
|
String name;
|
|
|
|
DBHelper dbHelper = DBHelper();
|
2020-03-31 18:36:20 +02:00
|
|
|
|
2020-02-25 10:57:12 +01:00
|
|
|
List<EpisodeBrief> _playlist;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-02-25 10:57:12 +01:00
|
|
|
List<EpisodeBrief> get playlist => _playlist;
|
|
|
|
KeyValueStorage storage = KeyValueStorage('playlist');
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
getPlaylist() async {
|
2020-03-14 04:14:24 +01:00
|
|
|
List<String> urls = await storage.getStringList();
|
|
|
|
if (urls.length == 0) {
|
2020-02-25 10:57:12 +01:00
|
|
|
_playlist = [];
|
|
|
|
} else {
|
|
|
|
_playlist = [];
|
2020-03-14 04:14:24 +01:00
|
|
|
await Future.forEach(urls, (url) async {
|
2020-02-25 10:57:12 +01:00
|
|
|
EpisodeBrief episode = await dbHelper.getRssItemWithUrl(url);
|
2020-03-31 18:36:20 +02:00
|
|
|
if (episode != null) _playlist.add(episode);
|
2020-02-25 10:57:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
savePlaylist() async {
|
2020-03-14 04:14:24 +01:00
|
|
|
List<String> urls = [];
|
2020-02-25 10:57:12 +01:00
|
|
|
urls.addAll(_playlist.map((e) => e.enclosureUrl));
|
2020-03-14 04:14:24 +01:00
|
|
|
await storage.saveStringList(urls);
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
addToPlayList(EpisodeBrief episodeBrief) async {
|
|
|
|
_playlist.add(episodeBrief);
|
|
|
|
await savePlaylist();
|
2020-04-23 19:46:36 +02:00
|
|
|
dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl);
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
addToPlayListAt(EpisodeBrief episodeBrief, int index) async {
|
|
|
|
_playlist.insert(index, episodeBrief);
|
|
|
|
await savePlaylist();
|
2020-04-23 19:46:36 +02:00
|
|
|
dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl);
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
Future<int> delFromPlaylist(EpisodeBrief episodeBrief) async {
|
|
|
|
int index = _playlist.indexOf(episodeBrief);
|
2020-03-01 13:17:06 +01:00
|
|
|
_playlist
|
|
|
|
.removeWhere((item) => item.enclosureUrl == episodeBrief.enclosureUrl);
|
|
|
|
await savePlaylist();
|
2020-03-31 18:36:20 +02:00
|
|
|
return index;
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:23:12 +02:00
|
|
|
enum SleepTimerMode { endOfEpisode, timer, undefined }
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
class AudioPlayerNotifier extends ChangeNotifier {
|
2020-02-25 10:57:12 +01:00
|
|
|
DBHelper dbHelper = DBHelper();
|
2020-04-22 20:10:57 +02:00
|
|
|
KeyValueStorage positionStorage = KeyValueStorage(audioPositionKey);
|
|
|
|
KeyValueStorage autoPlayStorage = KeyValueStorage(autoPlayKey);
|
|
|
|
KeyValueStorage autoAddStorage = KeyValueStorage(autoAddKey);
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
EpisodeBrief _episode;
|
2020-03-14 04:14:24 +01:00
|
|
|
Playlist _queue = Playlist();
|
2020-03-31 18:36:20 +02:00
|
|
|
bool _queueUpdate = false;
|
2020-03-14 04:14:24 +01:00
|
|
|
BasicPlaybackState _audioState = BasicPlaybackState.none;
|
2020-02-25 10:57:12 +01:00
|
|
|
bool _playerRunning = false;
|
2020-03-14 04:14:24 +01:00
|
|
|
bool _noSlide = true;
|
|
|
|
int _backgroundAudioDuration = 0;
|
|
|
|
int _backgroundAudioPosition = 0;
|
2020-02-25 10:57:12 +01:00
|
|
|
String _remoteErrorMessage;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-02-25 10:57:12 +01:00
|
|
|
double _seekSliderValue = 0.0;
|
2020-03-14 04:14:24 +01:00
|
|
|
int _lastPostion = 0;
|
2020-03-03 17:04:23 +01:00
|
|
|
bool _stopOnComplete = false;
|
|
|
|
Timer _stopTimer;
|
2020-03-19 20:58:30 +01:00
|
|
|
int _timeLeft = 0;
|
2020-04-02 11:52:26 +02:00
|
|
|
bool _startSleepTimer = false;
|
2020-03-19 20:58:30 +01:00
|
|
|
double _switchValue = 0;
|
2020-04-11 19:23:12 +02:00
|
|
|
SleepTimerMode _sleepTimerMode = SleepTimerMode.undefined;
|
2020-04-22 20:10:57 +02:00
|
|
|
//set autoplay episode in playlist
|
2020-03-14 04:14:24 +01:00
|
|
|
bool _autoPlay = true;
|
2020-04-27 19:26:33 +02:00
|
|
|
//TODO Set auto add episodes to playlist
|
|
|
|
//bool _autoAdd = false;
|
2020-03-14 04:14:24 +01:00
|
|
|
DateTime _current;
|
|
|
|
int _currentPosition;
|
2020-04-18 06:48:02 +02:00
|
|
|
double _currentSpeed = 1;
|
2020-04-11 19:59:52 +02:00
|
|
|
BehaviorSubject<List<MediaItem>> queueSubject;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
|
|
|
BasicPlaybackState get audioState => _audioState;
|
|
|
|
|
|
|
|
int get backgroundAudioDuration => _backgroundAudioDuration;
|
|
|
|
int get backgroundAudioPosition => _backgroundAudioPosition;
|
2020-02-25 10:57:12 +01:00
|
|
|
double get seekSliderValue => _seekSliderValue;
|
|
|
|
String get remoteErrorMessage => _remoteErrorMessage;
|
|
|
|
bool get playerRunning => _playerRunning;
|
2020-03-01 13:17:06 +01:00
|
|
|
int get lastPositin => _lastPostion;
|
2020-02-25 10:57:12 +01:00
|
|
|
Playlist get queue => _queue;
|
2020-03-31 18:36:20 +02:00
|
|
|
bool get queueUpdate => _queueUpdate;
|
2020-02-20 16:44:42 +01:00
|
|
|
EpisodeBrief get episode => _episode;
|
2020-03-03 17:04:23 +01:00
|
|
|
bool get stopOnComplete => _stopOnComplete;
|
2020-04-02 11:52:26 +02:00
|
|
|
bool get startSleepTimer => _startSleepTimer;
|
2020-04-11 19:23:12 +02:00
|
|
|
SleepTimerMode get sleepTimerMode => _sleepTimerMode;
|
2020-03-14 04:14:24 +01:00
|
|
|
bool get autoPlay => _autoPlay;
|
2020-03-19 20:58:30 +01:00
|
|
|
int get timeLeft => _timeLeft;
|
|
|
|
double get switchValue => _switchValue;
|
2020-04-18 06:48:02 +02:00
|
|
|
double get currentSpeed => _currentSpeed;
|
2020-03-03 17:04:23 +01:00
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
set setSwitchValue(double value) {
|
|
|
|
_switchValue = value;
|
|
|
|
notifyListeners();
|
2020-03-03 17:04:23 +01:00
|
|
|
}
|
2020-03-01 13:17:06 +01:00
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
set autoPlaySwitch(bool boo) {
|
2020-03-14 04:14:24 +01:00
|
|
|
_autoPlay = boo;
|
|
|
|
notifyListeners();
|
2020-04-22 20:10:57 +02:00
|
|
|
_setAutoPlay();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _getAutoPlay() async {
|
|
|
|
int i = await autoPlayStorage.getInt();
|
2020-04-27 19:26:33 +02:00
|
|
|
_autoPlay = i == 0 ? true : false;
|
2020-04-22 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future _setAutoPlay() async {
|
|
|
|
await autoPlayStorage.saveInt(_autoPlay ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:23:12 +02:00
|
|
|
set setSleepTimerMode(SleepTimerMode timer) {
|
|
|
|
_sleepTimerMode = timer;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
|
|
|
void addListener(VoidCallback listener) async {
|
|
|
|
super.addListener(listener);
|
2020-03-31 18:36:20 +02:00
|
|
|
_queueUpdate = false;
|
2020-03-14 04:14:24 +01:00
|
|
|
await AudioService.connect();
|
2020-04-18 06:48:02 +02:00
|
|
|
bool running = AudioService.running;
|
2020-04-02 11:52:26 +02:00
|
|
|
if (running) {}
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
loadPlaylist() async {
|
|
|
|
await _queue.getPlaylist();
|
2020-04-22 20:10:57 +02:00
|
|
|
await _getAutoPlay();
|
|
|
|
// await _getAutoAdd();
|
|
|
|
// await addNewEpisode('all');
|
|
|
|
_lastPostion = await positionStorage.getInt();
|
2020-03-31 18:36:20 +02:00
|
|
|
if (_lastPostion > 0 && _queue.playlist.length > 0) {
|
|
|
|
final EpisodeBrief episode = _queue.playlist.first;
|
2020-04-11 19:23:12 +02:00
|
|
|
final int duration = episode.duration * 1000;
|
2020-03-31 18:36:20 +02:00
|
|
|
final double seekValue = duration != 0 ? _lastPostion / duration : 1;
|
|
|
|
final PlayHistory history = PlayHistory(
|
|
|
|
episode.title, episode.enclosureUrl, _lastPostion / 1000, seekValue);
|
|
|
|
await dbHelper.saveHistory(history);
|
|
|
|
}
|
2020-04-22 20:10:57 +02:00
|
|
|
KeyValueStorage lastWorkStorage = KeyValueStorage(lastWorkKey);
|
|
|
|
await lastWorkStorage.saveInt(0);
|
2020-02-25 13:28:48 +01:00
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-04-11 19:59:52 +02:00
|
|
|
episodeLoad(EpisodeBrief episode, {int startPosition = 0}) async {
|
2020-03-31 18:36:20 +02:00
|
|
|
final EpisodeBrief episodeNew =
|
|
|
|
await dbHelper.getRssItemWithUrl(episode.enclosureUrl);
|
2020-04-25 15:50:27 +02:00
|
|
|
//TODO load episode from last position when player running
|
2020-03-14 04:14:24 +01:00
|
|
|
if (_playerRunning) {
|
2020-03-01 13:17:06 +01:00
|
|
|
PlayHistory history = PlayHistory(_episode.title, _episode.enclosureUrl,
|
2020-03-14 04:14:24 +01:00
|
|
|
backgroundAudioPosition / 1000, seekSliderValue);
|
2020-03-01 13:17:06 +01:00
|
|
|
await dbHelper.saveHistory(history);
|
2020-03-31 18:36:20 +02:00
|
|
|
AudioService.addQueueItemAt(episodeNew.toMediaItem(), 0);
|
2020-03-14 04:14:24 +01:00
|
|
|
_queue.playlist
|
|
|
|
.removeWhere((item) => item.enclosureUrl == episode.enclosureUrl);
|
2020-03-31 18:36:20 +02:00
|
|
|
_queue.playlist.insert(0, episodeNew);
|
2020-03-14 04:14:24 +01:00
|
|
|
notifyListeners();
|
|
|
|
await _queue.savePlaylist();
|
|
|
|
} else {
|
|
|
|
await _queue.getPlaylist();
|
2020-04-11 19:23:12 +02:00
|
|
|
await _queue.delFromPlaylist(episode);
|
|
|
|
await _queue.addToPlayListAt(episodeNew, 0);
|
2020-03-14 04:14:24 +01:00
|
|
|
_backgroundAudioDuration = 0;
|
|
|
|
_backgroundAudioPosition = 0;
|
|
|
|
_seekSliderValue = 0;
|
2020-03-31 18:36:20 +02:00
|
|
|
_episode = episodeNew;
|
2020-03-14 04:14:24 +01:00
|
|
|
_playerRunning = true;
|
2020-04-11 19:23:12 +02:00
|
|
|
_audioState = BasicPlaybackState.connecting;
|
2020-03-14 04:14:24 +01:00
|
|
|
notifyListeners();
|
2020-04-11 19:23:12 +02:00
|
|
|
//await _queue.savePlaylist();
|
2020-04-25 15:50:27 +02:00
|
|
|
_startAudioService(startPosition, episodeNew.enclosureUrl);
|
2020-04-23 19:46:36 +02:00
|
|
|
if (episodeNew.isNew == 1)
|
|
|
|
dbHelper.removeEpisodeNewMark(episodeNew.enclosureUrl);
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 15:50:27 +02:00
|
|
|
_startAudioService(int position, String url) async {
|
2020-04-11 19:23:12 +02:00
|
|
|
_stopOnComplete = false;
|
|
|
|
_sleepTimerMode = SleepTimerMode.undefined;
|
2020-03-14 04:14:24 +01:00
|
|
|
if (!AudioService.connected) {
|
|
|
|
await AudioService.connect();
|
|
|
|
}
|
2020-04-27 19:26:33 +02:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
await AudioService.start(
|
2020-04-06 14:18:08 +02:00
|
|
|
backgroundTaskEntrypoint: _audioPlayerTaskEntrypoint,
|
|
|
|
androidNotificationChannelName: 'Tsacdop',
|
|
|
|
notificationColor: 0xFF4d91be,
|
|
|
|
androidNotificationIcon: 'drawable/ic_notification',
|
|
|
|
enableQueue: true,
|
|
|
|
androidStopOnRemoveTask: true,
|
|
|
|
androidStopForegroundOnPause: true);
|
2020-03-19 20:58:30 +01:00
|
|
|
if (_autoPlay) {
|
2020-03-14 04:14:24 +01:00
|
|
|
await Future.forEach(_queue.playlist, (episode) async {
|
|
|
|
await AudioService.addQueueItem(episode.toMediaItem());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await AudioService.addQueueItem(_queue.playlist.first.toMediaItem());
|
|
|
|
}
|
2020-04-11 19:23:12 +02:00
|
|
|
_playerRunning = true;
|
2020-03-14 04:14:24 +01:00
|
|
|
await AudioService.play();
|
2020-04-11 19:23:12 +02:00
|
|
|
|
|
|
|
AudioService.currentMediaItemStream
|
|
|
|
.where((event) => event != null)
|
|
|
|
.listen((item) async {
|
|
|
|
_episode = await dbHelper.getRssItemWithMediaId(item.id);
|
|
|
|
_backgroundAudioDuration = item?.duration ?? 0;
|
2020-04-25 15:50:27 +02:00
|
|
|
if (position > 0 &&
|
|
|
|
_backgroundAudioDuration > 0 &&
|
|
|
|
_episode.enclosureUrl == url) {
|
2020-04-11 19:23:12 +02:00
|
|
|
AudioService.seekTo(position);
|
|
|
|
position = 0;
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
});
|
2020-04-18 06:48:02 +02:00
|
|
|
|
2020-04-11 19:59:52 +02:00
|
|
|
queueSubject = BehaviorSubject<List<MediaItem>>();
|
2020-04-11 19:23:12 +02:00
|
|
|
queueSubject.addStream(
|
|
|
|
AudioService.queueStream.distinct().where((event) => event != null));
|
|
|
|
queueSubject.stream.listen((event) {
|
|
|
|
print(event.length);
|
|
|
|
if (event.length == _queue.playlist.length - 1 &&
|
|
|
|
_audioState == BasicPlaybackState.skippingToNext) {
|
2020-05-06 14:08:41 +02:00
|
|
|
if (event.length == 0 || _stopOnComplete) {
|
2020-04-11 19:23:12 +02:00
|
|
|
_queue.delFromPlaylist(_episode);
|
|
|
|
_lastPostion = 0;
|
2020-05-06 14:08:41 +02:00
|
|
|
notifyListeners();
|
2020-04-22 20:10:57 +02:00
|
|
|
positionStorage.saveInt(_lastPostion);
|
2020-04-11 19:23:12 +02:00
|
|
|
final PlayHistory history = PlayHistory(
|
|
|
|
_episode.title,
|
|
|
|
_episode.enclosureUrl,
|
|
|
|
backgroundAudioPosition / 1000,
|
|
|
|
seekSliderValue);
|
|
|
|
dbHelper.saveHistory(history);
|
|
|
|
} else if (event.first.id != _episode.mediaId) {
|
2020-05-06 14:08:41 +02:00
|
|
|
_lastPostion = 0;
|
|
|
|
notifyListeners();
|
|
|
|
positionStorage.saveInt(_lastPostion);
|
2020-04-11 19:23:12 +02:00
|
|
|
_queue.delFromPlaylist(_episode);
|
|
|
|
final PlayHistory history = PlayHistory(
|
|
|
|
_episode.title,
|
|
|
|
_episode.enclosureUrl,
|
|
|
|
backgroundAudioPosition / 1000,
|
|
|
|
seekSliderValue);
|
|
|
|
dbHelper.saveHistory(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
AudioService.playbackStateStream.listen((event) async {
|
|
|
|
_current = DateTime.now();
|
|
|
|
_audioState = event?.basicState;
|
2020-04-18 21:46:10 +02:00
|
|
|
if (_audioState == BasicPlaybackState.stopped) {
|
|
|
|
_playerRunning = false;
|
|
|
|
if (_switchValue > 0) _switchValue = 0;
|
|
|
|
}
|
2020-03-31 18:36:20 +02:00
|
|
|
|
|
|
|
if (_audioState == BasicPlaybackState.error) {
|
|
|
|
_remoteErrorMessage = 'Network Error';
|
|
|
|
}
|
|
|
|
if (_audioState != BasicPlaybackState.error &&
|
|
|
|
_audioState != BasicPlaybackState.paused) {
|
|
|
|
_remoteErrorMessage = null;
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
2020-03-31 18:36:20 +02:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
_currentPosition = event?.currentPosition ?? 0;
|
|
|
|
notifyListeners();
|
|
|
|
});
|
2020-03-31 18:36:20 +02:00
|
|
|
|
2020-04-24 06:19:56 +02:00
|
|
|
//double s = _currentSpeed ?? 1.0;
|
2020-04-18 06:48:02 +02:00
|
|
|
int getPosition = 0;
|
2020-04-26 07:41:25 +02:00
|
|
|
Timer.periodic(Duration(milliseconds: 500), (timer) {
|
2020-04-24 06:19:56 +02:00
|
|
|
double s = _currentSpeed ?? 1.0;
|
2020-03-14 04:14:24 +01:00
|
|
|
if (_noSlide) {
|
2020-03-31 18:36:20 +02:00
|
|
|
if (_audioState == BasicPlaybackState.playing) {
|
2020-04-18 06:48:02 +02:00
|
|
|
getPosition = _currentPosition +
|
2020-04-24 06:19:56 +02:00
|
|
|
((DateTime.now().difference(_current).inMilliseconds) * s)
|
|
|
|
.toInt();
|
2020-04-18 06:48:02 +02:00
|
|
|
_backgroundAudioPosition = getPosition < _backgroundAudioDuration
|
|
|
|
? getPosition
|
|
|
|
: _backgroundAudioDuration;
|
2020-03-31 18:36:20 +02:00
|
|
|
} else
|
2020-04-24 06:19:56 +02:00
|
|
|
_backgroundAudioPosition = _currentPosition ?? 0;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
|
|
|
if (_backgroundAudioDuration != null &&
|
|
|
|
_backgroundAudioDuration != 0 &&
|
|
|
|
_backgroundAudioPosition != null) {
|
|
|
|
_seekSliderValue =
|
|
|
|
_backgroundAudioPosition / _backgroundAudioDuration ?? 0;
|
2020-03-31 18:36:20 +02:00
|
|
|
} else
|
|
|
|
_seekSliderValue = 0;
|
|
|
|
|
2020-05-06 14:08:41 +02:00
|
|
|
if (_backgroundAudioPosition > 0 &&
|
|
|
|
_backgroundAudioPosition < _backgroundAudioDuration) {
|
2020-03-14 04:14:24 +01:00
|
|
|
_lastPostion = _backgroundAudioPosition;
|
2020-04-22 20:10:57 +02:00
|
|
|
positionStorage.saveInt(_lastPostion);
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2020-04-11 19:23:12 +02:00
|
|
|
if (_audioState == BasicPlaybackState.stopped) {
|
2020-03-14 04:14:24 +01:00
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
});
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
playlistLoad() async {
|
|
|
|
await _queue.getPlaylist();
|
2020-03-14 04:14:24 +01:00
|
|
|
_backgroundAudioDuration = 0;
|
|
|
|
_backgroundAudioPosition = 0;
|
|
|
|
_seekSliderValue = 0;
|
2020-03-01 13:17:06 +01:00
|
|
|
_episode = _queue.playlist.first;
|
|
|
|
_playerRunning = true;
|
2020-04-11 19:23:12 +02:00
|
|
|
_audioState = BasicPlaybackState.connecting;
|
2020-04-02 11:52:26 +02:00
|
|
|
_queueUpdate = !_queueUpdate;
|
2020-02-20 16:44:42 +01:00
|
|
|
notifyListeners();
|
2020-04-25 15:50:27 +02:00
|
|
|
_startAudioService(_lastPostion ?? 0, _queue.playlist.first.enclosureUrl);
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
|
|
|
playNext() async {
|
2020-03-14 04:14:24 +01:00
|
|
|
AudioService.skipToNext();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
addToPlaylist(EpisodeBrief episode) async {
|
2020-04-22 20:10:57 +02:00
|
|
|
if (!_queue.playlist.contains(episode)) {
|
|
|
|
if (_playerRunning) {
|
|
|
|
await AudioService.addQueueItem(episode.toMediaItem());
|
|
|
|
}
|
|
|
|
await _queue.addToPlayList(episode);
|
|
|
|
notifyListeners();
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
addToPlaylistAt(EpisodeBrief episode, int index) async {
|
|
|
|
if (_playerRunning) {
|
|
|
|
await AudioService.addQueueItemAt(episode.toMediaItem(), index);
|
|
|
|
}
|
|
|
|
await _queue.addToPlayListAt(episode, index);
|
2020-03-31 18:36:20 +02:00
|
|
|
_queueUpdate = !_queueUpdate;
|
2020-03-14 04:14:24 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:10:57 +02:00
|
|
|
addNewEpisode(List<String> group) async {
|
|
|
|
List<EpisodeBrief> newEpisodes = [];
|
|
|
|
if (group.first == 'All')
|
|
|
|
newEpisodes = await dbHelper.getRecentNewRssItem();
|
|
|
|
else
|
|
|
|
newEpisodes = await dbHelper.getGroupNewRssItem(group);
|
|
|
|
if (newEpisodes.length > 0)
|
|
|
|
await Future.forEach(newEpisodes, (episode) async {
|
|
|
|
await addToPlaylist(episode);
|
|
|
|
});
|
|
|
|
if (group.first == 'All')
|
|
|
|
await dbHelper.removeAllNewMark();
|
|
|
|
else
|
|
|
|
await dbHelper.removeGroupNewMark(group);
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
updateMediaItem(EpisodeBrief episode) async {
|
|
|
|
int index = _queue.playlist
|
|
|
|
.indexWhere((item) => item.enclosureUrl == episode.enclosureUrl);
|
|
|
|
if (index > 0) {
|
2020-03-31 18:36:20 +02:00
|
|
|
EpisodeBrief episodeNew =
|
|
|
|
await dbHelper.getRssItemWithUrl(episode.enclosureUrl);
|
2020-03-14 04:14:24 +01:00
|
|
|
await delFromPlaylist(episode);
|
2020-03-31 18:36:20 +02:00
|
|
|
await addToPlaylistAt(episodeNew, index);
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
Future<int> delFromPlaylist(EpisodeBrief episode) async {
|
2020-03-14 04:14:24 +01:00
|
|
|
if (_playerRunning) {
|
|
|
|
await AudioService.removeQueueItem(episode.toMediaItem());
|
|
|
|
}
|
2020-03-31 18:36:20 +02:00
|
|
|
int index = await _queue.delFromPlaylist(episode);
|
2020-04-06 14:18:08 +02:00
|
|
|
_queueUpdate = !_queueUpdate;
|
2020-03-01 13:17:06 +01:00
|
|
|
notifyListeners();
|
2020-03-31 18:36:20 +02:00
|
|
|
return index;
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
moveToTop(EpisodeBrief episode) async {
|
|
|
|
await delFromPlaylist(episode);
|
|
|
|
if (_playerRunning) {
|
|
|
|
await addToPlaylistAt(episode, 1);
|
|
|
|
} else {
|
|
|
|
await addToPlaylistAt(episode, 0);
|
|
|
|
_lastPostion = 0;
|
2020-04-22 20:10:57 +02:00
|
|
|
positionStorage.saveInt(_lastPostion);
|
2020-03-19 20:58:30 +01:00
|
|
|
}
|
2020-03-31 18:36:20 +02:00
|
|
|
notifyListeners();
|
2020-03-19 20:58:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:57:12 +01:00
|
|
|
pauseAduio() async {
|
2020-03-14 04:14:24 +01:00
|
|
|
AudioService.pause();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
resumeAudio() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
if (_audioState != BasicPlaybackState.connecting &&
|
|
|
|
_audioState != BasicPlaybackState.none) AudioService.play();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
forwardAudio(int s) {
|
|
|
|
int pos = _backgroundAudioPosition + s * 1000;
|
|
|
|
AudioService.seekTo(pos);
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
2020-04-02 11:52:26 +02:00
|
|
|
|
|
|
|
seekTo(int position) async {
|
|
|
|
if (_audioState != BasicPlaybackState.connecting &&
|
2020-03-31 18:36:20 +02:00
|
|
|
_audioState != BasicPlaybackState.none)
|
|
|
|
await AudioService.seekTo(position);
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
sliderSeek(double val) async {
|
|
|
|
print(val.toString());
|
2020-03-31 18:36:20 +02:00
|
|
|
if (_audioState != BasicPlaybackState.connecting &&
|
|
|
|
_audioState != BasicPlaybackState.none) {
|
|
|
|
_noSlide = false;
|
|
|
|
_seekSliderValue = val;
|
|
|
|
notifyListeners();
|
|
|
|
_currentPosition = (val * _backgroundAudioDuration).toInt();
|
|
|
|
await AudioService.seekTo(_currentPosition);
|
|
|
|
_noSlide = true;
|
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-04-18 06:48:02 +02:00
|
|
|
setSpeed(double speed) async {
|
|
|
|
await AudioService.customAction('setSpeed', speed);
|
|
|
|
_currentSpeed = speed;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:52:26 +02:00
|
|
|
//Set sleep timer
|
2020-03-03 17:04:23 +01:00
|
|
|
sleepTimer(int mins) {
|
2020-04-11 19:23:12 +02:00
|
|
|
if (_sleepTimerMode == SleepTimerMode.timer) {
|
|
|
|
_startSleepTimer = true;
|
|
|
|
_switchValue = 1;
|
|
|
|
notifyListeners();
|
|
|
|
_timeLeft = mins * 60;
|
|
|
|
Timer.periodic(Duration(seconds: 1), (timer) {
|
|
|
|
if (_timeLeft == 0) {
|
|
|
|
timer.cancel();
|
|
|
|
notifyListeners();
|
|
|
|
} else {
|
|
|
|
_timeLeft = _timeLeft - 1;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_stopTimer = Timer(Duration(minutes: mins), () {
|
|
|
|
_stopOnComplete = false;
|
|
|
|
_startSleepTimer = false;
|
|
|
|
_switchValue = 0;
|
|
|
|
_playerRunning = false;
|
2020-03-19 20:58:30 +01:00
|
|
|
notifyListeners();
|
2020-04-11 19:23:12 +02:00
|
|
|
AudioService.stop();
|
|
|
|
AudioService.disconnect();
|
|
|
|
});
|
|
|
|
} else if (_sleepTimerMode == SleepTimerMode.endOfEpisode) {
|
|
|
|
_stopOnComplete = true;
|
|
|
|
_switchValue = 1;
|
2020-04-06 14:18:08 +02:00
|
|
|
notifyListeners();
|
2020-04-11 19:23:12 +02:00
|
|
|
if (_queue.playlist.length > 1 && _autoPlay) {
|
|
|
|
AudioService.customAction('stopAtEnd');
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 17:04:23 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-03-03 17:04:23 +01:00
|
|
|
//Cancel sleep timer
|
2020-03-14 04:14:24 +01:00
|
|
|
cancelTimer() {
|
2020-04-11 19:23:12 +02:00
|
|
|
if (_sleepTimerMode == SleepTimerMode.timer) {
|
|
|
|
_stopTimer.cancel();
|
|
|
|
_timeLeft = 0;
|
|
|
|
_startSleepTimer = false;
|
|
|
|
_switchValue = 0;
|
|
|
|
notifyListeners();
|
|
|
|
} else if (_sleepTimerMode == SleepTimerMode.endOfEpisode) {
|
|
|
|
AudioService.customAction('cancelStopAtEnd');
|
|
|
|
_switchValue = 0;
|
|
|
|
_stopOnComplete = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2020-03-03 17:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-03-14 04:14:24 +01:00
|
|
|
void dispose() async {
|
|
|
|
await AudioService.stop();
|
|
|
|
await AudioService.disconnect();
|
2020-04-11 19:23:12 +02:00
|
|
|
//_playerRunning = false;
|
2020-03-01 13:17:06 +01:00
|
|
|
super.dispose();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
class AudioPlayerTask extends BackgroundAudioTask {
|
2020-04-27 19:26:33 +02:00
|
|
|
KeyValueStorage cacheStorage = KeyValueStorage(cacheMaxKey);
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
List<MediaItem> _queue = [];
|
|
|
|
AudioPlayer _audioPlayer = AudioPlayer();
|
|
|
|
Completer _completer = Completer();
|
|
|
|
BasicPlaybackState _skipState;
|
|
|
|
bool _playing;
|
2020-04-11 19:23:12 +02:00
|
|
|
bool _stopAtEnd;
|
2020-04-27 19:26:33 +02:00
|
|
|
int cacheMax;
|
2020-03-14 04:14:24 +01:00
|
|
|
bool get hasNext => _queue.length > 0;
|
|
|
|
|
2020-04-23 19:46:36 +02:00
|
|
|
MediaItem get mediaItem => _queue.length > 0 ? _queue.first : null;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
|
|
|
BasicPlaybackState _stateToBasicState(AudioPlaybackState state) {
|
|
|
|
switch (state) {
|
|
|
|
case AudioPlaybackState.none:
|
|
|
|
return BasicPlaybackState.none;
|
|
|
|
case AudioPlaybackState.stopped:
|
2020-03-19 20:58:30 +01:00
|
|
|
return _skipState ?? BasicPlaybackState.stopped;
|
2020-03-14 04:14:24 +01:00
|
|
|
case AudioPlaybackState.paused:
|
|
|
|
return BasicPlaybackState.paused;
|
|
|
|
case AudioPlaybackState.playing:
|
|
|
|
return BasicPlaybackState.playing;
|
|
|
|
case AudioPlaybackState.connecting:
|
|
|
|
return _skipState ?? BasicPlaybackState.connecting;
|
|
|
|
case AudioPlaybackState.completed:
|
|
|
|
return BasicPlaybackState.stopped;
|
|
|
|
default:
|
|
|
|
throw Exception("Illegal state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> onStart() async {
|
2020-04-11 19:23:12 +02:00
|
|
|
_stopAtEnd = false;
|
2020-04-28 16:33:50 +02:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
var playerStateSubscription = _audioPlayer.playbackStateStream
|
|
|
|
.where((state) => state == AudioPlaybackState.completed)
|
|
|
|
.listen((state) {
|
|
|
|
_handlePlaybackCompleted();
|
2020-02-25 10:57:12 +01:00
|
|
|
});
|
2020-03-14 04:14:24 +01:00
|
|
|
var eventSubscription = _audioPlayer.playbackEventStream.listen((event) {
|
2020-03-31 18:36:20 +02:00
|
|
|
if (event.playbackError != null) {
|
|
|
|
_setState(state: BasicPlaybackState.error);
|
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
BasicPlaybackState state;
|
|
|
|
if (event.buffering) {
|
2020-04-11 19:23:12 +02:00
|
|
|
state = _skipState ?? BasicPlaybackState.buffering;
|
2020-03-14 04:14:24 +01:00
|
|
|
} else {
|
|
|
|
state = _stateToBasicState(event.state);
|
|
|
|
}
|
|
|
|
if (state != BasicPlaybackState.stopped) {
|
|
|
|
_setState(
|
|
|
|
state: state,
|
|
|
|
position: event.position.inMilliseconds,
|
2020-04-24 06:19:56 +02:00
|
|
|
speed: event.speed,
|
2020-03-14 04:14:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await _completer.future;
|
|
|
|
playerStateSubscription.cancel();
|
|
|
|
eventSubscription.cancel();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 19:23:12 +02:00
|
|
|
void _handlePlaybackCompleted() async {
|
2020-03-14 04:14:24 +01:00
|
|
|
if (hasNext) {
|
|
|
|
onSkipToNext();
|
|
|
|
} else {
|
2020-03-19 20:58:30 +01:00
|
|
|
_audioPlayer.stop();
|
|
|
|
_queue.removeAt(0);
|
2020-04-11 19:23:12 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
2020-03-14 04:14:24 +01:00
|
|
|
onStop();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
void playPause() {
|
|
|
|
if (AudioServiceBackground.state.basicState == BasicPlaybackState.playing)
|
|
|
|
onPause();
|
|
|
|
else
|
|
|
|
onPlay();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
|
|
|
Future<void> onSkipToNext() async {
|
2020-04-11 19:23:12 +02:00
|
|
|
_skipState = BasicPlaybackState.skippingToNext;
|
|
|
|
await _audioPlayer.stop();
|
2020-04-23 19:46:36 +02:00
|
|
|
if (_queue.length > 0) _queue.removeAt(0);
|
2020-04-11 19:23:12 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
|
|
|
// }
|
|
|
|
if (_queue.length == 0 || _stopAtEnd) {
|
2020-05-06 14:08:41 +02:00
|
|
|
// await Future.delayed(Duration(milliseconds: 300));
|
2020-04-11 19:23:12 +02:00
|
|
|
_skipState = null;
|
2020-03-19 20:58:30 +01:00
|
|
|
onStop();
|
2020-03-14 04:14:24 +01:00
|
|
|
} else {
|
2020-04-23 19:46:36 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
|
|
|
await AudioServiceBackground.setMediaItem(mediaItem);
|
2020-04-27 19:26:33 +02:00
|
|
|
await _audioPlayer.setUrl(mediaItem.id, cacheMax);
|
2020-04-23 19:46:36 +02:00
|
|
|
print(mediaItem.title);
|
|
|
|
Duration duration = await _audioPlayer.durationFuture;
|
2020-04-24 06:19:56 +02:00
|
|
|
if (duration != null)
|
|
|
|
await AudioServiceBackground.setMediaItem(
|
|
|
|
mediaItem.copyWith(duration: duration.inMilliseconds));
|
2020-03-19 20:58:30 +01:00
|
|
|
_skipState = null;
|
|
|
|
// Resume playback if we were playing
|
2020-04-11 19:23:12 +02:00
|
|
|
// if (_playing) {
|
2020-04-24 06:19:56 +02:00
|
|
|
//onPlay();
|
|
|
|
playFromStart();
|
2020-04-11 19:23:12 +02:00
|
|
|
// } else {
|
|
|
|
// _setState(state: BasicPlaybackState.paused);
|
|
|
|
// }
|
2020-03-03 17:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
|
|
|
void onPlay() async {
|
|
|
|
if (_skipState == null) {
|
|
|
|
if (_playing == null) {
|
|
|
|
_playing = true;
|
2020-04-28 16:33:50 +02:00
|
|
|
int cache = await cacheStorage.getInt();
|
|
|
|
cacheMax = cache == 0 ? 500 * 1024 * 1024 : cache;
|
2020-04-11 19:23:12 +02:00
|
|
|
// await AudioServiceBackground.setQueue(_queue);
|
2020-04-27 19:26:33 +02:00
|
|
|
await _audioPlayer.setUrl(mediaItem.id, cacheMax);
|
2020-04-23 19:46:36 +02:00
|
|
|
var duration = await _audioPlayer.durationFuture;
|
|
|
|
if (duration != null)
|
|
|
|
await AudioServiceBackground.setMediaItem(
|
|
|
|
mediaItem.copyWith(duration: duration.inMilliseconds));
|
2020-04-24 06:19:56 +02:00
|
|
|
playFromStart();
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
2020-04-22 20:10:57 +02:00
|
|
|
// if (mediaItem.extras['skip'] > 0) {
|
|
|
|
// await _audioPlayer.setClip(
|
|
|
|
// start: Duration(seconds: 60));
|
|
|
|
// print(mediaItem.extras['skip']);
|
|
|
|
// print('set clip success');
|
|
|
|
// }
|
2020-04-24 06:19:56 +02:00
|
|
|
else {
|
2020-04-23 19:46:36 +02:00
|
|
|
_playing = true;
|
2020-04-25 15:50:27 +02:00
|
|
|
if (_audioPlayer.playbackEvent.state != AudioPlaybackState.connecting ||
|
2020-04-24 06:19:56 +02:00
|
|
|
_audioPlayer.playbackEvent.state != AudioPlaybackState.none)
|
|
|
|
_audioPlayer.play();
|
2020-04-22 20:10:57 +02:00
|
|
|
}
|
2020-04-24 06:19:56 +02:00
|
|
|
// if (mediaItem.extras['skip'] >
|
|
|
|
// _audioPlayer.playbackEvent.position.inSeconds ??
|
|
|
|
// 0) {
|
|
|
|
// _audioPlayer.seek(Duration(seconds: mediaItem.extras['skip']));
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
playFromStart() async {
|
|
|
|
_playing = true;
|
2020-04-25 15:50:27 +02:00
|
|
|
if (_audioPlayer.playbackEvent.state != AudioPlaybackState.connecting ||
|
|
|
|
_audioPlayer.playbackEvent.state != AudioPlaybackState.none)
|
|
|
|
try {
|
|
|
|
_audioPlayer.play();
|
|
|
|
} catch (e) {
|
|
|
|
_setState(state: BasicPlaybackState.error);
|
|
|
|
}
|
2020-04-24 06:19:56 +02:00
|
|
|
if (mediaItem.extras['skip'] > 0) {
|
|
|
|
_audioPlayer.seek(Duration(seconds: mediaItem.extras['skip']));
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onPause() {
|
|
|
|
if (_skipState == null) {
|
|
|
|
if (_playing == null) {}
|
|
|
|
_playing = false;
|
|
|
|
_audioPlayer.pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onSeekTo(int position) {
|
2020-04-25 15:50:27 +02:00
|
|
|
if (_audioPlayer.playbackEvent.state != AudioPlaybackState.connecting ||
|
2020-04-24 06:19:56 +02:00
|
|
|
_audioPlayer.playbackEvent.state != AudioPlaybackState.none)
|
|
|
|
_audioPlayer.seek(Duration(milliseconds: position));
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onClick(MediaButton button) {
|
|
|
|
playPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onStop() async {
|
|
|
|
await _audioPlayer.stop();
|
2020-05-06 14:08:41 +02:00
|
|
|
await _audioPlayer.dispose();
|
2020-03-14 04:14:24 +01:00
|
|
|
_setState(state: BasicPlaybackState.stopped);
|
2020-05-06 14:08:41 +02:00
|
|
|
await Future.delayed(Duration(milliseconds: 300));
|
2020-04-23 19:46:36 +02:00
|
|
|
_completer?.complete();
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onAddQueueItem(MediaItem mediaItem) async {
|
|
|
|
_queue.add(mediaItem);
|
2020-04-11 19:23:12 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onRemoveQueueItem(MediaItem mediaItem) async {
|
|
|
|
_queue.removeWhere((item) => item.id == mediaItem.id);
|
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onAddQueueItemAt(MediaItem mediaItem, int index) async {
|
|
|
|
if (index == 0) {
|
|
|
|
await _audioPlayer.stop();
|
|
|
|
_queue.removeWhere((item) => item.id == mediaItem.id);
|
|
|
|
_queue.insert(0, mediaItem);
|
2020-04-11 19:23:12 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
|
|
|
await AudioServiceBackground.setMediaItem(mediaItem);
|
2020-04-27 19:26:33 +02:00
|
|
|
await _audioPlayer.setUrl(mediaItem.id, cacheMax);
|
2020-03-31 18:36:20 +02:00
|
|
|
Duration duration = await _audioPlayer.durationFuture ?? Duration.zero;
|
2020-03-14 04:14:24 +01:00
|
|
|
AudioServiceBackground.setMediaItem(
|
|
|
|
mediaItem.copyWith(duration: duration.inMilliseconds));
|
2020-04-24 06:19:56 +02:00
|
|
|
playFromStart();
|
|
|
|
//onPlay();
|
2020-03-03 17:04:23 +01:00
|
|
|
} else {
|
2020-03-14 04:14:24 +01:00
|
|
|
_queue.insert(index, mediaItem);
|
2020-04-11 19:23:12 +02:00
|
|
|
await AudioServiceBackground.setQueue(_queue);
|
2020-03-03 17:04:23 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onFastForward() {
|
|
|
|
_audioPlayer.seek(Duration(
|
|
|
|
milliseconds: AudioServiceBackground.state.position + 30 * 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onAudioFocusLost() {
|
|
|
|
if (_skipState == null) {
|
2020-04-06 14:18:08 +02:00
|
|
|
if (_playing == null ||
|
|
|
|
_audioPlayer.playbackState == AudioPlaybackState.none ||
|
|
|
|
_audioPlayer.playbackState == AudioPlaybackState.connecting) {}
|
2020-03-14 04:14:24 +01:00
|
|
|
_playing = false;
|
|
|
|
_audioPlayer.pause();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
|
|
|
void onAudioBecomingNoisy() {
|
|
|
|
if (_skipState == null) {
|
|
|
|
if (_playing == null) {}
|
|
|
|
_playing = false;
|
|
|
|
_audioPlayer.pause();
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
|
|
|
void onAudioFocusGained() {
|
|
|
|
if (_skipState == null) {
|
|
|
|
if (_playing == null) {}
|
|
|
|
_playing = true;
|
|
|
|
_audioPlayer.play();
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
@override
|
2020-05-08 13:35:08 +02:00
|
|
|
Future onCustomAction(funtion, argument) async {
|
2020-03-14 04:14:24 +01:00
|
|
|
switch (funtion) {
|
2020-04-11 19:23:12 +02:00
|
|
|
case 'stopAtEnd':
|
|
|
|
_stopAtEnd = true;
|
2020-03-14 04:14:24 +01:00
|
|
|
break;
|
2020-04-11 19:23:12 +02:00
|
|
|
case 'cancelStopAtEnd':
|
|
|
|
_stopAtEnd = false;
|
2020-03-14 04:14:24 +01:00
|
|
|
break;
|
2020-04-18 06:48:02 +02:00
|
|
|
case 'setSpeed':
|
|
|
|
await _audioPlayer.setSpeed(argument);
|
2020-04-25 15:50:27 +02:00
|
|
|
break;
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 06:19:56 +02:00
|
|
|
void _setState(
|
|
|
|
{@required BasicPlaybackState state, int position, double speed}) {
|
2020-03-14 04:14:24 +01:00
|
|
|
if (position == null) {
|
|
|
|
position = _audioPlayer.playbackEvent.position.inMilliseconds;
|
|
|
|
}
|
2020-04-24 06:19:56 +02:00
|
|
|
if (speed == null) {
|
|
|
|
speed = _audioPlayer.playbackEvent.speed;
|
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
AudioServiceBackground.setState(
|
|
|
|
controls: getControls(state),
|
|
|
|
systemActions: [MediaAction.seekTo],
|
|
|
|
basicState: state,
|
|
|
|
position: position,
|
2020-04-24 06:19:56 +02:00
|
|
|
speed: speed,
|
2020-03-14 04:14:24 +01:00
|
|
|
);
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
List<MediaControl> getControls(BasicPlaybackState state) {
|
|
|
|
if (_playing) {
|
|
|
|
return [pauseControl, forward30, skipToNextControl, stopControl];
|
|
|
|
} else {
|
|
|
|
return [playControl, forward30, skipToNextControl, stopControl];
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|