ryanheise / just_audio

Audio Player
1.03k stars 652 forks source link

Play from file resets duration (Web) #611

Closed NielsH12 closed 1 year ago

NielsH12 commented 2 years ago

Which API doesn't behave as documented, and how does it misbehave? AudioPlayer.play() sometimes resets the position on the AudioSource. Even AudioPlayer.seek() doesn't work. It is a bit inconsistent in it's behavior, and it's a bit outside my area of expertise. But as far as I can tell. It depends on whether the HTTP response from the server is 200 or 206. I have found it to fail more consistenly on larger files.

Minimal reproduction project https://github.com/NielsH12/just_audio

You might also be able to reproduce it with the existing sample file if you change main.dart to use the included mp3 file: await _player.setAudioSource(AudioSource.uri(Uri.file("audio/nature.mp3")));

To Reproduce (i.e. user steps, not code)

  1. Run in browser (I've tested in Chrome and Edge)
  2. Click play and wait for a few seconds
  3. Click pause
  4. Click play again.
  5. File plays from beginning instead of resuming

Expected behavior I expected the playback to resume from where I paused.

Desktop (please complete the following information):

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version 10.0.19043.1415], locale da-DK)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] IntelliJ IDEA Community Edition (version 2017.3)
[√] IntelliJ IDEA Ultimate Edition (version 2018.3)
[√] VS Code (version 1.63.2)
[√] Connected device (2 available)

! Doctor found issues in 1 category.
ryanheise commented 2 years ago

I'm a bit confused by your bug report since you talk about HTTP status codes and yet your example uses the file:// scheme.

NielsH12 commented 2 years ago

Like I saId, it's outside my area of expertise. But if I look at the developer tools in Chrome under networking, I see a Status 200. image

ryanheise commented 2 years ago

file:// URIs are not supported on web due to the security model.

NielsH12 commented 2 years ago

What does this mean? AudioPlayer can't play mp3 files on Flutter Web? Did you try the example I provided?

ryanheise commented 2 years ago

Is your intention to deploy this web app "on the web"? I'm trying to understand what use case you might have where you would need to use a file:// URI -- something that websites "on the web" would block.

NielsH12 commented 2 years ago

I'd like to host a webapp where clients can play their own mp3 files. As such, the files are only present client side. Am I going about this all wrong?

ryanheise commented 2 years ago

I can't provide advice on that, but you might try searching past questions on StackOverflow (not necessarily flutter-specific) or asking a new question, and once you have the advice on what features will help you build your app, you can then make a feature request here regarding what you need.

People have tried to do similar things in the past with just_audio so if you search the past issues on this page you might also find approaches that may currently work for some use cases.

netgfx commented 2 years ago

Perhaps the author is trying to load an mp3 file via input upload so what they get in return is a temp file:// path or a blob

NielsH12 commented 1 year ago

@netgfx Yup, that is exactly what I was trying to do.

netgfx commented 1 year ago

Well you could try saving it locally https://stackoverflow.com/questions/67508623/how-to-save-streamedresponse-of-audio-to-file-in-flutter and then reading it from this library or use audio players https://stackoverflow.com/questions/65607152/how-can-i-play-local-mp3-on-flutter-web where you can provide the path of the file (although not sure it will play correctly)

Just to know that web can't do that either, its a browser security issue not a flutter one @NielsH12

ryanheise commented 1 year ago

As I have said above, I don't believe you can use file:// URIs due to the web security model, but people have done this sort of thing so I recommend searching past issues. As for whether this is a bug, I don't think it's a bug since your code is trying to do something that is not supported by the web security model.

or use audio players https://stackoverflow.com/questions/65607152/how-can-i-play-local-mp3-on-flutter-web where you can provide the path of the file (although not sure it will play correctly)

No it won't play correctly because as mentioned, you can't use file:// URIs on the web due to the security model.

I will close this as it is not a bug.

However, I recommend searching past issues. What you need to do is use a file picker that doesn't give you the file path (which you can't use) but instead gives you a stream of bytes or an array of bytes. Once you have that, you can render that with your own StreamAudioSource. Searching past issues or StackOverflow questions about this may be helpful.

github-actions[bot] commented 1 year ago

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.