2020-03-07 02:50:59 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:html';
|
2020-07-09 03:27:53 +00:00
|
|
|
import 'dart:math';
|
2020-03-07 02:50:59 +00:00
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
2020-09-27 03:30:30 +00:00
|
|
|
import 'package:just_audio_platform_interface/just_audio_platform_interface.dart';
|
2020-03-07 02:50:59 +00:00
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
final Random _random = Random();
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
class JustAudioPlugin extends JustAudioPlatform {
|
2020-10-10 16:38:01 +00:00
|
|
|
final Map<String, JustAudioPlayer> players = {};
|
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
static void registerWith(Registrar registrar) {
|
2020-10-10 16:38:01 +00:00
|
|
|
print("registerWith setting instance");
|
2020-09-27 03:30:30 +00:00
|
|
|
JustAudioPlatform.instance = JustAudioPlugin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<AudioPlayerPlatform> init(InitRequest request) async {
|
2020-10-10 16:38:01 +00:00
|
|
|
final player = Html5AudioPlayer(id: request.id);
|
|
|
|
players[request.id] = player;
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<DisposePlayerResponse> disposePlayer(
|
|
|
|
DisposePlayerRequest request) async {
|
|
|
|
await players[request.id]?.release();
|
|
|
|
return DisposePlayerResponse();
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
abstract class JustAudioPlayer extends AudioPlayerPlatform {
|
|
|
|
final eventController = StreamController<PlaybackEventMessage>();
|
|
|
|
ProcessingStateMessage _processingState = ProcessingStateMessage.none;
|
2020-08-03 14:16:15 +00:00
|
|
|
bool _playing = false;
|
2020-07-09 03:27:53 +00:00
|
|
|
int _index;
|
2020-03-07 02:50:59 +00:00
|
|
|
|
2020-10-10 16:38:01 +00:00
|
|
|
JustAudioPlayer({@required String id}) : super(id);
|
2020-03-07 02:50:59 +00:00
|
|
|
|
|
|
|
@mustCallSuper
|
2020-10-10 16:38:01 +00:00
|
|
|
Future<void> release() async {
|
2020-03-07 02:50:59 +00:00
|
|
|
eventController.close();
|
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration getCurrentPosition();
|
2020-03-07 02:50:59 +00:00
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
Duration getBufferedPosition();
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration getDuration();
|
2020-06-09 17:42:36 +00:00
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
broadcastPlaybackEvent() {
|
2020-09-27 03:30:30 +00:00
|
|
|
var updateTime = DateTime.now();
|
|
|
|
eventController.add(PlaybackEventMessage(
|
|
|
|
processingState: _processingState,
|
|
|
|
updatePosition: getCurrentPosition(),
|
|
|
|
updateTime: updateTime,
|
|
|
|
bufferedPosition: getBufferedPosition(),
|
2020-06-09 17:42:36 +00:00
|
|
|
// TODO: Icy Metadata
|
2020-09-27 03:30:30 +00:00
|
|
|
icyMetadata: null,
|
|
|
|
duration: getDuration(),
|
|
|
|
currentIndex: _index,
|
|
|
|
androidAudioSessionId: null,
|
|
|
|
));
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage processingState) {
|
2020-08-03 14:16:15 +00:00
|
|
|
_processingState = processingState;
|
2020-03-07 02:50:59 +00:00
|
|
|
broadcastPlaybackEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Html5AudioPlayer extends JustAudioPlayer {
|
|
|
|
AudioElement _audioElement = AudioElement();
|
2020-07-09 03:27:53 +00:00
|
|
|
Completer _durationCompleter;
|
|
|
|
AudioSourcePlayer _audioSourcePlayer;
|
2020-09-27 03:30:30 +00:00
|
|
|
LoopModeMessage _loopMode = LoopModeMessage.off;
|
2020-07-09 03:27:53 +00:00
|
|
|
bool _shuffleModeEnabled = false;
|
|
|
|
final Map<String, AudioSourcePlayer> _audioSourcePlayers = {};
|
2020-03-07 02:50:59 +00:00
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
Html5AudioPlayer({@required String id}) : super(id: id) {
|
2020-03-07 02:50:59 +00:00
|
|
|
_audioElement.addEventListener('durationchange', (event) {
|
2020-07-09 03:27:53 +00:00
|
|
|
_durationCompleter?.complete();
|
2020-08-03 14:16:15 +00:00
|
|
|
broadcastPlaybackEvent();
|
2020-06-09 17:42:36 +00:00
|
|
|
});
|
|
|
|
_audioElement.addEventListener('error', (event) {
|
|
|
|
_durationCompleter?.completeError(_audioElement.error);
|
2020-03-07 02:50:59 +00:00
|
|
|
});
|
2020-07-09 03:27:53 +00:00
|
|
|
_audioElement.addEventListener('ended', (event) async {
|
2020-08-03 14:16:15 +00:00
|
|
|
_currentAudioSourcePlayer.complete();
|
2020-03-07 02:50:59 +00:00
|
|
|
});
|
2020-08-03 14:16:15 +00:00
|
|
|
_audioElement.addEventListener('timeupdate', (event) {
|
|
|
|
_currentAudioSourcePlayer.timeUpdated(_audioElement.currentTime);
|
|
|
|
});
|
|
|
|
_audioElement.addEventListener('loadstart', (event) {
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.buffering);
|
2020-08-03 14:16:15 +00:00
|
|
|
});
|
|
|
|
_audioElement.addEventListener('waiting', (event) {
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.buffering);
|
2020-03-07 02:50:59 +00:00
|
|
|
});
|
2020-08-03 14:16:15 +00:00
|
|
|
_audioElement.addEventListener('stalled', (event) {
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.buffering);
|
2020-08-03 14:16:15 +00:00
|
|
|
});
|
|
|
|
_audioElement.addEventListener('canplaythrough', (event) {
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.ready);
|
2020-08-03 14:16:15 +00:00
|
|
|
});
|
|
|
|
_audioElement.addEventListener('progress', (event) {
|
2020-03-07 02:50:59 +00:00
|
|
|
broadcastPlaybackEvent();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
List<int> get order {
|
|
|
|
final sequence = _audioSourcePlayer.sequence;
|
|
|
|
List<int> order = List<int>(sequence.length);
|
|
|
|
if (_shuffleModeEnabled) {
|
|
|
|
order = _audioSourcePlayer.shuffleOrder;
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < order.length; i++) {
|
|
|
|
order[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int> getInv(List<int> order) {
|
|
|
|
List<int> orderInv = List<int>(order.length);
|
|
|
|
for (var i = 0; i < order.length; i++) {
|
|
|
|
orderInv[order[i]] = i;
|
|
|
|
}
|
|
|
|
return orderInv;
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnded() async {
|
2020-09-27 03:30:30 +00:00
|
|
|
if (_loopMode == LoopModeMessage.one) {
|
|
|
|
await _seek(0, null);
|
|
|
|
_play();
|
2020-07-09 03:27:53 +00:00
|
|
|
} else {
|
|
|
|
final order = this.order;
|
|
|
|
final orderInv = getInv(order);
|
|
|
|
if (orderInv[_index] + 1 < order.length) {
|
|
|
|
// move to next item
|
|
|
|
_index = order[orderInv[_index] + 1];
|
|
|
|
await _currentAudioSourcePlayer.load();
|
|
|
|
// Should always be true...
|
|
|
|
if (_playing) {
|
2020-09-27 03:30:30 +00:00
|
|
|
_play();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// reached end of playlist
|
2020-09-27 03:30:30 +00:00
|
|
|
if (_loopMode == LoopModeMessage.all) {
|
2020-07-09 03:27:53 +00:00
|
|
|
// Loop back to the beginning
|
|
|
|
if (order.length == 1) {
|
2020-09-27 03:30:30 +00:00
|
|
|
await _seek(0, null);
|
|
|
|
_play();
|
2020-07-09 03:27:53 +00:00
|
|
|
} else {
|
|
|
|
_index = order[0];
|
|
|
|
await _currentAudioSourcePlayer.load();
|
|
|
|
// Should always be true...
|
|
|
|
if (_playing) {
|
2020-09-27 03:30:30 +00:00
|
|
|
_play();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.completed);
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Improve efficiency.
|
|
|
|
IndexedAudioSourcePlayer get _currentAudioSourcePlayer =>
|
|
|
|
_audioSourcePlayer != null && _index < _audioSourcePlayer.sequence.length
|
|
|
|
? _audioSourcePlayer.sequence[_index]
|
|
|
|
: null;
|
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Stream<PlaybackEventMessage> get playbackEventMessageStream =>
|
|
|
|
eventController.stream;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<LoadResponse> load(LoadRequest request) async {
|
2020-10-10 16:38:01 +00:00
|
|
|
print("web load");
|
2020-07-09 03:27:53 +00:00
|
|
|
_currentAudioSourcePlayer?.pause();
|
2020-09-27 03:30:30 +00:00
|
|
|
_audioSourcePlayer = getAudioSource(request.audioSourceMessage);
|
2020-07-09 03:27:53 +00:00
|
|
|
_index = 0;
|
|
|
|
if (_shuffleModeEnabled) {
|
|
|
|
_audioSourcePlayer?.shuffle(0, _index);
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
return LoadResponse(duration: await _currentAudioSourcePlayer.load());
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Duration> loadUri(final Uri uri) async {
|
2020-10-10 16:38:01 +00:00
|
|
|
print("loadUri $uri");
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.loading);
|
2020-07-09 03:27:53 +00:00
|
|
|
final src = uri.toString();
|
|
|
|
if (src != _audioElement.src) {
|
|
|
|
_durationCompleter = Completer<num>();
|
|
|
|
_audioElement.src = src;
|
|
|
|
_audioElement.preload = 'auto';
|
|
|
|
_audioElement.load();
|
|
|
|
try {
|
|
|
|
await _durationCompleter.future;
|
|
|
|
} on MediaError catch (e) {
|
|
|
|
throw PlatformException(
|
|
|
|
code: "${e.code}", message: "Failed to load URL");
|
|
|
|
} finally {
|
|
|
|
_durationCompleter = null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.ready);
|
2020-07-09 03:27:53 +00:00
|
|
|
final seconds = _audioElement.duration;
|
2020-10-10 16:38:01 +00:00
|
|
|
print("loadUri returning");
|
2020-07-09 03:27:53 +00:00
|
|
|
return seconds.isFinite
|
|
|
|
? Duration(milliseconds: (seconds * 1000).toInt())
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<PlayResponse> play(PlayRequest request) async {
|
|
|
|
await _play();
|
|
|
|
return PlayResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _play() async {
|
2020-07-09 03:27:53 +00:00
|
|
|
_playing = true;
|
2020-08-03 14:16:15 +00:00
|
|
|
await _currentAudioSourcePlayer.play();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<PauseResponse> pause(PauseRequest request) async {
|
2020-07-09 03:27:53 +00:00
|
|
|
_playing = false;
|
|
|
|
_currentAudioSourcePlayer.pause();
|
2020-09-27 03:30:30 +00:00
|
|
|
return PauseResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<SetVolumeResponse> setVolume(SetVolumeRequest request) async {
|
|
|
|
_audioElement.volume = request.volume;
|
|
|
|
return SetVolumeResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<SetSpeedResponse> setSpeed(SetSpeedRequest request) async {
|
|
|
|
_audioElement.playbackRate = request.speed;
|
|
|
|
return SetSpeedResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<SetLoopModeResponse> setLoopMode(SetLoopModeRequest request) async {
|
|
|
|
_loopMode = request.loopMode;
|
|
|
|
return SetLoopModeResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<SetShuffleModeResponse> setShuffleMode(
|
|
|
|
SetShuffleModeRequest request) async {
|
|
|
|
_shuffleModeEnabled = request.shuffleMode == ShuffleModeMessage.all;
|
|
|
|
if (_shuffleModeEnabled) {
|
2020-07-09 03:27:53 +00:00
|
|
|
_audioSourcePlayer?.shuffle(0, _index);
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
return SetShuffleModeResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-09-27 03:30:30 +00:00
|
|
|
Future<SeekResponse> seek(SeekRequest request) async {
|
|
|
|
await _seek(request.position.inMilliseconds, request.index);
|
|
|
|
return SeekResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _seek(int position, int newIndex) async {
|
2020-07-09 03:27:53 +00:00
|
|
|
int index = newIndex ?? _index;
|
|
|
|
if (index != _index) {
|
|
|
|
_currentAudioSourcePlayer.pause();
|
|
|
|
_index = index;
|
|
|
|
await _currentAudioSourcePlayer.load();
|
|
|
|
await _currentAudioSourcePlayer.seek(position);
|
|
|
|
if (_playing) {
|
2020-08-03 14:16:15 +00:00
|
|
|
_currentAudioSourcePlayer.play();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await _currentAudioSourcePlayer.seek(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConcatenatingAudioSourcePlayer _concatenating(String playerId) =>
|
|
|
|
_audioSourcePlayers[playerId] as ConcatenatingAudioSourcePlayer;
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
@override
|
|
|
|
Future<ConcatenatingInsertAllResponse> concatenatingInsertAll(
|
|
|
|
ConcatenatingInsertAllRequest request) async {
|
|
|
|
_concatenating(request.id)
|
|
|
|
.insertAll(request.index, getAudioSources(request.children));
|
|
|
|
if (request.index <= _index) {
|
|
|
|
_index += request.children.length;
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
return ConcatenatingInsertAllResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
@override
|
|
|
|
Future<ConcatenatingRemoveRangeResponse> concatenatingRemoveRange(
|
|
|
|
ConcatenatingRemoveRangeRequest request) async {
|
|
|
|
if (_index >= request.startIndex && _index < request.endIndex && _playing) {
|
2020-07-09 03:27:53 +00:00
|
|
|
// Pause if removing current item
|
|
|
|
_currentAudioSourcePlayer.pause();
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
_concatenating(request.id)
|
|
|
|
.removeRange(request.startIndex, request.endIndex);
|
|
|
|
if (_index >= request.startIndex && _index < request.endIndex) {
|
2020-07-09 03:27:53 +00:00
|
|
|
// Skip backward if there's nothing after this
|
2020-09-27 03:30:30 +00:00
|
|
|
if (request.startIndex >= _audioSourcePlayer.sequence.length) {
|
|
|
|
_index = request.startIndex - 1;
|
2020-07-09 03:27:53 +00:00
|
|
|
} else {
|
2020-09-27 03:30:30 +00:00
|
|
|
_index = request.startIndex;
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
// Resume playback at the new item (if it exists)
|
|
|
|
if (_playing && _currentAudioSourcePlayer != null) {
|
|
|
|
await _currentAudioSourcePlayer.load();
|
|
|
|
_currentAudioSourcePlayer.play();
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
} else if (request.endIndex <= _index) {
|
2020-07-09 03:27:53 +00:00
|
|
|
// Reflect that the current item has shifted its position
|
2020-09-27 03:30:30 +00:00
|
|
|
_index -= (request.endIndex - request.startIndex);
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
return ConcatenatingRemoveRangeResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
@override
|
|
|
|
Future<ConcatenatingMoveResponse> concatenatingMove(
|
|
|
|
ConcatenatingMoveRequest request) async {
|
|
|
|
_concatenating(request.id).move(request.currentIndex, request.newIndex);
|
|
|
|
if (request.currentIndex == _index) {
|
|
|
|
_index = request.newIndex;
|
|
|
|
} else if (request.currentIndex < _index && request.newIndex >= _index) {
|
2020-07-09 03:27:53 +00:00
|
|
|
_index--;
|
2020-09-27 03:30:30 +00:00
|
|
|
} else if (request.currentIndex > _index && request.newIndex <= _index) {
|
2020-07-09 03:27:53 +00:00
|
|
|
_index++;
|
|
|
|
}
|
2020-09-27 03:30:30 +00:00
|
|
|
return ConcatenatingMoveResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
@override
|
|
|
|
Future<SetAndroidAudioAttributesResponse> setAndroidAudioAttributes(
|
|
|
|
SetAndroidAudioAttributesRequest request) async {
|
|
|
|
return SetAndroidAudioAttributesResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<SetAutomaticallyWaitsToMinimizeStallingResponse>
|
|
|
|
setAutomaticallyWaitsToMinimizeStalling(
|
|
|
|
SetAutomaticallyWaitsToMinimizeStallingRequest request) async {
|
|
|
|
return SetAutomaticallyWaitsToMinimizeStallingResponse();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Duration getCurrentPosition() => _currentAudioSourcePlayer?.position;
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
@override
|
|
|
|
Duration getBufferedPosition() => _currentAudioSourcePlayer?.bufferedPosition;
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
@override
|
|
|
|
Duration getDuration() => _currentAudioSourcePlayer?.duration;
|
|
|
|
|
|
|
|
@override
|
2020-10-10 16:38:01 +00:00
|
|
|
Future<void> release() async {
|
|
|
|
print("web release");
|
2020-07-09 03:27:53 +00:00
|
|
|
_currentAudioSourcePlayer?.pause();
|
|
|
|
_audioElement.removeAttribute('src');
|
2020-03-07 02:50:59 +00:00
|
|
|
_audioElement.load();
|
2020-09-27 03:30:30 +00:00
|
|
|
transition(ProcessingStateMessage.none);
|
2020-10-10 16:38:01 +00:00
|
|
|
return await super.release();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
List<AudioSourcePlayer> getAudioSources(List messages) =>
|
|
|
|
messages.map((message) => getAudioSource(message)).toList();
|
2020-07-09 03:27:53 +00:00
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
AudioSourcePlayer getAudioSource(AudioSourceMessage audioSourceMessage) {
|
|
|
|
final String id = audioSourceMessage.id;
|
2020-07-09 03:27:53 +00:00
|
|
|
var audioSourcePlayer = _audioSourcePlayers[id];
|
|
|
|
if (audioSourcePlayer == null) {
|
2020-09-27 03:30:30 +00:00
|
|
|
audioSourcePlayer = decodeAudioSource(audioSourceMessage);
|
2020-07-09 03:27:53 +00:00
|
|
|
_audioSourcePlayers[id] = audioSourcePlayer;
|
|
|
|
}
|
|
|
|
return audioSourcePlayer;
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:30:30 +00:00
|
|
|
AudioSourcePlayer decodeAudioSource(AudioSourceMessage audioSourceMessage) {
|
2020-06-09 17:42:36 +00:00
|
|
|
try {
|
2020-09-27 03:30:30 +00:00
|
|
|
if (audioSourceMessage is ProgressiveAudioSourceMessage) {
|
|
|
|
return ProgressiveAudioSourcePlayer(this, audioSourceMessage.id,
|
|
|
|
Uri.parse(audioSourceMessage.uri), audioSourceMessage.headers);
|
|
|
|
} else if (audioSourceMessage is DashAudioSourceMessage) {
|
|
|
|
return DashAudioSourcePlayer(this, audioSourceMessage.id,
|
|
|
|
Uri.parse(audioSourceMessage.uri), audioSourceMessage.headers);
|
|
|
|
} else if (audioSourceMessage is HlsAudioSourceMessage) {
|
|
|
|
return HlsAudioSourcePlayer(this, audioSourceMessage.id,
|
|
|
|
Uri.parse(audioSourceMessage.uri), audioSourceMessage.headers);
|
|
|
|
} else if (audioSourceMessage is ConcatenatingAudioSourceMessage) {
|
|
|
|
return ConcatenatingAudioSourcePlayer(
|
|
|
|
this,
|
|
|
|
audioSourceMessage.id,
|
|
|
|
getAudioSources(audioSourceMessage.children),
|
|
|
|
audioSourceMessage.useLazyPreparation);
|
|
|
|
} else if (audioSourceMessage is ClippingAudioSourceMessage) {
|
|
|
|
return ClippingAudioSourcePlayer(
|
|
|
|
this,
|
|
|
|
audioSourceMessage.id,
|
|
|
|
getAudioSource(audioSourceMessage.child),
|
|
|
|
audioSourceMessage.start,
|
|
|
|
audioSourceMessage.end);
|
|
|
|
} else if (audioSourceMessage is LoopingAudioSourceMessage) {
|
|
|
|
return LoopingAudioSourcePlayer(this, audioSourceMessage.id,
|
|
|
|
getAudioSource(audioSourceMessage.child), audioSourceMessage.count);
|
|
|
|
} else {
|
|
|
|
throw Exception("Unknown AudioSource type: $audioSourceMessage");
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
} catch (e, stacktrace) {
|
|
|
|
print("$stacktrace");
|
|
|
|
rethrow;
|
2020-06-09 17:42:36 +00:00
|
|
|
}
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class AudioSourcePlayer {
|
|
|
|
Html5AudioPlayer html5AudioPlayer;
|
|
|
|
final String id;
|
|
|
|
|
|
|
|
AudioSourcePlayer(this.html5AudioPlayer, this.id);
|
|
|
|
|
|
|
|
List<IndexedAudioSourcePlayer> get sequence;
|
|
|
|
|
|
|
|
List<int> get shuffleOrder;
|
|
|
|
|
|
|
|
int shuffle(int treeIndex, int currentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class IndexedAudioSourcePlayer extends AudioSourcePlayer {
|
|
|
|
IndexedAudioSourcePlayer(Html5AudioPlayer html5AudioPlayer, String id)
|
|
|
|
: super(html5AudioPlayer, id);
|
|
|
|
|
|
|
|
Future<Duration> load();
|
|
|
|
|
|
|
|
Future<void> play();
|
|
|
|
|
|
|
|
Future<void> pause();
|
|
|
|
|
|
|
|
Future<void> seek(int position);
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
Future<void> complete();
|
|
|
|
|
|
|
|
Future<void> timeUpdated(double seconds) async {}
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration get duration;
|
|
|
|
|
|
|
|
Duration get position;
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
Duration get bufferedPosition;
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
AudioElement get _audioElement => html5AudioPlayer._audioElement;
|
2020-03-07 02:50:59 +00:00
|
|
|
|
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
int shuffle(int treeIndex, int currentIndex) => treeIndex + 1;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => "${this.runtimeType}";
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class UriAudioSourcePlayer extends IndexedAudioSourcePlayer {
|
|
|
|
final Uri uri;
|
|
|
|
final Map headers;
|
|
|
|
double _resumePos;
|
|
|
|
Duration _duration;
|
2020-08-03 14:16:15 +00:00
|
|
|
Completer _completer;
|
2020-07-09 03:27:53 +00:00
|
|
|
|
|
|
|
UriAudioSourcePlayer(
|
|
|
|
Html5AudioPlayer html5AudioPlayer, String id, this.uri, this.headers)
|
|
|
|
: super(html5AudioPlayer, id);
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<IndexedAudioSourcePlayer> get sequence => [this];
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<int> get shuffleOrder => [0];
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Duration> load() async {
|
|
|
|
_resumePos = 0.0;
|
|
|
|
return _duration = await html5AudioPlayer.loadUri(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> play() async {
|
|
|
|
_audioElement.currentTime = _resumePos;
|
|
|
|
_audioElement.play();
|
2020-08-03 14:16:15 +00:00
|
|
|
_completer = Completer();
|
|
|
|
await _completer.future;
|
|
|
|
_completer = null;
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> pause() async {
|
|
|
|
_resumePos = _audioElement.currentTime;
|
|
|
|
_audioElement.pause();
|
2020-08-03 14:16:15 +00:00
|
|
|
_interruptPlay();
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> seek(int position) async {
|
|
|
|
_audioElement.currentTime = _resumePos = position / 1000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-08-03 14:16:15 +00:00
|
|
|
Future<void> complete() async {
|
|
|
|
_interruptPlay();
|
|
|
|
html5AudioPlayer.onEnded();
|
|
|
|
}
|
|
|
|
|
|
|
|
_interruptPlay() {
|
|
|
|
if (_completer?.isCompleted == false) {
|
|
|
|
_completer.complete();
|
|
|
|
}
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Duration get duration {
|
|
|
|
return _duration;
|
|
|
|
//final seconds = _audioElement.duration;
|
|
|
|
//return seconds.isFinite
|
|
|
|
// ? Duration(milliseconds: (seconds * 1000).toInt())
|
|
|
|
// : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Duration get position {
|
|
|
|
double seconds = _audioElement.currentTime;
|
|
|
|
return Duration(milliseconds: (seconds * 1000).toInt());
|
|
|
|
}
|
2020-08-03 14:16:15 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Duration get bufferedPosition {
|
|
|
|
if (_audioElement.buffered.length > 0) {
|
|
|
|
return Duration(
|
|
|
|
milliseconds:
|
|
|
|
(_audioElement.buffered.end(_audioElement.buffered.length - 1) *
|
|
|
|
1000)
|
|
|
|
.toInt());
|
|
|
|
} else {
|
|
|
|
return Duration.zero;
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ProgressiveAudioSourcePlayer extends UriAudioSourcePlayer {
|
|
|
|
ProgressiveAudioSourcePlayer(
|
|
|
|
Html5AudioPlayer html5AudioPlayer, String id, Uri uri, Map headers)
|
|
|
|
: super(html5AudioPlayer, id, uri, headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DashAudioSourcePlayer extends UriAudioSourcePlayer {
|
|
|
|
DashAudioSourcePlayer(
|
|
|
|
Html5AudioPlayer html5AudioPlayer, String id, Uri uri, Map headers)
|
|
|
|
: super(html5AudioPlayer, id, uri, headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
class HlsAudioSourcePlayer extends UriAudioSourcePlayer {
|
|
|
|
HlsAudioSourcePlayer(
|
|
|
|
Html5AudioPlayer html5AudioPlayer, String id, Uri uri, Map headers)
|
|
|
|
: super(html5AudioPlayer, id, uri, headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConcatenatingAudioSourcePlayer extends AudioSourcePlayer {
|
|
|
|
static List<int> generateShuffleOrder(int length, [int firstIndex]) {
|
|
|
|
final shuffleOrder = List<int>(length);
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
final j = _random.nextInt(i + 1);
|
|
|
|
shuffleOrder[i] = shuffleOrder[j];
|
|
|
|
shuffleOrder[j] = i;
|
|
|
|
}
|
|
|
|
if (firstIndex != null) {
|
|
|
|
for (var i = 1; i < length; i++) {
|
|
|
|
if (shuffleOrder[i] == firstIndex) {
|
|
|
|
final v = shuffleOrder[0];
|
|
|
|
shuffleOrder[0] = shuffleOrder[i];
|
|
|
|
shuffleOrder[i] = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shuffleOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<AudioSourcePlayer> audioSourcePlayers;
|
|
|
|
final bool useLazyPreparation;
|
|
|
|
List<int> _shuffleOrder;
|
|
|
|
|
|
|
|
ConcatenatingAudioSourcePlayer(Html5AudioPlayer html5AudioPlayer, String id,
|
|
|
|
this.audioSourcePlayers, this.useLazyPreparation)
|
|
|
|
: _shuffleOrder = generateShuffleOrder(audioSourcePlayers.length),
|
|
|
|
super(html5AudioPlayer, id);
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<IndexedAudioSourcePlayer> get sequence =>
|
|
|
|
audioSourcePlayers.expand((p) => p.sequence).toList();
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<int> get shuffleOrder {
|
|
|
|
final order = <int>[];
|
|
|
|
var offset = order.length;
|
|
|
|
final childOrders = <List<int>>[];
|
|
|
|
for (var audioSourcePlayer in audioSourcePlayers) {
|
|
|
|
final childShuffleOrder = audioSourcePlayer.shuffleOrder;
|
|
|
|
childOrders.add(childShuffleOrder.map((i) => i + offset).toList());
|
|
|
|
offset += childShuffleOrder.length;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < childOrders.length; i++) {
|
|
|
|
order.addAll(childOrders[_shuffleOrder[i]]);
|
|
|
|
}
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int shuffle(int treeIndex, int currentIndex) {
|
|
|
|
int currentChildIndex;
|
|
|
|
for (var i = 0; i < audioSourcePlayers.length; i++) {
|
|
|
|
final indexBefore = treeIndex;
|
|
|
|
final child = audioSourcePlayers[i];
|
|
|
|
treeIndex = child.shuffle(treeIndex, currentIndex);
|
|
|
|
if (currentIndex >= indexBefore && currentIndex < treeIndex) {
|
|
|
|
currentChildIndex = i;
|
|
|
|
} else {}
|
|
|
|
}
|
|
|
|
// Shuffle so that the current child is first in the shuffle order
|
|
|
|
_shuffleOrder =
|
|
|
|
generateShuffleOrder(audioSourcePlayers.length, currentChildIndex);
|
|
|
|
return treeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
insertAll(int index, List<AudioSourcePlayer> players) {
|
|
|
|
audioSourcePlayers.insertAll(index, players);
|
|
|
|
for (var i = 0; i < audioSourcePlayers.length; i++) {
|
|
|
|
if (_shuffleOrder[i] >= index) {
|
|
|
|
_shuffleOrder[i] += players.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_shuffleOrder.addAll(
|
|
|
|
List.generate(players.length, (i) => index + i).toList()..shuffle());
|
|
|
|
}
|
|
|
|
|
|
|
|
removeRange(int start, int end) {
|
|
|
|
audioSourcePlayers.removeRange(start, end);
|
|
|
|
for (var i = 0; i < audioSourcePlayers.length; i++) {
|
|
|
|
if (_shuffleOrder[i] >= end) {
|
|
|
|
_shuffleOrder[i] -= (end - start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_shuffleOrder.removeWhere((i) => i >= start && i < end);
|
|
|
|
}
|
|
|
|
|
|
|
|
move(int currentIndex, int newIndex) {
|
|
|
|
audioSourcePlayers.insert(
|
|
|
|
newIndex, audioSourcePlayers.removeAt(currentIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClippingAudioSourcePlayer extends IndexedAudioSourcePlayer {
|
|
|
|
final UriAudioSourcePlayer audioSourcePlayer;
|
|
|
|
final Duration start;
|
|
|
|
final Duration end;
|
2020-08-03 14:16:15 +00:00
|
|
|
Completer<ClipInterruptReason> _completer;
|
2020-07-09 03:27:53 +00:00
|
|
|
double _resumePos;
|
|
|
|
Duration _duration;
|
|
|
|
|
|
|
|
ClippingAudioSourcePlayer(Html5AudioPlayer html5AudioPlayer, String id,
|
|
|
|
this.audioSourcePlayer, this.start, this.end)
|
|
|
|
: super(html5AudioPlayer, id);
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<IndexedAudioSourcePlayer> get sequence => [this];
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<int> get shuffleOrder => [0];
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Duration> load() async {
|
2020-08-03 14:16:15 +00:00
|
|
|
_resumePos = (start ?? Duration.zero).inMilliseconds / 1000.0;
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration fullDuration =
|
|
|
|
await html5AudioPlayer.loadUri(audioSourcePlayer.uri);
|
|
|
|
_audioElement.currentTime = _resumePos;
|
|
|
|
_duration = Duration(
|
2020-08-03 14:16:15 +00:00
|
|
|
milliseconds: min((end ?? fullDuration).inMilliseconds,
|
|
|
|
fullDuration.inMilliseconds) -
|
|
|
|
(start ?? Duration.zero).inMilliseconds);
|
2020-07-09 03:27:53 +00:00
|
|
|
return _duration;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
double get remaining => end.inMilliseconds / 1000 - _audioElement.currentTime;
|
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
@override
|
|
|
|
Future<void> play() async {
|
2020-08-03 14:16:15 +00:00
|
|
|
_interruptPlay(ClipInterruptReason.simultaneous);
|
2020-07-09 03:27:53 +00:00
|
|
|
_audioElement.currentTime = _resumePos;
|
2020-03-07 02:50:59 +00:00
|
|
|
_audioElement.play();
|
2020-08-03 14:16:15 +00:00
|
|
|
_completer = Completer<ClipInterruptReason>();
|
|
|
|
ClipInterruptReason reason;
|
|
|
|
while ((reason = await _completer.future) == ClipInterruptReason.seek) {
|
|
|
|
_completer = Completer<ClipInterruptReason>();
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
2020-08-03 14:16:15 +00:00
|
|
|
if (reason == ClipInterruptReason.end) {
|
|
|
|
html5AudioPlayer.onEnded();
|
|
|
|
}
|
|
|
|
_completer = null;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> pause() async {
|
2020-08-03 14:16:15 +00:00
|
|
|
_interruptPlay(ClipInterruptReason.pause);
|
2020-07-09 03:27:53 +00:00
|
|
|
_resumePos = _audioElement.currentTime;
|
2020-03-07 02:50:59 +00:00
|
|
|
_audioElement.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
Future<void> seek(int position) async {
|
2020-08-03 14:16:15 +00:00
|
|
|
_interruptPlay(ClipInterruptReason.seek);
|
2020-07-09 03:27:53 +00:00
|
|
|
_audioElement.currentTime =
|
|
|
|
_resumePos = start.inMilliseconds / 1000.0 + position / 1000.0;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-08-03 14:16:15 +00:00
|
|
|
Future<void> complete() async {
|
|
|
|
_interruptPlay(ClipInterruptReason.end);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> timeUpdated(double seconds) async {
|
|
|
|
if (end != null) {
|
|
|
|
if (seconds >= end.inMilliseconds / 1000) {
|
|
|
|
_interruptPlay(ClipInterruptReason.end);
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration get duration {
|
|
|
|
return _duration;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
Duration get position {
|
|
|
|
double seconds = _audioElement.currentTime;
|
|
|
|
var position = Duration(milliseconds: (seconds * 1000).toInt());
|
|
|
|
if (start != null) {
|
|
|
|
position -= start;
|
|
|
|
}
|
|
|
|
if (position < Duration.zero) {
|
|
|
|
position = Duration.zero;
|
|
|
|
}
|
|
|
|
return position;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
@override
|
|
|
|
Duration get bufferedPosition {
|
|
|
|
if (_audioElement.buffered.length > 0) {
|
|
|
|
var seconds =
|
|
|
|
_audioElement.buffered.end(_audioElement.buffered.length - 1);
|
|
|
|
var position = Duration(milliseconds: (seconds * 1000).toInt());
|
|
|
|
if (start != null) {
|
|
|
|
position -= start;
|
|
|
|
}
|
|
|
|
if (position < Duration.zero) {
|
|
|
|
position = Duration.zero;
|
|
|
|
}
|
|
|
|
if (duration != null && position > duration) {
|
|
|
|
position = duration;
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
} else {
|
|
|
|
return Duration.zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_interruptPlay(ClipInterruptReason reason) {
|
|
|
|
if (_completer?.isCompleted == false) {
|
|
|
|
_completer.complete(reason);
|
|
|
|
}
|
2020-07-09 03:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:16:15 +00:00
|
|
|
enum ClipInterruptReason { end, pause, seek, simultaneous }
|
|
|
|
|
2020-07-09 03:27:53 +00:00
|
|
|
class LoopingAudioSourcePlayer extends AudioSourcePlayer {
|
|
|
|
final AudioSourcePlayer audioSourcePlayer;
|
|
|
|
final int count;
|
|
|
|
|
|
|
|
LoopingAudioSourcePlayer(Html5AudioPlayer html5AudioPlayer, String id,
|
|
|
|
this.audioSourcePlayer, this.count)
|
|
|
|
: super(html5AudioPlayer, id);
|
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
List<IndexedAudioSourcePlayer> get sequence =>
|
|
|
|
List.generate(count, (i) => audioSourcePlayer)
|
|
|
|
.expand((p) => p.sequence)
|
|
|
|
.toList();
|
2020-03-07 02:50:59 +00:00
|
|
|
|
2020-06-09 17:42:36 +00:00
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
List<int> get shuffleOrder {
|
|
|
|
final order = <int>[];
|
|
|
|
var offset = order.length;
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
final childShuffleOrder = audioSourcePlayer.shuffleOrder;
|
|
|
|
order.addAll(childShuffleOrder.map((i) => i + offset).toList());
|
|
|
|
offset += childShuffleOrder.length;
|
|
|
|
}
|
|
|
|
return order;
|
|
|
|
}
|
2020-06-09 17:42:36 +00:00
|
|
|
|
2020-03-07 02:50:59 +00:00
|
|
|
@override
|
2020-07-09 03:27:53 +00:00
|
|
|
int shuffle(int treeIndex, int currentIndex) {
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
treeIndex = audioSourcePlayer.shuffle(treeIndex, currentIndex);
|
|
|
|
}
|
|
|
|
return treeIndex;
|
2020-03-07 02:50:59 +00:00
|
|
|
}
|
|
|
|
}
|