Search code examples
xamarinxamarin.formsandroid-mediaplayermedia-playeravaudioplayer

How to play audio continuously in xamarin forms


I implemented audio implementation and added looping property also. But the audio Plays a two times only and after that audio doesn't play. Here I attached a code I implemented. Kindly help how to fix. Thanks for advance

namespace Com.Audio.Droid
{
    public class AudioService : IAudio
    {
        public AudioService()
        {
        }

        public void PlayAudioFile(string fileName)
        {
            var player = new MediaPlayer();
            var fd = global::Android.App.Application.Context.Assets.OpenFd("siren.wav");
            player.Prepared += (s, e) =>
            {
                player.Start();
            };
            player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            player.Looping = true;
            player.Prepare();
        }

        public void PauseAudioFile(string fileName)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                var player = new MediaPlayer();
                var fd = global::Android.App.Application.Context.Assets.OpenFd("siren.wav");
                player.Prepared += (s, e) =>
                {
                    player.Stop();
                    player.Release();
                    player.Dispose();
                };
                player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
                player.Prepare();
            });

        }
    }
}

And in iOS the audio play a only one second. It's doesn't play the audio file entirely

namespace com.Audio.ios
{
    public class AudioService : IAudio
    {
        public AudioService()
        {
        }

        public void PlayAudioFile(string fileName)
        {
            string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension("siren"), Path.GetExtension("siren.wav"));
            var url = NSUrl.FromString(sFilePath);
            var _player = AVAudioPlayer.FromUrl(url);
            _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>
            {
                _player = null;
            };
            _player.NumberOfLoops = -1;
            _player.Play();
        }

        public void PauseAudioFile(string fileName)
        {
            string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension("siren"), Path.GetExtension("siren.wav"));
            var url = NSUrl.FromString(sFilePath);
            var _player = AVAudioPlayer.FromUrl(url);
            _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>
            {
                _player = null;
            };
            _player.Stop();
        }
    }
}

Solution

  • For Android

    You need a hook way to implement a loopMediaPlayer, that is creating a new player when the audio is finished.

    Refer to : https://stackoverflow.com/a/29883923/8187800 .

    For iOS

    Due to ARC , probably AVAudioPlayer is released after calling dependency service , to solved it you can try to make _player as global variable .

    Refer to https://stackoverflow.com/a/8415802/8187800 .