Giter Site home page Giter Site logo

Comments (15)

Sauceee avatar Sauceee commented on July 4, 2024 3

@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.

Sauceee avatar Sauceee commented on July 4, 2024 1

@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.

ryanheise avatar ryanheise commented on July 4, 2024

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.

cnttechio avatar cnttechio commented on July 4, 2024

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.

ariefwijaya avatar ariefwijaya commented on July 4, 2024

@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.

ryanheise avatar ryanheise commented on July 4, 2024

@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.

ariefwijaya avatar ariefwijaya commented on July 4, 2024

@ryanheise Okay, Thankyou for detail explanation. So, we can determine the play event will be failed or not in setUrl callback,

from just_audio.

ryanheise avatar ryanheise commented on July 4, 2024

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.

Sauceee avatar Sauceee commented on July 4, 2024

@ryanheise Thank you, that helped!

from just_audio.

na-si avatar na-si commented on July 4, 2024

@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.

@Sauceee , Did you find any solution? Unfortunately, I have the same problem

from just_audio.

ryanheise avatar ryanheise commented on July 4, 2024

@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.

na-si avatar na-si commented on July 4, 2024

@Sauceee , thank you

from just_audio.

ryanheise avatar ryanheise commented on July 4, 2024

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.

parthghedia avatar parthghedia commented on July 4, 2024

@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.

github-actions avatar github-actions commented on July 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.