您好,登錄后才能下訂單哦!
#import <AVFoundation/AVFoundation.h> 需要引入
- //獲取document目錄的路徑
- view plain
- - (NSString*) documentsPath {
- if (! _documentsPath) {
- NSArray *searchPaths =
- NSSearchPathForDirectoriesInDomains
- (NSDocumentDirectory, NSUserDomainMask, YES);
- _documentsPath = [searchPaths objectAtIndex: 0];
- [_documentsPath retain];
- }
- return _documentsPath;
- }
- //(document目錄的路徑)
- NSString *destinationString = [[self documentsPath]
- stringByAppendingPathComponent:filenameField.text];
- NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
- //初始化AVAudioRecorder
- NSError *recorderSetupError = nil;
- AVAudioRecorder audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL
- settings:recordSettings error:&recorderSetupError];
- [recordSettings release];
第二個(gè)參數(shù) settings是一個(gè)容納鍵值對(duì)的NSDictionary有四種一般的鍵
1:一般的音頻設(shè)置
2:線性PCM設(shè)置
3:編碼器設(shè)置
4:采樣率轉(zhuǎn)換設(shè)置
NSMutableDictionary 需要加入五個(gè)設(shè)置值(線性PCM)
- NSMutableDictionary *recordSettings =
- [[NSMutableDictionary alloc] initWithCapacity:10];
- //1 ID號(hào)
- [recordSettings setObject:
- [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
- float sampleRate =
- [pcmSettingsViewController.sampleRateField.text floatValue];
- //2 采樣率
- [recordSettings setObject:
- [NSNumber numberWithFloat:sampleRate] forKey: AVSampleRateKey];
-
- //3 通道的數(shù)目
- [recordSettings setObject:
- [NSNumber numberWithInt:
- (pcmSettingsViewController.stereoSwitch.on ? 2 : 1)]
- forKey:AVNumberOfChannelsKey];
- int bitDepth =
- [pcmSettingsViewController.sampleDepthField.text intValue];
-
- //4 采樣位數(shù) 默認(rèn) 16
- [recordSettings setObject:
- [NSNumber numberWithInt:bitDepth] forKey:AVLinearPCMBitDepthKey];
-
- //5
- [recordSettings setObject:
- [NSNumber numberWithBool:
- pcmSettingsViewController.bigEndianSwitch.on]
- forKey:AVLinearPCMIsBigEndianKey];
-
- //6 采樣信號(hào)是整數(shù)還是浮點(diǎn)數(shù)
- [recordSettings setObject:
- [NSNumber numberWithBool:
- pcmSettingsViewController.floatingSamplesSwitch.on]
- forKey:AVLinearPCMIsFloatKey]
-
-
- ;
AVAudioRecorder成功創(chuàng)建后,使用他非常直接.它的三個(gè)基本方法如下
- -(void) startRecording {
- [audioRecorder record];
- }
- -(void) pauseRecording {
- [audioRecorder pause];
- recordPauseButton.selected = NO;
- }
- -(void) stopRecording {
- [audioRecorder stop];
- }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。