/// <summary> /// Handles result of audio recording tasks /// </summary> /// <param name="sender"></param> /// <param name="e">stores information about current captured audio</param> private void audioRecordingTask_Completed(object sender, AudioResult e) { if (e.Error != null) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR)); return; } switch (e.TaskResult) { case TaskResult.OK: try { // Get image data MediaFile data = new MediaFile(e.AudioFileName, e.AudioFile); this.files.Add(data); if (files.Count < this.captureAudioOptions.Limit) { audioCaptureTask.Show(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error capturing audio.")); } break; case TaskResult.Cancel: if (files.Count > 0) { // User canceled operation, but some audio clips were made DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Canceled.")); } break; default: if (files.Count > 0) { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Did not complete!")); } break; } }
/// <summary> /// Handles Take button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTake_Click(object sender, RoutedEventArgs e) { this.result = this.SaveAudioClipToLocalStorage(); if (this.NavigationService.CanGoBack) { this.NavigationService.GoBack(); } }
/// <summary> /// Writes audio data from memory to isolated storage /// </summary> /// <returns></returns> private AudioResult SaveAudioClipToLocalStorage() { if (this.memoryStream == null || this.memoryStream.Length <= 0) { return(new AudioResult(TaskResult.Cancel)); } this.UpdateWavHeader(this.memoryStream); // save audio data to local isolated storage string filename = String.Format(FileNameFormat, Guid.NewGuid().ToString()); try { using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isoFile.DirectoryExists(LocalFolderName)) { isoFile.CreateDirectory(LocalFolderName); } string filePath = System.IO.Path.Combine("/" + LocalFolderName + "/", filename); this.memoryStream.Seek(0, SeekOrigin.Begin); using (IsolatedStorageFileStream fileStream = isoFile.CreateFile(filePath)) { this.memoryStream.CopyTo(fileStream); } AudioResult result = new AudioResult(TaskResult.OK); result.AudioFileName = filePath; result.AudioFile = this.memoryStream; result.AudioFile.Seek(0, SeekOrigin.Begin); return(result); } } catch (Exception) { //TODO: log or do something else throw; } }
/// <summary> /// Writes audio data from memory to isolated storage /// </summary> /// <returns></returns> private AudioResult SaveAudioClipToLocalStorage() { if (this.memoryStream == null || this.memoryStream.Length <= 0) { return new AudioResult(TaskResult.Cancel); } this.UpdateWavHeader(this.memoryStream); // save audio data to local isolated storage string filename = String.Format(FileNameFormat, Guid.NewGuid().ToString()); try { using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isoFile.DirectoryExists(LocalFolderName)) { isoFile.CreateDirectory(LocalFolderName); } string filePath = System.IO.Path.Combine("/" + LocalFolderName + "/", filename); this.memoryStream.Seek(0, SeekOrigin.Begin); using (IsolatedStorageFileStream fileStream = isoFile.CreateFile(filePath)) { this.memoryStream.CopyTo(fileStream); } AudioResult result = new AudioResult(TaskResult.OK); result.AudioFileName = filePath; result.AudioFile = this.memoryStream; result.AudioFile.Seek(0, SeekOrigin.Begin); return result; } } catch (Exception) { //TODO: log or do something else throw; } }
/// <summary> /// Handles result of audio recording tasks /// </summary> /// <param name="sender"></param> /// <param name="e">stores information about current captured audio</param> private void audioRecordingTask_Completed(object sender, AudioResult e) { if (e.Error != null) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR)); return; } switch (e.TaskResult) { case TaskResult.OK: try { // Get image data MediaFile data = new MediaFile(e.AudioFileName, e.AudioFile); this.files.Add(data); if (files.Count < this.captureAudioOptions.Limit) { audioCaptureTask.Show(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } } catch (Exception ex) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error capturing audio.")); } break; case TaskResult.Cancel: if (files.Count > 0) { // User canceled operation, but some audio clips were made DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Canceled.")); } break; default: if (files.Count > 0) { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files, "navigator.device.capture._castMediaFile")); files.Clear(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Did not complete!")); } break; } }