How do I play a WAV file?

Hi,

Sorry for the general nature of this question, but I am looking for a fairly simple way to play WAV files on iOS and WinPhone. I am having to begin my learning process on WinPhone, and this is where Xamarin documents come up short. I have found myself looking at WPF examples, struggling to see how much of it carries across to Xamarin's subset of .NET.

So far I have this code, which doesn't give an error but produces only silence:

public void PlayTestWAV()
{
    MediaElement player = new MediaElement();
    player.Source = new Uri( @"/Assets/Beep.wav", UriKind.Relative );
    player.Play();
}

Can anyone suggest what I am doing wrong, or, better still, point me at a decent resource on the topic of playing sound across both iOS and WinPhone (and I guess Android eventually)?

Kind wishes ~ Patrick

Best Answer

  • GeraldVersluisGeraldVersluis NL ✭✭✭✭
    edited January 2016 Accepted Answer

    There won't be a unified way. You're going to have to use the DependencyService for this.
    For iOS the code would probably be something like this;

        private AVAudioPlayer _ringtoneAudioPlayer;
        public AudioService()
        {
            _ringtoneAudioPlayer = AVAudioPlayer.FromUrl(NSUrl.FromFilename("Monkeys.wav"));
            _ringtoneAudioPlayer.NumberOfLoops = -1; // infinite
        }
    
        public void PlaySound()
        {
            if (_ringtoneAudioPlayer != null)
            {
                _ringtoneAudioPlayer.Stop();
            }
            _ringtoneAudioPlayer.Play();
        }
    
        public void StopSound()
        {
            if (_ringtoneAudioPlayer != null)
            {
                _ringtoneAudioPlayer.Stop();
            }
        }
    

    Android like this

        public AudioService() {}
    
        private MediaPlayer _mediaPlayer;
    
        public void PlaySound()
        {
            _mediaPlayer = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.Monkeys);
        _mediaPlayer.Start();
        }
    
        public void StopSound()
        {
            if (_mediaPlayer != null)
                _mediaPlayer.Stop();
        }
    

    And Windows Phone like this:

        public void PlaySound()
        {
            string monkeySoundFile = "Sound/Monkeys.wav";
            var stream = TitleContainer.OpenStream(monkeySoundFile);
    
            if (stream != null)
            {
                  var effect = SoundEffect.FromStream(stream);
                  FrameworkDispatcher.Update();
                  effect.Play();
            }
    }
    

    Of course the code needs some polishing and you need to add the resources. You could even make them some more flexible by adding the possibility to supply a filename yourself. But this should be a good start :smile:

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    edited January 2016 Accepted Answer

    There won't be a unified way. You're going to have to use the DependencyService for this.
    For iOS the code would probably be something like this;

        private AVAudioPlayer _ringtoneAudioPlayer;
        public AudioService()
        {
            _ringtoneAudioPlayer = AVAudioPlayer.FromUrl(NSUrl.FromFilename("Monkeys.wav"));
            _ringtoneAudioPlayer.NumberOfLoops = -1; // infinite
        }
    
        public void PlaySound()
        {
            if (_ringtoneAudioPlayer != null)
            {
                _ringtoneAudioPlayer.Stop();
            }
            _ringtoneAudioPlayer.Play();
        }
    
        public void StopSound()
        {
            if (_ringtoneAudioPlayer != null)
            {
                _ringtoneAudioPlayer.Stop();
            }
        }
    

    Android like this

        public AudioService() {}
    
        private MediaPlayer _mediaPlayer;
    
        public void PlaySound()
        {
            _mediaPlayer = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.Monkeys);
        _mediaPlayer.Start();
        }
    
        public void StopSound()
        {
            if (_mediaPlayer != null)
                _mediaPlayer.Stop();
        }
    

    And Windows Phone like this:

        public void PlaySound()
        {
            string monkeySoundFile = "Sound/Monkeys.wav";
            var stream = TitleContainer.OpenStream(monkeySoundFile);
    
            if (stream != null)
            {
                  var effect = SoundEffect.FromStream(stream);
                  FrameworkDispatcher.Update();
                  effect.Play();
            }
    }
    

    Of course the code needs some polishing and you need to add the resources. You could even make them some more flexible by adding the possibility to supply a filename yourself. But this should be a good start :smile:

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    Just had to recreate my entire project, and the Windows Phone project seems to have changed so much that I no longer have access to this way to play a WAV file. Specifically, attempting to include a reference to Microsoft.Xna.Framework. Attempting to include a reference to this results in an obscure internal Xaml error (can't find Externalexception in mscorlib.dll).

    Am I on the right lines, trying to use Xna, or is there a better way simply to play a WAV? If I should use Xna, does anyone know why I am getting the exception in Xaml?

    Pretty desperate to solve this problem now, so any help would save what little hair I have remaining.

    Kind wishes ~ Patrick

Sign In or Register to comment.