{"id":9914,"date":"2023-03-05T19:13:54","date_gmt":"2023-03-05T19:13:54","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=9914"},"modified":"2023-03-05T19:13:54","modified_gmt":"2023-03-05T19:13:54","slug":"plugin-maui-audio-plays-sound-even-if-android-device-is-set-to-vibrate","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/plugin-maui-audio-plays-sound-even-if-android-device-is-set-to-vibrate\/","title":{"rendered":"Plugin.Maui.Audio plays sound even if Android device is set to vibrate"},"content":{"rendered":"<p>I have a MAUI application which alerts the user with a sound and vibration when a task is completed. I noticed that turning the phone to vibrate didn&#8217;t actually stop the sound being played via the Plugin.Maui.Audio (as I sort of expected it to do).<\/p>\n<p>It looks like what we need to do is write some Android specific code to check the Android AudioManager&#8217;s RingerMode.<\/p>\n<p>If we assume we have a device independent enum such as the following (this simply mirrors the Android RingerMode)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic enum AudioMode\r\n{\r\n    Silent = 0,\r\n    Vibrate = 1,\r\n    Normal = 2\r\n}\r\n<\/pre>\n<p>Now, from our Android code we would expose the RingerMode like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (Context.GetSystemService(AudioService) is not AudioManager audioService)\r\n  return AudioMode.Normal;\r\n\r\nswitch (audioService.RingerMode)\r\n{\r\n  case RingerMode.Normal:\r\n    return AudioMode.Normal;\r\n  case RingerMode.Silent:\r\n    return AudioMode.Silent;\r\n  case RingerMode.Vibrate:\r\n    return AudioMode.Vibrate;\r\n}\r\n\r\nreturn AudioMode.Normal;\r\n<\/pre>\n<p>Now we would simply wrap our Plugin.Maui.Audio code in an if statement, checking the AudioMode of the device. i.e. if Vibrate or Silent, don&#8217;t play the audio. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (_deviceAudioService.AudioMode == AudioMode.Normal)\r\n{\r\n  var audioPlayer =\r\n    _audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync(&quot;chimes.wav&quot;));\r\n  audioPlayer.Volume = Math.Clamp(_configuration.NotificationSoundVolume \/ 100.0, 0, 1);\r\n  audioPlayer.Play();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a MAUI application which alerts the user with a sound and vibration when a task is completed. I noticed that turning the phone to vibrate didn&#8217;t actually stop the sound being played via the Plugin.Maui.Audio (as I sort of expected it to do). It looks like what we need to do is write [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[120,330],"tags":[],"class_list":["post-9914","post","type-post","status-publish","format-standard","hentry","category-android","category-maui"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9914"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9914\/revisions"}],"predecessor-version":[{"id":9918,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9914\/revisions\/9918"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}