Comments (15)
@nsilina Yes, with the link provided by @ryanheise I came up with the following implementation:
@override
Future<void> onStart(Map<String, dynamic> params) async {
_playerStateSubscription = _audioPlayer.playbackStateStream
.where((state) => state == AudioPlaybackState.completed)
.listen((state) {
_handlePlaybackCompleted();
});
_eventSubscription = _audioPlayer.playbackEventStream.listen((event) {
var state = event.buffering ? AudioProcessingState.buffering : AudioProcessingState.ready;
switch (event.state) {
case AudioPlaybackState.paused:
_setState(processingState: state, position: event.position);
break;
case AudioPlaybackState.playing:
_setState(processingState: state, position: event.position);
break;
case AudioPlaybackState.connecting:
_setState(processingState: AudioProcessingState.connecting);
break;
default:
break;
}
}, onError: (e) async {
if (e is PlatformException && e.code == "abort" && e.message == "Connection aborted") return;
_error = true;
if (_audioPlayer.playbackState != AudioPlaybackState.none) {
await _audioPlayer.stop();
}
await _setState(processingState: AudioProcessingState.error);
});
}
from just_audio.
@ryanheise I currently have also trouble to catch errors. Currently just testing on android. How do I catch asynchronous errors?
For example, if I play an url audio file and turn off the internet connection and do a seek after that, I only get the following in the console:
E/ExoPlayerImplInternal(11872): Source error E/ExoPlayerImplInternal(11872): UnknownHostException (no network) E/AudioPlayer(11872): TYPE_SOURCE: Unable to connect E/flutter (11872): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(0, com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect, null)
But I don't know how to catch it in my code. Could you give an advice, please? I cannot find an error attribute in any streams or methods.
Edit: Just tested further, AudioPlaybackState
is still showing AudioPlaybackState.playing
or AudioPlaybackState.paused
and the play
method also does not throw errors.
from just_audio.
Hi @cnttechio
The play
method returns a future which completes when playback completes which means you can simply await that future.
If a method fails, it should throw an exception, although this has so far been implemented only on the Android side. The iOS side does not yet handle the error cases.
Let me know if the above works for you on the Android side, and I will do more work on the iOS side.
from just_audio.
Hi @ryanheise .
So cool, i got it, it working fine on the Android side. I will wait for you do more work on the iOS side.
Many thanks
from just_audio.
@ryanheise How if we want to do some action after play
method success ? where we gonna put the function since the play
method await until paused or stop.
from just_audio.
@ariefwijaya , almost all errors can be caught on the initial load (during setUrl
) and the rest can be caught asynchronously during playback since the errors are also emitted on the stream. On iOS I've currently only implemented the first category of errors on setUrl
but Android also reports asynchronous errors.
You should not expect play
to fail immediately since the prior call to setUrl
would have already started reading and buffering audio in order to at least determine the duration of the audio.
from just_audio.
@ryanheise Okay, Thankyou for detail explanation. So, we can determine the play event will be failed or not in setUrl callback,
from just_audio.
Hi @Sauceee
The Dart tutorial on streams may be helpful with regards to error handling: https://dart.dev/tutorials/language/streams
Edit: Just tested further, AudioPlaybackState is still showing AudioPlaybackState.playing or AudioPlaybackState.paused and the play method also does not throw errors.
Updating the playing state and also having play
throw an exception both sound like good ideas. I'll consider them for the future.
from just_audio.
@ryanheise Thank you, that helped!
from just_audio.
@ryanheise I currently have also trouble to catch errors. Currently just testing on android. How do I catch asynchronous errors?
For example, if I play an url audio file and turn off the internet connection and do a seek after that, I only get the following in the console:
E/ExoPlayerImplInternal(11872): Source error E/ExoPlayerImplInternal(11872): UnknownHostException (no network) E/AudioPlayer(11872): TYPE_SOURCE: Unable to connect E/flutter (11872): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(0, com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect, null)
But I don't know how to catch it in my code. Could you give an advice, please? I cannot find an error attribute in any streams or methods.
Edit: Just tested further,
AudioPlaybackState
is still showingAudioPlaybackState.playing
orAudioPlaybackState.paused
and theplay
method also does not throw errors.
@Sauceee , Did you find any solution? Unfortunately, I have the same problem
from just_audio.
@nsilina If you you're just playing a simple audio file, not a stream, you can do this:
try {
await audioPlayer.setUrl(....);
} catch (e) {
...
}
If you're playing a stream, errors can occur at any time after the initial loading, so you need to listen to errors on the stream (see the link I provided above if you need to learn more about streams).
from just_audio.
@Sauceee , thank you
from just_audio.
Just to update on the iOS side of things, catching errors on the setUrl
future has been working already for a while and is available in the current release.
As for handling asynchronous errors on the stream, this is also now implemented on the media-source branch. Issues on this branch are being tracked on #131 so if you discover any issues, please report there. Note that #131 bundles a significant number of features and therefore needs some testing. There is also a planned API change to watch out for in which I intend to make just_audio's state model more closely align with audio_service in terms of buffering.
I will close this issue, and will use #131 for tracking any bugs related to the media-source branch.
from just_audio.
@nsilina Yes, with the link provided by @ryanheise I came up with the following implementation:
@override Future<void> onStart(Map<String, dynamic> params) async { _playerStateSubscription = _audioPlayer.playbackStateStream .where((state) => state == AudioPlaybackState.completed) .listen((state) { _handlePlaybackCompleted(); }); _eventSubscription = _audioPlayer.playbackEventStream.listen((event) { var state = event.buffering ? AudioProcessingState.buffering : AudioProcessingState.ready; switch (event.state) { case AudioPlaybackState.paused: _setState(processingState: state, position: event.position); break; case AudioPlaybackState.playing: _setState(processingState: state, position: event.position); break; case AudioPlaybackState.connecting: _setState(processingState: AudioProcessingState.connecting); break; default: break; } }, onError: (e) async { if (e is PlatformException && e.code == "abort" && e.message == "Connection aborted") return; _error = true; if (_audioPlayer.playbackState != AudioPlaybackState.none) { await _audioPlayer.stop(); } await _setState(processingState: AudioProcessingState.error); }); }
@ryanheise @Sauceee when I try this await _setState(processingState: AudioProcessingState.error);
it changes to error state for a brief moment and then switches back to AudioProcessingState.ready
What can I do for this?
from just_audio.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with just_audio.
from just_audio.
Related Issues (20)
- [Android] Sound stops playing when backgrounding app and letting phone go to sleep HOT 2
- Track completes too eary HOT 10
- "Getting 'type 'Null' is not a subtype of type 'MediaItem' error with Just Audio Background package' HOT 2
- 22 kHz mono mp3s have wrong duration and seek behavior HOT 7
- Error during file preparation on Android 6 when playing MP3 files: "setConfig(1 .mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)" HOT 2
- Error during file preparation on Android 6 when playing MP3 files HOT 4
- Compile WEB release plugin dosent work HOT 2
- Compile WEB release plugin dosent work HOT 6
- Sound is not played on iOS 17.5.1 in background HOT 2
- PlatformException(561017449, The operation couldn't be completed. (OSStatus error 561017449.), null, null) 👉 IOS HOT 2
- Support rxdart 0.28.0
- PlatformException(561017449, The operation couldn't be completed. (OSStatus error 561017449.), null, null) //IOS BUG HOT 2
- Incorrect AudioPlayer Position data on Android HOT 1
- Command failed for android_media_AudioSystem_error_callback: -32, AudioFlinger server died & AudioPolicyService server died. HOT 1
- Error when streaming on iOS or MacOS HOT 1
- AudioSource.uri does not set dynamically headers HOT 1
- Pause ,Stop or any other functions not working while play function called HOT 1
- No longer maintained packages in the readme HOT 2
- sound cuts out HOT 1
- How can get the current playing position? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from just_audio.