Advertisement
FrayxRulez

Untitled

Nov 21st, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. public class MicRecorder
  2. {
  3. private MediaCapture mediaCaptureManager;
  4. private StorageFile recordStorageFile;
  5.  
  6. private bool isAudioInizialized = false;
  7. private MicRecorderStatus status = MicRecorderStatus.NotRecording;
  8.  
  9. private async void _mediaCaptureManager_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
  10. {
  11. await Stop(errorEventArgs.Message);
  12. }
  13.  
  14. private async void _mediaCaptureManager_RecordLimitationExceeded(MediaCapture sender)
  15. {
  16. await Stop("limitation_error");
  17. }
  18.  
  19. public async Task InitializeAudioRecording()
  20. {
  21. if (isAudioInizialized)
  22. {
  23. throw new MicRecorderException("InitializeAudioRecording is already inizialized.");
  24. }
  25. else
  26. {
  27. try
  28. {
  29. mediaCaptureManager = new MediaCapture();
  30.  
  31. var settings = new MediaCaptureInitializationSettings();
  32. settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
  33. settings.MediaCategory = MediaCategory.Other;
  34. settings.AudioProcessing = AudioProcessing.Default;
  35.  
  36. await mediaCaptureManager.InitializeAsync(settings);
  37.  
  38.  
  39. mediaCaptureManager.RecordLimitationExceeded += _mediaCaptureManager_RecordLimitationExceeded;
  40. mediaCaptureManager.Failed += _mediaCaptureManager_Failed;
  41.  
  42. isAudioInizialized = true;
  43. }
  44. catch (UnauthorizedAccessException e1)
  45. {
  46. throw new MicRecorderException("Please add Microphone in capatibilies tab.", e1);
  47. }
  48. catch (Exception e2)
  49. {
  50. throw new MicRecorderException("Internal Exception", e2);
  51. }
  52. }
  53. }
  54.  
  55. public async Task Start(StorageFolder folder, string filename)
  56. {
  57. if (!isAudioInizialized)
  58. {
  59. throw new MicRecorderException("InitializeAudioRecording is not inizialized.");
  60.  
  61. }
  62. else if (status == MicRecorderStatus.Recording)
  63. {
  64. throw new MicRecorderException("MicRecorder is already recording.");
  65. }
  66. else
  67. {
  68. try
  69. {
  70. recordStorageFile = await folder.CreateFileAsync(filename + ".mp4", CreationCollisionOption.ReplaceExisting);
  71. //MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
  72. MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
  73. await mediaCaptureManager.StartRecordToStorageFileAsync(recordProfile, recordStorageFile);
  74.  
  75. status = MicRecorderStatus.Recording;
  76. }
  77. catch (Exception e)
  78. {
  79. throw new MicRecorderException("Internal Exception", e);
  80. }
  81. }
  82. }
  83.  
  84. public async Task Stop(string er = null)
  85. {
  86. if (!isAudioInizialized)
  87. {
  88. throw new MicRecorderException("InitializeAudioRecording is not inizialized.");
  89. }
  90. else if (status == MicRecorderStatus.NotRecording)
  91. {
  92. throw new MicRecorderException("MicRecorder is not recording.");
  93. }
  94. else
  95. {
  96. try
  97. {
  98. await mediaCaptureManager.StopRecordAsync();
  99.  
  100. status = MicRecorderStatus.NotRecording;
  101. }
  102. catch (Exception e)
  103. {
  104. throw new MicRecorderException("Internal Exception", e);
  105. }
  106. }
  107.  
  108. if (er != null)
  109. {
  110. throw new MicRecorderException(er);
  111. }
  112. }
  113.  
  114. public MicRecorderStatus GetStatus()
  115. {
  116. return status;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement