Report errors and duration stream on web

This commit is contained in:
Ryan Heise 2020-06-10 03:42:36 +10:00
parent f0ad4cdd50
commit 4f383b9393
2 changed files with 32 additions and 9 deletions

View File

@ -9,16 +9,15 @@ A Flutter plugin to play audio from URLs, files, assets and DASH/HLS streams. Th
| read from URL | ✅ | ✅ | ✅ | ✅ | | read from URL | ✅ | ✅ | ✅ | ✅ |
| read from file | ✅ | ✅ | ✅ | | | read from file | ✅ | ✅ | ✅ | |
| read from asset | ✅ | ✅ | ✅ | | | read from asset | ✅ | ✅ | ✅ | |
| pass headers | ✅ | ✅ | ✅ | | | request headers | ✅ | ✅ | ✅ | |
| DASH | ✅ | (untested) | (untested) | (untested) | | DASH | ✅ | (untested) | (untested) | (untested) |
| HLS | ✅ | ✅ | (untested) | (untested) | | HLS | ✅ | ✅ | (untested) | (untested) |
| play/pause/stop/seek | ✅ | ✅ | ✅ | ✅ | | play/pause/stop/seek | ✅ | ✅ | ✅ | ✅ |
| set volume | ✅ | (untested) | (untested) | (untested) | | set volume | ✅ | (untested) | (untested) | |
| set speed | ✅ | ✅ | ✅ | ✅ | | set speed | ✅ | ✅ | ✅ | ✅ |
| clip audio | ✅ | | | ✅ | | clip audio | ✅ | | | ✅ |
| dispose | ✅ | ✅ | ✅ | ✅ | | dispose | ✅ | ✅ | ✅ | ✅ |
| report player errors | ✅ | ✅ | ✅ | | | report player errors | ✅ | ✅ | ✅ | ✅ |
| request headers | ✅ | ✅ | ✅ | |
This plugin has been tested on Android and Web, and is being made available for testing on iOS. Please consider reporting any bugs you encounter [here](https://github.com/ryanheise/just_audio/issues) or submitting pull requests [here](https://github.com/ryanheise/just_audio/pulls). This plugin has been tested on Android and Web, and is being made available for testing on iOS. Please consider reporting any bugs you encounter [here](https://github.com/ryanheise/just_audio/issues) or submitting pull requests [here](https://github.com/ryanheise/just_audio/pulls).

View File

@ -102,15 +102,27 @@ abstract class JustAudioPlayer {
double getCurrentPosition(); double getCurrentPosition();
int getCurrentPositionMs() => (getCurrentPosition() * 1000).toInt();
double getDuration();
int getDurationMs() {
final duration = getDuration();
return duration.isFinite ? (duration * 1000).toInt() : -1;
}
broadcastPlaybackEvent() { broadcastPlaybackEvent() {
var updateTime = DateTime.now().millisecondsSinceEpoch; var updateTime = DateTime.now().millisecondsSinceEpoch;
eventController.add([ eventController.add([
_state.index, _state.index,
_buffering, _buffering,
(getCurrentPosition() * 1000).toInt(), getCurrentPositionMs(),
updateTime, updateTime,
// TODO: buffered position // TODO: buffered position
(getCurrentPosition() * 1000).toInt(), getCurrentPositionMs(),
// TODO: Icy Metadata
null,
getDurationMs(),
]); ]);
} }
@ -131,7 +143,10 @@ class Html5AudioPlayer extends JustAudioPlayer {
Html5AudioPlayer({@required String id, @required Registrar registrar}) Html5AudioPlayer({@required String id, @required Registrar registrar})
: super(id: id, registrar: registrar) { : super(id: id, registrar: registrar) {
_audioElement.addEventListener('durationchange', (event) { _audioElement.addEventListener('durationchange', (event) {
_durationCompleter?.complete(_audioElement.duration); _durationCompleter?.complete(getDuration());
});
_audioElement.addEventListener('error', (event) {
_durationCompleter?.completeError(_audioElement.error);
}); });
_audioElement.addEventListener('ended', (event) { _audioElement.addEventListener('ended', (event) {
transition(AudioPlaybackState.completed); transition(AudioPlaybackState.completed);
@ -154,9 +169,15 @@ class Html5AudioPlayer extends JustAudioPlayer {
_audioElement.src = url; _audioElement.src = url;
_audioElement.preload = 'auto'; _audioElement.preload = 'auto';
_audioElement.load(); _audioElement.load();
final duration = await _durationCompleter.future; try {
await _durationCompleter.future;
} on MediaError catch (e) {
throw PlatformException(code: "${e.code}", message: "Failed to load URL");
} finally {
_durationCompleter = null;
}
transition(AudioPlaybackState.stopped); transition(AudioPlaybackState.stopped);
return (duration * 1000).toInt(); return getDurationMs();
} }
@override @override
@ -229,6 +250,9 @@ class Html5AudioPlayer extends JustAudioPlayer {
@override @override
double getCurrentPosition() => _audioElement.currentTime; double getCurrentPosition() => _audioElement.currentTime;
@override
double getDuration() => _audioElement.duration;
@override @override
void dispose() { void dispose() {
_interruptPlay(); _interruptPlay();