adrianstevens / Xamarin-Plugins

Cross-platform Plugins for Xamarin, Xamarin.Forms and Windows
https://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer/
MIT License
132 stars 53 forks source link

SimpleAudioPlayer: setting volume to 0 not working on iOS #95

Closed KnoxCameron closed 1 year ago

KnoxCameron commented 2 years ago

I have a Xamarin Forms app that I originally tested and released on Android, and am now testing and modifying as required for iOS. I use SimpleAudioPlayer as a simple way of playing audio clips (thanks!).

I need to be able to sometimes play an audio clip silently i.e. the app pauses playback from the user's point of view for the time of the audio clip's length. I do this on Android by simply setting the volume to 0, with code something like this:

if (audioToPlay.PlaySilenceFlag) { MyVolume = 0; PlayerSvc.player.Volume = MyVolume; }

This works fine on Android with the clip playing silently. On iOS, the clip plays at normal volume. My code around this is the same on both platforms. FYI my code sets the volume back to 1 in the playback ended event.

Are you aware of any issue setting volume to 0 on iOS? Or can you suggest a possible cause for this behaviour?

Thanks!

KnoxCameron commented 2 years ago

In my limited testing, it looks like setting the volume has no effect on the player on iOS. When debugging, it always shows the value of the Volume property of the object as 1.

As a workaround, I am using this code to pause execution for the time it would have played the clip silently:

                                PlayerSvc.player.Load(stream);
                                // SimpleAudioPlayer volume setting doesn't work on iOS
                                if (Device.RuntimePlatform == Device.iOS && MyVolume == 0)
                                {
                                    double speechLengthSecs = PlayerSvc.player.Duration;
                                    // convert to millisecs
                                    int speechLengthMillisecs = Convert.ToInt32(speechLengthSecs * 1000);
                                    await Task.Delay(speechLengthMillisecs);
                                    DoMediaEnded();
                                }
                                else
                                {
                                    PlayerSvc.player.Play();
                                }
BlueRaja commented 2 years ago

I think this is most likely caused by https://github.com/adrianstevens/Xamarin-Plugins/issues/107. Try setting the volume after calling Load(...)

KnoxCameron commented 1 year ago

I think this is most likely caused by #107. Try setting the volume after calling Load(...)

Thanks! That does seem to have fixed the issue for me.