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

feat: get current source #1350

Merged
merged 7 commits into from Dec 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/audioplayers/lib/src/audioplayer.dart
@@ -1,4 +1,5 @@
import 'dart:async';

// TODO(gustl22): remove when upgrading min Flutter version to >=3.3.0
// ignore: unnecessary_import
import 'dart:typed_data';
Expand Down Expand Up @@ -29,6 +30,10 @@ class AudioPlayer {

PlayerState get state => _playerState;

Source? _source;

Source? get source => _source;

set state(PlayerState state) {
if (!_playerStateController.isClosed) {
_playerStateController.add(state);
Expand Down Expand Up @@ -94,6 +99,9 @@ class AudioPlayer {
AudioPlayer({String? playerId}) : playerId = playerId ?? _uuid.v4() {
_onPlayerCompleteStreamSubscription = onPlayerComplete.listen((_) {
state = PlayerState.completed;
if (releaseMode == ReleaseMode.release) {
_source = null;
}
});
}

Expand Down Expand Up @@ -164,6 +172,7 @@ class AudioPlayer {
Future<void> release() async {
await _platform.release(playerId);
state = PlayerState.stopped;
_source = null;
}

/// Moves the cursor to the desired position.
Expand Down Expand Up @@ -217,6 +226,7 @@ class AudioPlayer {
/// The resources will start being fetched or buffered as soon as you call
/// this method.
Future<void> setSourceUrl(String url) {
_source = UrlSource(url);
return _platform.setSourceUrl(playerId, url, isLocal: false);
}

Expand All @@ -225,6 +235,7 @@ class AudioPlayer {
/// The resources will start being fetched or buffered as soon as you call
/// this method.
Future<void> setSourceDeviceFile(String path) {
_source = DeviceFileSource(path);
return _platform.setSourceUrl(playerId, path, isLocal: true);
}

Expand All @@ -234,11 +245,13 @@ class AudioPlayer {
/// The resources will start being fetched or buffered as soon as you call
/// this method.
Future<void> setSourceAsset(String path) async {
_source = AssetSource(path);
final url = await audioCache.load(path);
return _platform.setSourceUrl(playerId, url.path, isLocal: true);
}

Future<void> setSourceBytes(Uint8List bytes) {
_source = BytesSource(bytes);
return _platform.setSourceBytes(playerId, bytes);
}

Expand Down Expand Up @@ -277,6 +290,8 @@ class AudioPlayer {
_onPlayerCompleteStreamSubscription.cancel()
];

_source = null;

await Future.wait<dynamic>(futures);
}
}