I have a collection of Audio data stored as byte[] in an ObservableCollection. I want to iterate through and be able to play them all one after the other in my Maui app. I need the ability to Stop playback midway through as well. The issue with my code below is that all the audio files start playing at the same time over one another and ending as they end. (The player does work fine when sending individual songs, and even able to Stop() as needed)
foreach (var record in VoiceAudioCollection)
{
voicePlayer = new VoicePlayer(record.FileBytes);
voicePlayer.PlaybackEnded += (sender, args) =>
{
record.IsPlaying = false;
currentlyPlaying = null;
voicePlayer = null;
Console.WriteLine($"*** PLAYBACK ENDED *** {record.Id}");
};
voicePlayer.Play();
}
I have the VoicePlayer defined in each of Android and iOS as follows. iOS:
private AVFoundation.AVAudioPlayer player;
public void Play()
{
isFinishedPlaying = false;
player.Play();
}
Android:
private Android.Media.MediaPlayer player;
public void Play()
{
if (!isPlaying)
{
isPlaying = true;
player.Start();
}
}
I'm looking for pointers on how to play this list of songs in sequence.
something like this should work
int ndx = 0;
void PlayNext()
{
if (ndx > VoiceAudioCollection.Count) return;
var record = VoiceAudioCollection[ndx];
voicePlayer = new VoicePlayer(record.FileBytes);
voicePlayer.PlaybackEnded += (sender, args) =>
{
record.IsPlaying = false;
currentlyPlaying = null;
voicePlayer = null;
Console.WriteLine($"*** PLAYBACK ENDED *** {record.Id}");
ndx++;
PlayNext();
};
voicePlayer.Play();
}