Xamarin Sound Effects Part 1

Adding another UX to your apps

Posted by 40zdhp469a8f32s5rrr737qrwx on May 26, 2022

Adding sound to Xamarin

Adding sound might be the last thing that we think about as developers when calling an app complete, but you would be surprised at how much audio feedback can add to your the user experience.

Thanks to the SimpleAudioPlayer package written by Adrian Stevens, it has never been easier to add sounds to all platforms.

1.) Getting SoundAudioPlayer

You will need to add the SoundAudioPlayer Nuget package to each of your target projects (iOS/Android/UWP/Tizen etc.) as well as to your common/PCL project.

2.) Choosing audio formats

Make sure you choose a file format that works on each of the platforms that you plan on using. Keep in mind that the SimpleAudioPlayer plugin is amazing but it does not convert file formats - it simply provides a cross-platform way playing audio files using the native audio players without you having to worry about the gotchas and class hierarchies on each platform.

My recommendation is to use FLAC for small to medium length files and MP3s for longer files where size is more important than quality. It would be nice to declare: "Always use MP3s" since they are universally playable, but FLAC is pretty close and has some advantages.

When using MP3s, there is a small bit of silence at the beginning of the sound that you can't get rid of. If you are using the sound player for short audio effects like clicks, laser sounds or other kind of quick feedback, having a small pause between the action and the sound is.. annoying.

FLACs generally have a higher quality sound. FLAC is a lossless format that can still compress the audio size by 50 to 70 percent of it's original size. For small files where you want quick and polished, it is probably the way to go.

3.) Adding the audio files

This is easy.

  • Create a folder such as Audio in your common/PCL project.
  • Copy your audio files into this folder.
  • Set the Build Action of each file to Embedded resource Embedded Resource

4.) Playing the file

There are three major parts here:

1.) Loading the embedded file into a Stream

public override Stream GetAudioStream()
{
   var stream = GetType().Assembly.GetManifestResourceStream(_filePath);

   return stream;
}

2.) Creating the sound player from the Stream

protected void EnsurePlayerLoaded()
{
   if (_soundPlayer == null)
   {
      _soundPlayer = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();

      var stream = GetAudioStream();
      _soundPlayer.Load(stream);
   }
}

3.) Play the file.

private void ExecutePlay()
{
   EnsurePlayerLoaded();
	 
   _soundPlayer.Loop = IsLooped;   
   _soundPlayer.Play();

   Device.BeginInvokeOnMainThread(() =>
   {
      //Some sounds are less than one second and will be rounded down to zero on some platforms. We don't want that.
      Duration = _soundPlayer.Duration == 0 ? 1 : _soundPlayer.Duration;
   });
}

Other Bits

The SimpleAudioPlayer has a property to enable looping which is great for background music for games or for making a sound machine for sleeping etc. without having an hours long audio file.

You will need to edit the sound file beforehand so that the beginning and ending of the sound file match acoustically using something like Audacity.

Actually, I would recommend to always edit your sound clips to make sure that the audio is around the same volume as well as cleaning up any empty areas before and after the main audio. This is a place where a little extra time will go a long way. Remember that your users will hear the same sounds over and over so they will pickup on any oddities.

The audio files are from Wikimedia Commons and I would recommend that as a good place to find all sorts of free media. Please just make sure to attribute as requested when using...

That's it for this one

Next time we'll be looking into how to generate your own sounds straight from code. Check back soon! Part 2

You can find a complete sample of all of the code found above on GitHub: JamesThomas/SoundMachine!

Header image from: Xmly - Own work, CC BY-SA 4.0 https://commons.wikimedia.org/w/index.php?curid=53703491