AudioRecorderManager.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. self.recordedFilePath = [documentsPath stringByAppendingPathComponent:@"recording.m4a"];
  41. NSURL *outputFileURL = [NSURL fileURLWithPath:self.recordedFilePath];
  42. // 录音设置
  43. NSDictionary *recordSettings = @{
  44. AVFormatIDKey: @(kAudioFormatMPEG4AAC),
  45. AVSampleRateKey: @44100.0,
  46. AVNumberOfChannelsKey: @2,
  47. AVEncoderAudioQualityKey: @(AVAudioQualityHigh)
  48. };
  49. NSError *error = nil;
  50. self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSettings error:&error];
  51. self.audioRecorder.delegate = self;
  52. if (error) {
  53. NSLog(@"Audio Recorder error: %@", error.localizedDescription);
  54. } else {
  55. [self.audioRecorder prepareToRecord];
  56. [self.audioRecorder record];
  57. NSLog(@"Recording started");
  58. }
  59. }
  60. - (void)stopRecording {
  61. if ([self isRecording]) {
  62. [self.audioRecorder stop];
  63. self.audioRecorder = nil;
  64. NSLog(@"Recording stopped");
  65. }
  66. }
  67. - (void)playRecording {
  68. if ([self isPlaying]) {
  69. [self stopPlaying];
  70. return;
  71. }
  72. if (!self.recordedFilePath) {
  73. NSLog(@"No recording to play");
  74. return;
  75. }
  76. NSError *error = nil;
  77. self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.recordedFilePath] error:&error];
  78. self.audioPlayer.numberOfLoops = 0;
  79. if (error) {
  80. NSLog(@"Audio Player error: %@", error.localizedDescription);
  81. } else {
  82. [self.audioPlayer play];
  83. NSLog(@"Playing recording");
  84. }
  85. }
  86. - (void)stopPlaying {
  87. if ([self isPlaying]) {
  88. [self.audioPlayer stop];
  89. self.audioPlayer = nil;
  90. NSLog(@"Playback stopped");
  91. }
  92. }
  93. - (BOOL)isRecording {
  94. return self.audioRecorder && self.audioRecorder.isRecording;
  95. }
  96. - (BOOL)isPlaying {
  97. return self.audioPlayer && self.audioPlayer.isPlaying;
  98. }
  99. #pragma mark - AVAudioRecorderDelegate
  100. - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
  101. if (flag) {
  102. NSLog(@"Recording finished successfully");
  103. } else {
  104. NSLog(@"Recording failed");
  105. }
  106. self.audioRecorder = nil;
  107. }
  108. @end