audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

Add to example project: Play mp3 from local assets using just_audio with audio_service

Open headfulness opened this issue 4 years ago • 11 comments

To which pages does your suggestion apply?

  • https://github.com/ryanheise/audio_service/blob/master/example/lib/main.dart

Quote the sentences(s) from the documentation to be improved (if any)

n/a

Describe your suggestion

A working example of playing a local asset using just_audio and audio_service would be helpful.

In the audio_service example project, it would be cool to see a third MediaItem added to MediaLibrary's _items property, where the id of the third MediaItem is path to local asset (like "asset:///audio/nature.mp3" in the just_audio example project)

I don't have any issues when playing an asset using just_audio alone. But when I try to do what I described above, I get "Operation Stopped" when setting the audio source to an mp3 from assets.

I only saw open issues for loading Art from assets, so I assumed this is working and I'm just doing something wrong.

...

headfulness avatar Apr 05 '21 03:04 headfulness

I think the playing of mp3s is more of an issue for just_audio, but note that the just_audio example already shows how to load from assets.

ryanheise avatar Apr 05 '21 04:04 ryanheise

I don't have any issues when playing an asset using just_audio alone. But when I try to do what I described above, I get "Operation Stopped" when setting the audio source to an mp3 from assets.

I would be interested to know more about this, do you have a reproduction project?

ryanheise avatar Apr 05 '21 04:04 ryanheise

Hi Ryan, I realized this is only an issue with iOS simulator. It works fine on an iOS device. Works fine in Android emulator. And works on Android device.

Here's a reproduction project below. I forked audio_service and in the example project, I swapped out the first MediaItem in MediaLibrary for a local asset (asset:///audio/nature.mp3). Pubspec.yaml is updated to include the asset.

https://github.com/headfulness/audio_service

Thank you

headfulness avatar Apr 05 '21 22:04 headfulness

We can close this issue. There was enough documentation between the two libraries to figure it out. The error on iOS simulator just confused me.

Let me know if the iOS simulator problem should be opened as a separate issue.

headfulness avatar Apr 05 '21 22:04 headfulness

Hmm, that's interesting. So assets work but not in the first position of a playlist? I'll reopen until I can investigate this. Even though it may only happen in the Simulator, I suspect there's a random element to it and it could manifest on a real device by chance.

ryanheise avatar Apr 06 '21 10:04 ryanheise

It happens when playing an asset in any position in the playlist.

In my reproduction project I switched the playlist to play the HTTP AudioSource first, asset second. The url plays fine, then once the asset starts, there is a log for "Operation Stopped" and the audio doesn't play. I'll debug it a bit tonight and see where it's coming from.

Edit: here's the log [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: PlatformException(-11849, Operation Stopped, null, null)

headfulness avatar Apr 07 '21 00:04 headfulness

Hmm, so is this bug reproducible in the example, which includes an asset? I have never encountered this error myself on the Simulator.

ryanheise avatar Apr 07 '21 06:04 ryanheise

Yeah I modified the example project in audio_service to reproduce it. The fork is below.

https://github.com/headfulness/audio_service

headfulness avatar Apr 07 '21 16:04 headfulness

Interestingly, it worked on ios simulator last night. So there must be a timing or environment aspect causing it. I'm working a lot with assets right now so I'll see if I can find out what the trigger is.

headfulness avatar Apr 08 '21 13:04 headfulness

There is a known issue where assets can fail if you don't await the loading process before you try to play it, and in the example app there is no await because each action is driven by user interaction.

If you have control over such things in your app, you might try doing await player.setAsset(...); before playing (or whatever set method you use instead).

ryanheise avatar Apr 08 '21 14:04 ryanheise

thank you, I will implement that

headfulness avatar Apr 08 '21 23:04 headfulness