IOS開發實現錄音功能

來源:互聯網
上載者:User

標籤:

匯入架構:

?
1 #import <AVFoundation/AVFoundation.h>

聲明全域變數:

?
12345 @interface ViewController ()<AVAudioRecorderDelegate>{  AVAudioRecorder *audioRecorder;}@end


在ViewDidLoad中:

?
123456 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); [button setTitle:@"TICK" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; [button addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];

按鈕的觸發事件

?
123456789101112131415161718192021222324252627282930313233343536 - (void)startAudioRecoder:(UIButton *)sender{  sender.selected = !sender.selected;  if (sender.selected != YES) {    [audioRecorder stop];    return;  }     //  URL是本地的URL AVAudioRecorder需要一個儲存的路徑  NSString *name = [NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970];     NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];  NSError *error;  //  錄音機 初始化  audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];  [audioRecorder prepareToRecord];  [audioRecorder record];  audioRecorder.delegate = self;  /*   1.AVNumberOfChannelsKey 通道數 通常為雙聲道 值2   2.AVSampleRateKey 採樣率 單位HZ 通常設定成44100 也就是44.1k   3.AVLinearPCMBitDepthKey 位元速率 8 16 24 32   4.AVEncoderAudioQualityKey 聲音品質       ① AVAudioQualityMin  = 0, 最小的品質       ② AVAudioQualityLow  = 0x20, 比較低的品質       ③ AVAudioQualityMedium = 0x40, 中間的品質       ④ AVAudioQualityHigh  = 0x60,高的品質       ⑤ AVAudioQualityMax  = 0x7F 最好的品質   5.AVEncoderBitRateKey 音頻編碼的位元速率 單位Kbps 傳輸的速率 一般設定128000 也就是128kbps       */           NSLog(@"%@",path); }

代理方法:

?
1234567891011121314151617181920212223 - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{  NSLog(@"錄音結束");//  檔案操作的類 NSFileManager *manger = [NSFileManager defaultManager];   NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];//  獲得當前檔案的所有子檔案subpathsAtPath  NSArray *pathlList = [manger subpathsAtPath:path]; //  需要只獲得錄音檔案  NSMutableArray *audioPathList = [NSMutableArray array];//  遍曆所有這個檔案夾下的子檔案  for (NSString *audioPath in pathlList) {//    通過對比檔案的延展名(副檔名 尾綴) 來區分是不是錄音檔案    if ([audioPath.pathExtension isEqualToString:@"aiff"]) {//      把篩選出來的檔案放到數組中      [audioPathList addObject:audioPath];    }  }     NSLog(@"%@",audioPathList);   }

IOS開發實現錄音功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.