<|start_filename|>SimpleAudioForms/iOS/Services/AudioService.cs<|end_filename|>
﻿using System.Diagnostics;
using System.IO;
using AVFoundation;
using Foundation;
using SimpleAudioForms.iOS;
using Xamarin.Forms;

[assembly: Dependency(typeof(AudioService))]

namespace SimpleAudioForms.iOS
{
    public class AudioService : IAudio
    {
        private AVAudioPlayer audioPlayer;

        public AudioService() { }

        private bool Play(string fileName, string fileTypeHint)
        {
            var filePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName),
                                                               Path.GetExtension(fileName));
            Debug.Assert(!string.IsNullOrEmpty(filePath));
            var url = NSUrl.FromString(filePath);

            NSError outError;
            NSUrl fileURL = new NSUrl(fileName);
            audioPlayer = new AVAudioPlayer(url: fileURL, fileTypeHint: fileTypeHint, outError: out outError);
            audioPlayer.FinishedPlaying += (object sender, AVStatusEventArgs e) => audioPlayer = null;
            audioPlayer.Play();

            return null == outError;
        }

        #region IAudio
        public bool PlayMp3File(string fileName)
        {
            Debug.WriteLine($"PlayMp3File(string {fileName})");
            return Play(fileName, AVFileType.MpegLayer3);
        }

        public bool PlayWavFile(string fileName)
        {
            Debug.WriteLine($"PlayWavFile(string {fileName})");
            return Play(fileName, AVFileType.Wave);
        }
        #endregion IAudio
    }
}


<|start_filename|>SimpleAudioForms/SimpleAudioForms/SimpleAudioForms.cs<|end_filename|>
﻿using System;

using Xamarin.Forms;

namespace SimpleAudioForms
{
	public class App : Application
	{
		public App ()
		{
			// The root page of your application
			MainPage = new ContentPage {
				Content = new StackLayout {
					VerticalOptions = LayoutOptions.Center,
					Children = {
						new Button {
							Text = "Play *.wav file!",
							Command = new Command(() =>
								{
									DependencyService.Get<IAudio>().PlayWavFile(
										"ding_persevy.wav"
									);
								})
						},
						new Button {
							Text = "Play *.mp3 file!",
							Command = new Command(() =>
								{
									DependencyService.Get<IAudio>().PlayMp3File(
										"test.mp3"
									);
								})
						}
					}
				}
			};
		}

		protected override void OnStart ()
		{
			// Handle when your app starts
		}

		protected override void OnSleep ()
		{
			// Handle when your app sleeps
		}

		protected override void OnResume ()
		{
			// Handle when your app resumes
		}
	}
}



<|start_filename|>SimpleAudioForms/SimpleAudioForms/Services/IAudio.cs<|end_filename|>
﻿using System;

namespace SimpleAudioForms
{
	public interface IAudio
	{
		bool PlayMp3File(string fileName);
		bool PlayWavFile(string fileName);
	}
}

<|start_filename|>SimpleAudioForms/Droid/Services/AudioService.cs<|end_filename|>
﻿using System;
using Xamarin.Forms;
using SimpleAudioForms.Droid;
using Android.Media;

[assembly: Dependency(typeof(AudioService))]

namespace SimpleAudioForms.Droid
{
	public class AudioService : IAudio
	{
		public AudioService() {}

		private MediaPlayer _mediaPlayer;

		public bool PlayMp3File(string fileName)
		{
			_mediaPlayer = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.test);
			_mediaPlayer.Start();

			return true;
		}

		public bool PlayWavFile(string fileName)
		{
			_mediaPlayer = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.ding_persevy);
			_mediaPlayer.Start();

			return true;
		}
	}
}