AudioRecorderManager.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // AudioRecorderManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2025/3/28.
  6. //
  7. #import "AudioRecorderManager.h"
  8. @implementation AudioRecorderManager
  9. + (instancetype)sharedManager {
  10. static AudioRecorderManager *sharedInstance = nil;
  11. static dispatch_once_t onceToken;
  12. dispatch_once(&onceToken, ^{
  13. sharedInstance = [[self alloc] init];
  14. });
  15. return sharedInstance;
  16. }
  17. - (instancetype)init {
  18. self = [super init];
  19. if (self) {
  20. [self setupAudioSession];
  21. }
  22. return self;
  23. }
  24. - (void)setupAudioSession {
  25. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  26. NSError *error = nil;
  27. [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
  28. [audioSession setActive:YES error:&error];
  29. if (error) {
  30. NSLog(@"Audio Session error: %@", error.localizedDescription);
  31. }
  32. }
  33. - (void)startRecording {
  34. if ([self isRecording]) {
  35. [self stopRecording];
  36. return;
  37. }
  38. // 设置录音文件路径
  39. NSString *documentsPath = kSHPath_Record;
  40. NSString*fileName = [AudioRecorderManager sharedManager].curRecordName;
  41. if(!fileName){
  42. fileName = @"1111.m4a";
  43. }
  44. self.recordedFilePath = [documentsPath stringByAppendingPathComponent:fileName];
  45. NSURL *outputFileURL = [NSURL fileURLWithPath:self.recordedFilePath];
  46. // 录音设置
  47. NSDictionary *recordSettings = @{
  48. AVFormatIDKey: @(kAudioFormatMPEG4AAC),
  49. AVSampleRateKey: @44100.0,
  50. AVNumberOfChannelsKey: @2,
  51. AVEncoderAudioQualityKey: @(AVAudioQualityHigh)
  52. };
  53. NSError *error = nil;
  54. self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSettings error:&error];
  55. self.audioRecorder.delegate = self;
  56. if (error) {
  57. NSLog(@"Audio Recorder error: %@", error.localizedDescription);
  58. } else {
  59. [self.audioRecorder prepareToRecord];
  60. [self.audioRecorder record];
  61. NSLog(@"Recording started");
  62. }
  63. }
  64. - (void)stopRecording {
  65. if ([self isRecording]) {
  66. [self.audioRecorder stop];
  67. self.audioRecorder = nil;
  68. NSLog(@"Recording stopped");
  69. }
  70. }
  71. - (void)playRecording {
  72. if ([self isPlaying]) {
  73. [self stopPlaying];
  74. return;
  75. }
  76. if (!self.recordedFilePath) {
  77. NSLog(@"No recording to play");
  78. return;
  79. }
  80. NSError *error = nil;
  81. self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.recordedFilePath] error:&error];
  82. self.audioPlayer.numberOfLoops = 0;
  83. if (error) {
  84. NSLog(@"Audio Player error: %@", error.localizedDescription);
  85. } else {
  86. [self.audioPlayer play];
  87. NSLog(@"Playing recording");
  88. }
  89. }
  90. - (void)stopPlaying {
  91. if ([self isPlaying]) {
  92. [self.audioPlayer stop];
  93. self.audioPlayer = nil;
  94. NSLog(@"Playback stopped");
  95. }
  96. }
  97. - (BOOL)isRecording {
  98. return self.audioRecorder && self.audioRecorder.isRecording;
  99. }
  100. - (BOOL)isPlaying {
  101. return self.audioPlayer && self.audioPlayer.isPlaying;
  102. }
  103. #pragma mark - AVAudioRecorderDelegate
  104. - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
  105. if (flag) {
  106. NSLog(@"Recording finished successfully");
  107. } else {
  108. NSLog(@"Recording failed");
  109. }
  110. self.audioRecorder = nil;
  111. }
  112. @end