{"id":34237,"date":"2017-11-13T12:15:04","date_gmt":"2017-11-13T20:15:04","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=34237"},"modified":"2019-04-04T11:03:11","modified_gmt":"2019-04-04T18:03:11","slug":"adding-sound-xamarin-forms-app","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/adding-sound-xamarin-forms-app\/","title":{"rendered":"Adding Sound to a Xamarin.Forms App"},"content":{"rendered":"<p>\t\t\t\tAudio 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. <\/p>\n<p>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\u2019d prefer a common API that can load audio data from a shared location.<\/p>\n<h2>Xam.Plugin.SimpleAudioPlayer<\/h2>\n<p>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.  <\/p>\n<p>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\u2019re using the plugin from a shared library such as a .NET Standard lib or PCL, add the package to the library.  <\/p>\n<p>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 <strong>Build Action<\/strong> set to <strong>Content<\/strong> and <strong>Android Asset<\/strong> respectively. <\/p>\n<p>For iOS, macOS, and tvOS, place the audio files in the <strong>Resources<\/strong> folder and ensure the <strong>Build Action<\/strong> is set to <strong>BundleResource<\/strong>. <\/p>\n<p>Audio files are then loaded by name: <\/p>\n<pre><code>var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; \r\nplayer.Load(\"intro_sound.mp3\");<\/code><\/pre>\n<p>When loading audio from a shared library, set the <strong>Build Action<\/strong> to <strong>Embedded Resource<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/EmbeddedResource.png\" alt=\"\" width=\"400\" class=\"aligncenter size-full wp-image-34238\" \/> <\/p>\n<p>Files stored in a shared library are loaded into Stream before being loaded by the plugin:<\/p>\n<pre><code>\r\nvar assembly = typeof(App).GetTypeInfo().Assembly; \r\nStream audioStream = assembly.GetManifestResourceStream(\"YourSharedAssemblyName.\" + \"yoursound.wav\"); \r\n \r\n \r\nvar player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; \r\nplayer.Load(audioStream);<\/code><\/pre>\n<p>You start playback by calling the Play method. <\/p>\n<pre><code>player.Play();<\/code><\/pre>\n<p>SimpleAudioPlayer provides methods and properties to control audio playback, including the ability to stop, pause, seek, set volume, and set balance. <\/p>\n<h2>Playing Multiple Sounds Simultaneously<\/h2>\n<p>If you need to play more than one sound, the SimpleAudioPlayer plugin provides a factory method to create <code>ISimpleAudioPlayer<\/code> objects. Each object can load and play back audio independent of the others.<\/p>\n<pre><code> \r\nvar alertSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); \r\nvar warningSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); \r\nvar clickSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); \r\nvar submitSound = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); \r\n \r\nalertSound.Load(...); \r\nwarningSound.Load(...); \r\n... \r\nalertSound.Play(); \r\n...<\/code><\/pre>\n<p>For an example of simultaneous playback, <a href=\"https:\/\/github.com\/adrianstevens\/Xamarin-Forms\/tree\/master\/DrumPad2\" rel=\"noopener\" target=\"_blank\">see the cross-platform Druminator sample app here<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/DrumPad.png\" alt=\"\" width=\"500\" class=\"aligncenter size-full wp-image-34239\" \/>   <\/p>\n<p>SimpleAudioPlayer is open-source; if you\u2019d like to request features, you can file an issue on GitHub, or better yet, make the changes and submit a pull request.<\/p>\n<h2>Further Reading<\/h2>\n<p>Xamarin and Xamarin.Forms have a fantastic plugin community. To find other great plugins, visit  <a href=\"https:\/\/github.com\/xamarin\/XamarinComponents\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/xamarin\/XamarinComponents<\/a>. You\u2019ll even find other audio plugins to perform tasks such as converting text-to-speech and recording audio. <\/p>\n<p>If you\u2019re interested in learning more about the platform-specific APIs to playback audio, we have excellent documentation at <a href=\"http:\/\/developer.xamarin.com\" rel=\"noopener\" target=\"_blank\">developer.xamarin.com<\/a>. To see how the Druminator app was built, <a href=\"https:\/\/www.youtube.com\/watch?v=NGvn-pGZFPA\" rel=\"noopener\" target=\"_blank\">watch the Xamarin University webinar here<\/a>. <\/p>\n<p>And to learn more about Xamarin development, be sure to check out our free courses available at <a href=\"http:\/\/university.xamarin.com\" rel=\"noopener\" target=\"_blank\">university.xamarin.com<\/a>. Better yet, <a href=\"https:\/\/www.xamarin.com\/university\" rel=\"noopener\" target=\"_blank\">sign up for a free trial<\/a> to access all of Xamarin University&#8217;s Self-Guided Learning courses!<\/p>\n<p><a href=\"https:\/\/forums.xamarin.com\/106563\/\">Discuss this post on the forums!<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":559,"featured_media":43802,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,367],"tags":[4,16],"class_list":["post-34237","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin-forms","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/34237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/559"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=34237"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/34237\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/43802"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=34237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=34237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=34237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}