using Android.Widget; using Plugin.AudioRecorder; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.PlatformConfiguration; using Xamarin.Forms.Xaml; namespace MST.MobileApp.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class RecordAudioPage : ContentPage { AudioRecorderService recorder { get; set; } AudioPlayer player { get; set; } string path { get; set; } public RecordAudioPage() { InitializeComponent(); recorder = new AudioRecorderService { //FilePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), //StopRecordingOnSilence = true, //will stop recording after 2 seconds (default) //StopRecordingAfterTimeout = true, //stop recording after a max timeout (defined below) TotalAudioTimeout = TimeSpan.FromSeconds(15) //audio will stop recording after 15 seconds }; player = new AudioPlayer(); player.FinishedPlaying += Player_FinishedPlaying; } public async void Record_Clicked(object sender, EventArgs e) { await RecordAudio(); } public async Task RecordAudio() { try { if (!recorder.IsRecording) //Record button clicked { //recorder.StopRecordingOnSilence = !TimeoutSwitch.IsToggled; RecordButton.IsEnabled = false; PlayButton.IsEnabled = false; //start recording audio var audioRecordTask = await recorder.StartRecording(); RecordButton.Text = "Stop Recording"; RecordButton.IsEnabled = true; var audioFile = await audioRecordTask; RecordButton.Text = "Record"; if (audioFile != null) //non-null audioFile indicates audio was successfully recorded { path = audioFile; PlayButton.IsEnabled = true; } } else //Stop button clicked { RecordButton.IsEnabled = false; //stop the recording... await recorder.StopRecording(); //Get the path to save the audio to Application data folder string path1 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //string filename = DateTime.Now.ToShortDateString().Trim().Replace("/", "") + // DateTime.Now.ToShortTimeString().Trim().Replace(":", "").Replace("AM", ""); string fileName = string.Format("audio_{0}.wav", DateTime.Now.ToString("yyyyMMddHHmmss")); string filePath = Path.Combine(path1, "audio_" + fileName); //Create a file and write the stream into it. FileStream audioStreamCache = System.IO.File.Open(recorder.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream fileStream = File.Open(filePath, FileMode.Create); audioStreamCache.Position = 0; //copy the file from cache location to application data folder audioStreamCache.CopyTo(fileStream); audioStreamCache.Dispose(); audioStreamCache.Close(); fileStream.Flush(); fileStream.Close(); path = filePath; RecordButton.IsEnabled = true; } } catch (Exception ex) { //blow up the app! //Toast.MakeText(Android.App.Application.Context, ex.Message, ToastLength.Long).Show(); } } void Play_Clicked(object sender, EventArgs e) { PlayAudio(); } void PlayAudio() { var filePath = recorder.GetAudioFilePath(); var fileStream = recorder.GetAudioFileStream(); try { if (path != null) { PlayButton.IsEnabled = false; RecordButton.IsEnabled = false; player.Play(path); } } catch (Exception ex) { //Toast.MakeText(Android.App.Application.Context, ex.Message, ToastLength.Short).Show(); } } void Player_FinishedPlaying(object sender, EventArgs e) { PlayButton.IsEnabled = true; RecordButton.IsEnabled = true; } private async void BtnClose_Clicked(object sender, EventArgs e) { await Navigation.PopModalAsync(); } } }