November 13th, 2017

Adding Sound to a Xamarin.Forms App

Audio has many uses in mobile applications. Sounds can be essential to your app; they may notify users of important events or make your app accessible to visually-impaired users. We can also use sound to help convey moods, intentions, or feelings within our apps. In fact, many companies include jingles or sounds as part of their brand.

Including audio in cross-platform apps means invoking platform-specific code. UWP, Android, and iOS all have powerful, but different APIs, to manage and play sounds. As cross-platform C# developers, we’d prefer a common API that can load audio data from a shared location.

Xam.Plugin.SimpleAudioPlayer

The open-source SimpleAudioPlayer plugin provides a cross-platform C# API that can be used to load and play back audio in Windows UWP, Xamarin.Android, Xamarin.iOS, Xamarin.tvOS, and Xamarin.mac projects.

Xam.Plugin.SimpleAudioPlayer is available from Nuget.org. To get started, add the NuGet package to each platform-specific project you wish to support. If you’re using the plugin from a shared library such as a .NET Standard lib or PCL, add the package to the library.

You can add audio files to the platform-specific projects or to a shared library. If storing in the platform-specific projects, the files are expected to be in the standard locations. For Windows UWP and Android, place the files in the Assets folder with the Build Action set to Content and Android Asset respectively.

For iOS, macOS, and tvOS, place the audio files in the Resources folder and ensure the Build Action is set to BundleResource.

Audio files are then loaded by name:

var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; 
player.Load("intro_sound.mp3");

When loading audio from a shared library, set the Build Action to Embedded Resource.

Files stored in a shared library are loaded into Stream before being loaded by the plugin:


var assembly = typeof(App).GetTypeInfo().Assembly; 
Stream audioStream = assembly.GetManifestResourceStream("YourSharedAssemblyName." + "yoursound.wav"); 
 
 
var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; 
player.Load(audioStream);

You start playback by calling the Play method.

player.Play();

SimpleAudioPlayer provides methods and properties to control audio playback, including the ability to stop, pause, seek, set volume, and set balance.

Playing Multiple Sounds Simultaneously

If you need to play more than one sound, the SimpleAudioPlayer plugin provides a factory method to create ISimpleAudioPlayer objects. Each object can load and play back audio independent of the others.

 
var alertSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 
var warningSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 
var clickSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 
var submitSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 
 
alertSound.Load(...); 
warningSound.Load(...); 
... 
alertSound.Play(); 
...

For an example of simultaneous playback, see the cross-platform Druminator sample app here.

SimpleAudioPlayer is open-source; if you’d like to request features, you can file an issue on GitHub, or better yet, make the changes and submit a pull request.

Further Reading

Xamarin and Xamarin.Forms have a fantastic plugin community. To find other great plugins, visit https://github.com/xamarin/XamarinComponents. You’ll even find other audio plugins to perform tasks such as converting text-to-speech and recording audio.

If you’re interested in learning more about the platform-specific APIs to playback audio, we have excellent documentation at developer.xamarin.com. To see how the Druminator app was built, watch the Xamarin University webinar here.

And to learn more about Xamarin development, be sure to check out our free courses available at university.xamarin.com. Better yet, sign up for a free trial to access all of Xamarin University’s Self-Guided Learning courses!

Discuss this post on the forums!

0 comments

Discussion are closed.