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

kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout #8591

Open
az75014 opened this issue Jan 25, 2024 · 1 comment

Comments

@az75014
Copy link

az75014 commented Jan 25, 2024

Is your feature request related to a problem? Please describe.

While e.g. ffpyplayer supports URLs as source for the audio, it doesn't appear to be fully supported within Kivy:

There is no handling in the audio_ffpyplayer.py code for fatal errors by ffpyplayer such as "HTTP error 403 Forbidden". It can't be caught by a try/except and will cause the app to crash (this may be a bug but perhaps it was not the intention for us to use URLs here so have opened as feature request).

Here's an example:

The following URL causes an error for ffpyplayer (note: this URL works fine when opened in browser, and the audio can be played in-browser and downloaded as MP3):

from ffpyplayer.player import MediaPlayer
ffplayer = MediaPlayer("https://www.buzzsprout.com/1984146/14368896-january-24-2024.mp3")

Output:

[https @ 000002168f74fd40] HTTP error 403 Forbidden
https://www.buzzsprout.com/1984146/14368896-january-24-2024.mp3: Server returned 403 Forbidden (access denied)

If you try it with SoundLoader it crashes the app when the play() method is applied. You can put try/except around the load and play but this will not help.

sound = SoundLoader.load("https://www.buzzsprout.com/1984146/14368896-january-24-2024.mp3")
if sound:
sound.play()

Output:

[WARNING] [ffpyplayer ] [https @ 0000020bebfeb9c0] HTTP error 403 Forbidden
[ERROR ] [ffpyplayer ] https://www.buzzsprout.com/1984146/14368896-january-24-2024.mp3: Server returned 403 Forbidden (access denied)
(followed by crash)

I have also encountered issues with pause/play for URLs: the audio is resumed at the right timestamp but then it restarts again from the beginning. For files it works perfectly for me. I couldn't see reasons/errors for this behaviour so maybe the URL causes issues with pause/play on a deeper level (EDIT: See my comment below).

Describe the solution you'd like
A clear and concise description of what you want to happen.

Full support for URLs as source for SoundLoader:

  1. Correctly deal with fatal errors (such as above) so we can catch them with try/except and deal with them in the code - e.g. raise exceptions related to HTTP errors
  2. Support the same functionality as currently possible with audio files (e.g. pause/play)
  3. I think SoundLoader should have an optional field to add timeout (with error handling if not completed in time): I saw there are timeouts defined in places in the code but it should be possible to have an overarching timeout to meet the use case of the implementation?

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Downloading the file and then playing it via SoundLoader doesn't fit my use case (my intention was to provide a "play" button for podcasts, which can be lengthy.) I have considered going outside of Kivy but that would be out-of-scope for me at this time.

Additional context
Add any other context or screenshots about the feature request here.

@az75014 az75014 changed the title Kivy Audio - SoundLoader: support URLs as source, error handling and timeout kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout Jan 25, 2024
@az75014 az75014 changed the title kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout 'kivy.core.audio' - SoundLoader: support URLs as source, error handling and timeout Jan 25, 2024
@az75014 az75014 changed the title 'kivy.core.audio' - SoundLoader: support URLs as source, error handling and timeout "kivy.core.audio" - SoundLoader: support URLs as source, error handling and timeout Jan 25, 2024
@az75014 az75014 changed the title "kivy.core.audio" - SoundLoader: support URLs as source, error handling and timeout kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout Jan 25, 2024
@az75014 az75014 changed the title kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout kivy.core.audio - SoundLoader: support URLs as source, error handling and timeout Jan 25, 2024
@az75014
Copy link
Author

az75014 commented Jan 26, 2024

The issue with pause/play causing the audio to go to the timestamp specified by seek(position) and then restart from the beginning may have to do with the play method having an extra step of seek(0) at the end? I can't quite figure out why this step is there. If you use ffpyplayer directly the toggle_pause() method works exactly like you expect -- as a toggle, there is no need to play around with seek() at all as it remembers the time stamp.

EDIT: If in my own code I build in a delay (e.g. via Clock.schedule_once) between play and seek(position) by triggering the seek() via the callback then it works fine -- I guess timing issues are causing the extra "seek(0)" to fall after my own code is doing seek(position)? Seems like a bug to me?

Code snippet from audio_ffpyplayer.py:

    def play(self):
        if self._state == 'playing':
            super(SoundFFPy, self).play()
            return
        if not self._ffplayer:
            self.load()
        self._ffplayer.toggle_pause()
        self._state = 'playing'
        self.state = 'play'
        super(SoundFFPy, self).play()
        self.seek(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant