apache / cordova-plugin-media

Apache Cordova Media Plugin
https://cordova.apache.org/
Apache License 2.0
388 stars 768 forks source link

Cannot preload sound files #294

Open yunyun27 opened 3 years ago

yunyun27 commented 3 years ago

With media plugin version 1.0.1, you can preload sound files, and play them without any problem. With the latest media plugin, if you preload multiple sound files, only the last one will be played.

For example: var a = new Media(file1); var b = new Media(file2); a.play(); // works in v.1.0.1 but not the latest version

I'd been using 1.0.1 to bypass the issue but it seems no longer possible with new ios build requirements.

Preloading is quite important for me. Could someone please help? Thank you!

timbru31 commented 3 years ago

It would really help us, if you can identify the version which "broke"(?) the feature you are describing. Was the behavior changed in the v2.0.0 release or later on?

yunyun27 commented 3 years ago

It "broke" in the v3.0.0 release. Thank you!

yunyun27 commented 3 years ago

I am so sorry, I just realized there are a bunch of minor versions between v2 and v3. It actually broke in the v.2.2.0 release. In other words, v.2.1.0 works fine. Thanks!

breautek commented 3 years ago

These are the changes between v2.1.0 and v2.2.0

https://github.com/apache/cordova-plugin-media/compare/rel/2.1.0...rel/2.2.0

gormlabenz commented 3 years ago

Had the same issue on ios, didn't test it on andorid. Downgrading from 5.0 to 2.0 solved the issue.

gormlabenz commented 3 years ago

Version 4.0 is also working

Heshyo commented 3 years ago

I don't know if it's linked to https://github.com/apache/cordova-plugin-media/issues/293#issuecomment-766520722 , but I also fixed my issue on iOS by reverting from 5.0.3 to 4.0.

goffioul commented 2 years ago

I believe this is due to https://github.com/apache/cordova-plugin-media/commit/00ddb9372bf4c9b96ccea0dbd285499df1809277. For remote streams, the plugin does not keep track of the created AVPlayer instance and keeps overwriting a single CDVSound avPlayer field. This has then various knock-off effects, e.g. create 2 sounds with different URL, the second one overwrite the AVPlayer of the first one and they both end up controlling the same playback:

s1 = new Media(url_1);
s1.play();
s2 = new Media(url_2);  // <-- s1 stops playing
s2.play();
s1.pause();  // <-- s2 pauses
s1.play();   // <-- s2 resumes

I think you can also run into inconsistencies with one remote and one local sound, although I haven't actually tested it: create a remote sound, then create a local sound, then play the local sound. Because the logic only checks for the presence of avPlayer (https://github.com/apache/cordova-plugin-media/blob/master/src/ios/CDVSound.m#L384), I think it might start playing the remote sound instead (just a guess, though).

Unfortunately, it seems the iOS implementation of the plugin is badly unmaintained. Whatever the reasons were to use different code path (AVPlayer vs. AVAudioPlayer) for local and remote streams, I don't think these are still relevant nowadays.

ghenry22 commented 2 years ago

The plugin was originally written only with avaudioplayer. This framework does not support streaming audio, it will download an entire file and then play it.

So the avplayer implementation was added so that streaming sources would use avplayer so that media streaming worked properly.

I have a fork of the plugin that I have been working on slowly removing the avaudioplayer code completely so that everything just uses avplayer as it supports both file and streaming sources. There is a lot of cruft in this plugin though so it is slow going cleaning it up.