/// <summary> /// Launches our own audio recording control to capture audio /// </summary> /// <param name="options">may contains additional parameters</param> public void captureAudio(string options) { try { try { this.captureAudioOptions = String.IsNullOrEmpty(options) ? CaptureAudioOptions.Default : JSON.JsonHelper.Deserialize<CaptureAudioOptions>(options); } catch (Exception ex) { this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); return; } audioCaptureTask = new AudioCaptureTask(); audioCaptureTask.Completed += audioRecordingTask_Completed; audioCaptureTask.Show(); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }
/// <summary> /// Launches our own audio recording control to capture audio /// </summary> /// <param name="options">may contains additional parameters</param> public void captureAudio(string options) { try { try { this.captureAudioOptions = String.IsNullOrEmpty(options) ? CaptureAudioOptions.Default : JSON.JsonHelper.Deserialize <CaptureAudioOptions>(options); } catch (Exception ex) { this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); return; } audioCaptureTask = new AudioCaptureTask(); audioCaptureTask.Completed += audioRecordingTask_Completed; audioCaptureTask.Show(); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }
public async void captureAudio(string options) { try { try { string args = JSON.JsonHelper.Deserialize <string[]>(options)[0]; this.captureAudioOptions = String.IsNullOrEmpty(args) ? CaptureAudioOptions.Default : JSON.JsonHelper.Deserialize <CaptureAudioOptions>(args); } catch (Exception ex) { this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); return; } var mediaCaputreSettings = new MediaCaptureInitializationSettings(); mediaCaputreSettings.StreamingCaptureMode = StreamingCaptureMode.Audio; audioCaptureTask = new MediaCapture(); await audioCaptureTask.InitializeAsync(mediaCaputreSettings); var mediaEncodingProfile = MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto); var storageFile = await KnownFolders.MusicLibrary.CreateFileAsync("captureAudio.mp3", CreationCollisionOption.GenerateUniqueName); var timer = new Windows.UI.Xaml.DispatcherTimer(); timer.Tick += async delegate(object sender, object e) { timer.Stop(); await audioCaptureTask.StopRecordAsync(); if (storageFile != null) { long size = 0; string modifiedDate = ""; var tasks = new List <Task <BasicProperties> >(); tasks.Add(storageFile.GetBasicPropertiesAsync().AsTask()); var result = await Task.WhenAll(tasks); foreach (var prop in result) { size = (long)prop.Size; modifiedDate = prop.DateModified.ToString(); } string imagePathOrContent = string.Empty; var readStream = await storageFile.OpenAsync(FileAccessMode.Read); var inputStream = readStream.GetInputStreamAt(0); var dataReaderFile = new DataReader(inputStream); var numByteLoaded = await dataReaderFile.LoadAsync((uint)readStream.Size); var byteString = new byte[numByteLoaded]; var imageBase64String = ""; dataReaderFile.ReadBytes(byteString); imageBase64String = Convert.ToBase64String(byteString); imagePathOrContent = "data:audio/mpeg;base64," + imageBase64String; MediaFile data = new MediaFile(imagePathOrContent, storageFile.ContentType, storageFile.Name, size, modifiedDate); this.files.Add(data); if (files.Count < this.captureAudioOptions.Limit) { //dosomething here } else { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files)); files.Clear(); } } }; timer.Interval = TimeSpan.FromMilliseconds(captureAudioDuration * 1000); await audioCaptureTask.StartRecordToStorageFileAsync(mediaEncodingProfile, storageFile); timer.Start(); } catch (Exception ex) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message)); } }