Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MicRecorder
- {
- private MediaCapture mediaCaptureManager;
- private StorageFile recordStorageFile;
- private bool isAudioInizialized = false;
- private MicRecorderStatus status = MicRecorderStatus.NotRecording;
- private async void _mediaCaptureManager_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
- {
- await Stop(errorEventArgs.Message);
- }
- private async void _mediaCaptureManager_RecordLimitationExceeded(MediaCapture sender)
- {
- await Stop("limitation_error");
- }
- public async Task InitializeAudioRecording()
- {
- if (isAudioInizialized)
- {
- throw new MicRecorderException("InitializeAudioRecording is already inizialized.");
- }
- else
- {
- try
- {
- mediaCaptureManager = new MediaCapture();
- var settings = new MediaCaptureInitializationSettings();
- settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
- settings.MediaCategory = MediaCategory.Other;
- settings.AudioProcessing = AudioProcessing.Default;
- await mediaCaptureManager.InitializeAsync(settings);
- mediaCaptureManager.RecordLimitationExceeded += _mediaCaptureManager_RecordLimitationExceeded;
- mediaCaptureManager.Failed += _mediaCaptureManager_Failed;
- isAudioInizialized = true;
- }
- catch (UnauthorizedAccessException e1)
- {
- throw new MicRecorderException("Please add Microphone in capatibilies tab.", e1);
- }
- catch (Exception e2)
- {
- throw new MicRecorderException("Internal Exception", e2);
- }
- }
- }
- public async Task Start(StorageFolder folder, string filename)
- {
- if (!isAudioInizialized)
- {
- throw new MicRecorderException("InitializeAudioRecording is not inizialized.");
- }
- else if (status == MicRecorderStatus.Recording)
- {
- throw new MicRecorderException("MicRecorder is already recording.");
- }
- else
- {
- try
- {
- recordStorageFile = await folder.CreateFileAsync(filename + ".mp4", CreationCollisionOption.ReplaceExisting);
- //MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
- MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
- await mediaCaptureManager.StartRecordToStorageFileAsync(recordProfile, recordStorageFile);
- status = MicRecorderStatus.Recording;
- }
- catch (Exception e)
- {
- throw new MicRecorderException("Internal Exception", e);
- }
- }
- }
- public async Task Stop(string er = null)
- {
- if (!isAudioInizialized)
- {
- throw new MicRecorderException("InitializeAudioRecording is not inizialized.");
- }
- else if (status == MicRecorderStatus.NotRecording)
- {
- throw new MicRecorderException("MicRecorder is not recording.");
- }
- else
- {
- try
- {
- await mediaCaptureManager.StopRecordAsync();
- status = MicRecorderStatus.NotRecording;
- }
- catch (Exception e)
- {
- throw new MicRecorderException("Internal Exception", e);
- }
- }
- if (er != null)
- {
- throw new MicRecorderException(er);
- }
- }
- public MicRecorderStatus GetStatus()
- {
- return status;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement