async Task RecordAudio() { try { if (!recorder.IsRecording) { recorder.StopRecordingOnSilence = TimeoutSwitch.On; RecordButton.Enabled = false; await recorder.StartRecording(); RecordButton.SetTitle("Stop", UIControlState.Normal); RecordButton.Enabled = true; } else { RecordButton.Enabled = false; await recorder.StopRecording(); RecordButton.SetTitle("Record", UIControlState.Normal); RecordButton.Enabled = true; } } catch (Exception ex) { //blow up the app! throw ex; } }
public void DidStartRecording(AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject[] connections) { // Enable the Record button to let the user stop the recording. DispatchQueue.MainQueue.DispatchAsync(() => { RecordButton.Enabled = true; RecordButton.SetTitle("Stop", UIControlState.Normal); }); }
private void Recorder_AudioInputReceived(object sender, string audioFile) { InvokeOnMainThread(() => { RecordButton.SetTitle("Record", UIControlState.Normal); PlayButton.Enabled = !string.IsNullOrEmpty(audioFile); }); }
void OnRecordingChanged(NSObservedChange change) { bool isRecording = ((NSNumber)change.NewValue).BoolValue; DispatchQueue.MainQueue.DispatchAsync(() => { if (isRecording) { CameraButton.Enabled = false; RecordButton.Enabled = true; RecordButton.SetTitle("Stop", UIControlState.Normal); } else { // Only enable the ability to change camera if the device has more than one camera. CameraButton.Enabled = NumberOfVideoCameras() > 1; RecordButton.Enabled = true; RecordButton.SetTitle("Record", UIControlState.Normal); } }); }
async Task RecordAudio() { try { if (!recorder.IsRecording) { recorder.StopRecordingOnSilence = TimeoutSwitch.On; RecordButton.Enabled = false; PlayButton.Enabled = false; //the returned Task here will complete once recording is finished var recordTask = await recorder.StartRecording(); RecordButton.SetTitle("Stop", UIControlState.Normal); RecordButton.Enabled = true; var audioFile = await recordTask; //audioFile will contain the path to the recorded audio file RecordButton.SetTitle("Record", UIControlState.Normal); PlayButton.Enabled = !string.IsNullOrEmpty(audioFile); } else { RecordButton.Enabled = false; await recorder.StopRecording(); RecordButton.SetTitle("Record", UIControlState.Normal); RecordButton.Enabled = true; } } catch (Exception ex) { //blow up the app! throw ex; } }
public void FinishedRecording(AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject[] connections, NSError error) { // Note that currentBackgroundRecordingID is used to end the background task associated with this recording. // This allows a new recording to be started, associated with a new UIBackgroundTaskIdentifier, once the movie file output's isRecording property // is back to false — which happens sometime after this method returns. // Note: Since we use a unique file path for each recording, a new recording will not overwrite a recording currently being saved. Action cleanup = () => { var path = outputFileUrl.Path; if (NSFileManager.DefaultManager.FileExists(path)) { NSError err; if (!NSFileManager.DefaultManager.Remove(path, out err)) { Console.WriteLine($"Could not remove file at url: {outputFileUrl}"); } } var currentBackgroundRecordingID = backgroundRecordingID; if (currentBackgroundRecordingID != -1) { backgroundRecordingID = -1; UIApplication.SharedApplication.EndBackgroundTask(currentBackgroundRecordingID); } }; bool success = true; if (error != null) { Console.WriteLine($"Movie file finishing error: {error.LocalizedDescription}"); success = ((NSNumber)error.UserInfo[AVErrorKeys.RecordingSuccessfullyFinished]).BoolValue; } if (success) { // Check authorization status. PHPhotoLibrary.RequestAuthorization(status => { if (status == PHAuthorizationStatus.Authorized) { // Save the movie file to the photo library and cleanup. PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => { var options = new PHAssetResourceCreationOptions { ShouldMoveFile = true }; var creationRequest = PHAssetCreationRequest.CreationRequestForAsset(); creationRequest.AddResource(PHAssetResourceType.Video, outputFileUrl, options); }, (success2, error2) => { if (!success2) { Console.WriteLine($"Could not save movie to photo library: {error2}"); } cleanup(); }); } else { cleanup(); } }); } else { cleanup(); } // Enable the Camera and Record buttons to let the user switch camera and start another recording. DispatchQueue.MainQueue.DispatchAsync(() => { // Only enable the ability to change camera if the device has more than one camera. CameraButton.Enabled = UniqueDevicePositionsCount(videoDeviceDiscoverySession) > 1; RecordButton.Enabled = true; CaptureModeControl.Enabled = true; RecordButton.SetTitle("Record", UIControlState.Normal); }); }