Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void onMethodCall(MethodCall call, Result result) {
}
// All subsequent calls need a valid player.
ManagedMediaPlayer player = getAndVerifyPlayer(call, result);

if (call.method.equals(PLAY_METHOD)) {
Boolean playFromStartBoolean = call.argument(PLAY_FROM_START);
boolean playFromStart = playFromStartBoolean.booleanValue();
Expand Down Expand Up @@ -135,6 +136,7 @@ private void onLoad(MethodCall call, Result result) {
String key = registrar.lookupKeyForAsset(flutterPath);
AssetFileDescriptor fd = assetManager.openFd(key);
ManagedMediaPlayer newPlayer = new LocalManagedMediaPlayer(audioId, fd, this, looping);
fd.close();
mediaPlayers.put(audioId, newPlayer);
handleDurationForPlayer(newPlayer, audioId);
result.success(null);
Expand Down
74 changes: 51 additions & 23 deletions packages/audiofileplayer/lib/audiofileplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,21 @@ class Audio with WidgetsBindingObserver {
audioIdKey: _audioId,
loopingKey: _looping
});
} on PlatformException catch (_) {
} on PlatformException catch (e) {
// Note that exceptions during [_load] are assumed to have failed to
// create underlying resources, so a call to [_releaseNative] is not
// required. Just remove the instance from the static structures it was
// added to within this call to [_load].
_undisposedAudios.remove(_audioId);
_awaitingOnDurationAudios.remove(_audioId);
// If this Audio does not use an onError callback, rethrow the exception.
if (_usingOnErrorAudios.remove(_audioId) == null) rethrow;
final Audio audio = _usingOnErrorAudios.remove(_audioId);
if (audio != null) {
// Audio has an onError callback.
audio._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand Down Expand Up @@ -486,9 +492,14 @@ class Audio with WidgetsBindingObserver {
audioIdKey: _audioId,
positionSecondsKey: positionSeconds
});
} on PlatformException catch (_) {
// If this Audio does not use an onError callback, rethrow the exception.
if (!_usingOnErrorAudios.containsKey(_audioId)) rethrow;
} on PlatformException catch (e) {
if (_usingOnErrorAudios.containsKey(_audioId)) {
// Audio has an onError callback.
_usingOnErrorAudios[_audioId]._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand All @@ -515,9 +526,14 @@ class Audio with WidgetsBindingObserver {
try {
await _sendMethodCall(_audioId, setVolumeMethod,
<String, dynamic>{audioIdKey: _audioId, volumeKey: volume});
} on PlatformException catch (_) {
// If this Audio does not use an onError callback, rethrow the exception.
if (!_usingOnErrorAudios.containsKey(_audioId)) rethrow;
} on PlatformException catch (e) {
if (_usingOnErrorAudios.containsKey(_audioId)) {
// Audio has an onError callback.
_usingOnErrorAudios[_audioId]._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand Down Expand Up @@ -565,9 +581,14 @@ class Audio with WidgetsBindingObserver {
};
if (endpointSeconds != null) args[endpointSecondsKey] = endpointSeconds;
await _sendMethodCall(_audioId, playMethod, args);
} on PlatformException catch (_) {
// If this Audio does not use an onError callback, rethrow the exception.
if (!_usingOnErrorAudios.containsKey(_audioId)) rethrow;
} on PlatformException catch (e) {
if (_usingOnErrorAudios.containsKey(_audioId)) {
// Audio has an onError callback.
_usingOnErrorAudios[_audioId]._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand All @@ -576,9 +597,14 @@ class Audio with WidgetsBindingObserver {
try {
await _sendMethodCall(
_audioId, pauseMethod, <String, dynamic>{audioIdKey: _audioId});
} on PlatformException catch (_) {
// If this Audio does not use an onError callback, rethrow the exception.
if (!_usingOnErrorAudios.containsKey(_audioId)) rethrow;
} on PlatformException catch (e) {
if (_usingOnErrorAudios.containsKey(_audioId)) {
// Audio has an onError callback.
_usingOnErrorAudios[_audioId]._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand Down Expand Up @@ -627,9 +653,14 @@ class Audio with WidgetsBindingObserver {
try {
await _sendMethodCall(
audioId, releaseMethod, <String, dynamic>{audioIdKey: audioId});
} on PlatformException catch (_) {
// If this Audio does not use an onError callback, rethrow the exception.
if (!_usingOnErrorAudios.containsKey(audioId)) rethrow;
} on PlatformException catch (e) {
if (_usingOnErrorAudios.containsKey(audioId)) {
// Audio has an onError callback.
_usingOnErrorAudios[audioId]._onError(e.message);
} else {
// Audio does not use an onError callback: rethrow the exception.
rethrow;
}
}
}

Expand All @@ -643,11 +674,8 @@ class Audio with WidgetsBindingObserver {
} on PlatformException catch (e) {
_logger.severe(
'_sendMethodCall error: audioId: $audioId method: $method', e);

// Call onError on the Audio instance.
_usingOnErrorAudios[audioId]?._onError(e.message);
// Rethrow to the calling Audio method. Callers should not rethrow if
// this instance of Audio uses onError().
// Calling methods should do any cleanup. Then, either call the _onError
// callback (if the audio uses it), or rethrow again.
rethrow;
}
}
Expand Down