EMCDDeviceManager+Microphone.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EMCDDeviceManager+Microphone.h"
  13. #import "EMAudioRecorderUtil.h"
  14. @implementation EMCDDeviceManager (Microphone)
  15. // Check the availability for microphone
  16. - (BOOL)emCheckMicrophoneAvailability{
  17. __block BOOL ret = NO;
  18. AVAudioSession *session = [AVAudioSession sharedInstance];
  19. if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
  20. [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
  21. ret = granted;
  22. }];
  23. } else {
  24. ret = YES;
  25. }
  26. return ret;
  27. }
  28. // Get the audio volumn (0~1)
  29. - (double)emPeekRecorderVoiceMeter{
  30. double ret = 0.0;
  31. if ([EMAudioRecorderUtil recorder].isRecording) {
  32. [[EMAudioRecorderUtil recorder] updateMeters];
  33. //Average volumn [recorder averagePowerForChannel:0];
  34. //Maximum volumn [recorder peakPowerForChannel:0];
  35. double lowPassResults = pow(10, (0.05 * [[EMAudioRecorderUtil recorder] peakPowerForChannel:0]));
  36. ret = lowPassResults;
  37. }
  38. return ret;
  39. }
  40. @end