wang-bin / fvp

Flutter video player plugin for all desktop+mobile platforms. download prebuilt examples from github actions. https://pub.dev/packages/fvp
BSD 3-Clause "New" or "Revised" License
197 stars 31 forks source link

How to play audio from assets? #106

Closed CaptainDario closed 4 months ago

CaptainDario commented 4 months ago

I am trying to play some audio files that are included in my assets folder. But I cannot seem to figure out how to do that. I can play an audio file from the filesystem using this code

final mdk.Player player = mdk.Player();
player.setMedia(
  '${audioFilesDir.path}/${widget.entry!.audio}.mp3',
  mdk.MediaType.audio);
player.state = mdk.PlaybackState.playing;

But trying to play from assets using something like

player.setMedia(
  'assets/audios/my_fancy_track.wav',
  mdk.MediaType.audio);

does not work.

Is this currently not supported?

wang-bin commented 4 months ago

https://github.com/wang-bin/fvp/blob/v0.20.2/lib/src/video_player_mdk.dart#L362 It's how video_player reads files in assets folder

CaptainDario commented 4 months ago

So there is no way to read from assets using an uri likeassets://audios/my_audio.wav? I need to copy your function _assetUri?

wang-bin commented 4 months ago

yes, copy that function. 'assets:' is a standard protocol on android. for other platforms, flutter assets are are regular files

CaptainDario commented 4 months ago

Is there a problem with making the function _assetUri public? Seems like it is quite important.

wang-bin commented 4 months ago

no problem. i even considered moving it to player.dart and make it public

CaptainDario commented 4 months ago

That would be awesome!

wang-bin commented 4 months ago

Try master branch. player.setAsset('assets/audios/my_fancy_track.wav') will play the audio. player.setAsset('assets/audios/my_fancy_track.wav', type: mdk.MediaType.audio) will load an external audio track while playing a video set by player.media or player.setAsset(asset)

CaptainDario commented 4 months ago

Thank you this works perfectly!

wang-bin commented 4 months ago

available in the latest release