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

Audio queue/playlist #10

Open superbluecool opened 6 years ago

superbluecool commented 6 years ago

Anyway to do this? Would be super helpful to be able to have a queue/playlist. Thanks

adrianstevens commented 6 years ago

This could be implemented by subscribing to the PlaybackEnded EventHandler and reading from a collection/queue.

I'll leave this issue open as a reminder to write a sample.

nielscup commented 3 years ago

Starting a new sound in PlaybackeEnded results in the following error right after calling player.Load(...:

Could not initialize an instance of the type 'AVFoundation.AVAudioPlayer': the native 'initWithContentsOfURL:error:' method returned nil.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
shoaibkazemi commented 1 year ago

see this: https://stackoverflow.com/questions/58692234/simpleaudioplayer-currentposition-and-duration-are-always-0

shoaibkazemi commented 1 year ago

private async void PlayPlaylist(string value) { if (PlayButtonMode == 1) { int startPosition; //Playing playlist from selected item if user selected one. (first item if he didn't) if (PlaylistSelectedItem == null) startPosition = 0; else startPosition = Playlist.IndexOf(playlistSelectedItem); if (audio.IsPlaying) audio.Stop(); PlaylistSelectedItem = Playlist[startPosition]; audio.Load(GetStreamFromFile(PlaylistSelectedItem.Name + ".wav")); audio.Play(); audio.PlaybackEnded += Audio_PlaybackEnded; } else if (PlayButtonMode == 2) audio.Play(); else { if (audio.IsPlaying) audio.Pause(); } ChangeButtonMode(PlayButtonMode); }

private void ChangeButtonMode(int senderMode) { if (senderMode == 1 || senderMode == 2) { PlayButtonText = "PAUSE"; PlayButtonImage = "pause_icon.png"; PlayButtonMode = 0; } else { PlayButtonText = "PLAY"; PlayButtonImage = "play_icon.png"; if (senderMode == 3) PlayButtonMode = 1; else PlayButtonMode = 2; } NotifyButtonChange(); }

private void Audio_PlaybackEnded(object sender, EventArgs e) { //Check if next playlist element exists if(Playlist.IndexOf(PlaylistSelectedItem) + 1 < Playlist.Count) { PlaylistSelectedItem = Playlist[Playlist.IndexOf(PlaylistSelectedItem) + 1]; if (audio.IsPlaying) audio.Stop(); audio.Load(GetStreamFromFile(PlaylistSelectedItem.Name + ".wav")); audio.Play(); audio.PlaybackEnded += Audio_PlaybackEnded; }else if(audio.IsPlaying) audio.Stop();
}

private void StopAudio() { if (audio.IsPlaying) audio.Stop(); ChangeButtonMode(3); }