Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): handle infinite value on getDuration for live streams #1287

Merged
merged 5 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/audioplayers_web/lib/audioplayers_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:audioplayers_platform_interface/api/player_mode.dart';
import 'package:audioplayers_platform_interface/api/release_mode.dart';
import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart';
import 'package:audioplayers_platform_interface/streams_interface.dart';
import 'package:audioplayers_web/num_extension.dart';
import 'package:audioplayers_web/wrapped_player.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

Expand Down Expand Up @@ -33,11 +34,11 @@ class AudioplayersPlugin extends AudioplayersPlatform with StreamsInterface {

@override
Future<int?> getDuration(String playerId) async {
final duration = getOrCreatePlayer(playerId).player?.duration;
if (duration == null) {
final jsDuration = getOrCreatePlayer(playerId).player?.duration;
if (jsDuration == null) {
return null;
}
return (duration * 1000).toInt();
return jsDuration.fromSecondsToDuration().inMilliseconds;
}

@override
Expand Down
6 changes: 6 additions & 0 deletions packages/audioplayers_web/lib/num_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension NumExtension on num {
/// Converts [num] (expected in seconds) to the duration.
Duration fromSecondsToDuration() => Duration(
seconds: (isNaN || isInfinite ? 0 : this).round(),
);
}
20 changes: 13 additions & 7 deletions packages/audioplayers_web/lib/wrapped_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:html';

import 'package:audioplayers_platform_interface/api/release_mode.dart';
import 'package:audioplayers_platform_interface/streams_interface.dart';
import 'package:audioplayers_web/num_extension.dart';

class WrappedPlayer {
final String playerId;
Expand Down Expand Up @@ -55,23 +56,28 @@ class WrappedPlayer {
if (currentUrl == null) {
return;
}
Duration toDuration(num jsNum) => Duration(
milliseconds:
(1000 * (jsNum.isNaN || jsNum.isInfinite ? 0 : jsNum)).round(),
);

final p = player = AudioElement(currentUrl);
p.loop = shouldLoop();
p.volume = currentVolume;
p.playbackRate = currentPlaybackRate;
playerPlaySubscription = p.onPlay.listen((_) {
streamsInterface.emitDuration(playerId, toDuration(p.duration));
streamsInterface.emitDuration(
playerId,
p.duration.fromSecondsToDuration(),
);
});
playerLoadedDataSubscription = p.onLoadedData.listen((_) {
streamsInterface.emitDuration(playerId, toDuration(p.duration));
streamsInterface.emitDuration(
playerId,
p.duration.fromSecondsToDuration(),
);
});
playerTimeUpdateSubscription = p.onTimeUpdate.listen((_) {
streamsInterface.emitPosition(playerId, toDuration(p.currentTime));
streamsInterface.emitPosition(
playerId,
p.currentTime.fromSecondsToDuration(),
);
});
playerSeekedSubscription = p.onSeeked.listen((_) {
streamsInterface.emitSeekComplete(playerId);
Expand Down