title: How to play a simple audio file with Xamarin iOS/Android
body: <p>I'm trying to learn and play a bit with Xamarin :)
  I wanted to play a simple sound at the end of a certain time and in part I succeeded, this works on the android and ios emulators but when I try to build the app on my iPhone this crush at the moment of sound reproduction!</p>
  
  <p>the code I wrote I copied from <a href="https://www.c-sharpcorner.com/article/playing-audio-mp3-file-in-xamarin-forms/" rel="nofollow noreferrer">here</a>!</p>
  
  <p>so my code is this:</p>
  
  <p>iAudio.cs :</p>
  
  <pre><code>using System;
  namespace StopWatch
  {
      public interface IAudio
      {
          void PlayAudioFile(string fileName);
      }
  }
  </code></pre>
  
  <p>AudioService.cs in Android :</p>
  
  <pre><code>using System;
  using Xamarin.Forms;
  using StopWatch.Droid;
  using Android.Media;
  using Android.Content.Res;
  
  [assembly: Dependency(typeof(AudioService))]
  namespace StopWatch.Droid
  {
      public class AudioService : IAudio
      {
          public AudioService()
          { }
          public void PlayAudioFile(string fileName)
          {
              var player = new MediaPlayer();
              var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);
              player.Prepared += (s, e) =&gt;
              {
                  player.Start();
              };
              player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
              player.Prepare();
          }
      }
  }
  </code></pre>
  
  <p>AudioService.cs in iOS:</p>
  
  <pre><code>using System; using Xamarin.Forms; using StopWatch; using StopWatch.iOS; using System.IO; using Foundation; using AVFoundation; [assembly: Dependency(typeof(AudioService))] namespace StopWatch.iOS {
      public class AudioService : IAudio
      {
          public AudioService()
          { }
          public void PlayAudioFile(string fileName)
          {
              string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
              NSUrl url = NSUrl.FromString(sFilePath);
              var _player = AVAudioPlayer.FromUrl(url);
              _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =&gt;
              {
                  _player = null;
              };
              _player.Play();
          }
      } 
  }
  </code></pre>
  
  <p>and this is mi MainPage.xaml.cs :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Reflection;
  using System.Text;
  using System.Threading.Tasks;
  using Xamarin.Forms;
  
  namespace StopWatch
  {
      // Learn more about making custom code visible in the Xamarin.Forms previewer
      // by visiting https://aka.ms/xamarinforms-previewer
      [DesignTimeVisible(false)]
      public partial class MainPage : ContentPage
      {
          Stopwatch stopwatch;
          public MainPage()
          {
              InitializeComponent();
              stopwatch = new Stopwatch();
              lblStopWatch.Text = "00:00:00";
              lblStopWatchAllert.Text = "";
          }
  
          private void btnStartClicked(object sender, EventArgs e)
          {
              stopwatch.Start();
              Device.StartTimer(TimeSpan.FromMilliseconds(10), () =&gt;
              {
                  lblStopWatch.Text = stopwatch.Elapsed.ToString().Substring(0, 8);
                  controll(lblStopWatch.Text);
                  return true;
  
              });
          }
  
          private void btnStopClicked(object sender, EventArgs e)
          {
              stopwatch.Stop();
          }
  
          private void btnResetClicked(object sender, EventArgs e)
          {
              stopwatch.Reset();
          }
  
          private void controll(String time)
          {
              if (string.Compare(time, "00:00:03:0000") &gt; 0)
              {
  
                  stopwatch.Reset();
                  lblStopWatchAllert.Text = "time is over!";
  
                  DependencyService.Get&lt;IAudio&gt;().PlayAudioFile("Alert.mp3");
  
              }
          }
  
      }
  }
  </code></pre>
  
  <p>the code crashes me at this point of the iOS AudioService.cs file:
  <a href="https://i.stack.imgur.com/wHAkD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wHAkD.jpg" alt="enter image description here"></a></p>
  
  <p>I think the problem lies in the info.plist (although I am most likely wrong) but I don't know how to solve it :(
  can someone help me? thank you</p>