Ios_37_recording, iOS_37 recording
Recorder Parameters
Strongly Coupled Controller
//// BeyondViewController. m // 37 _ recording /// Created by beyond on 14-9-16. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "BeyondViewController. h "// The recording must be imported into this framework # import <AVFoundation/AVFoundation. h> @ interface BeyondViewController () # pragma mark-member attributes // recorder @ property (nonatomic, strong) AVAudioRecorder * recorder; // clock (listener ???) @ Property (nonatomic, strong) CADisplayLink * link; // flag is used to mark the silent duration and determine the conditions for stopping the recording. @ property (nonatomic, assign) double slientDuration; # pragma mark-interface link // start recording-(IBAction) startRecord; // stop recording-(IBAction) stopRecord; // play the recording (using SongTool)-(IBAction) startPlay; @ end @ implementation BeyondViewController # pragma mark-lazy loading-(CADisplayLink *) link {if (! _ Link) {self. link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (update)];} return _ link;} # pragma mark-connection method // start recording-(IBAction) startRecord {// 1. when creating a recorder, you must specify the fileURL to save the file and the recording initialization parameter [self setupAudioRecord]; // 4. enable the timer self. slientDuration = 0; [self. link addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode];} // core code ~ ~ ~ To create a recorder, you must specify the fileURL to save the file and the recording initialization parameter-(void) setupAudioRecord {// 1. specify the FileURL NSString * path = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "test. caf "]; NSURL * url = [NSURL fileURLWithPath: path]; // 2. set the recorder's sampling parameter (dictionary) NSMutableDictionary * setting = [NSMutableDictionary dictionary]; // audio format setting [AVFormatIDKey] = @ (kAudioFormatAppleIMA4 ); // audio sampling rate setting [AVSampleRateKey] = @ (8000.0); // number of audio channels setting [AVNumberOfChannelsKey] = @ (1 ); // setting [AVLinearPCMBitDepthKey] = @ (8); // 3. based on the preceding two condition parameters, you can create a recorder AVAudioRecorder * recorder = [[AVAudioRecorder alloc] initWithURL: url settings: setting error: nil]; // 4. set the recorder attributes, such as allowing the measurement of decibels (must be set) recorder. meteringEnabled = YES; // 5. recorder preparation buffer [recorder prepareToRecord]; // 6. recorder recording [recorder record]; self. recorder = recorder;} // stop recording-(IBAction) stopRecord {[self. recorder stop];} // call (IBAction) startPlay {}# pragma mark-clock method // automatically stops recording Based on the silent duration-(void) update {// 1. the decibel value [self. recorder updateMeters]; // 2. in order to obtain the average score (about-50 in quiet time and about-10 in loud speech) float power = [self. recorder averagePowerForChannel: 0]; // 3. if it is less than-30, start to mute if (power <-30) {// mark, note the unintentional duration of self. slientDuration + = self. link. duration; // if the silence lasts for at least 2 seconds, it indicates that the speech ends if (self. slientDuration> = 2) {// stop recording [self. recorder stop]; // stop the timer and leave it blank [self. link invalidate]; self. link = nil; NSLog (@ "-------- stop recording") ;}} else {// note that a loud noise is being made and self is marked. slientDuration = 0; NSLog (@ "*********** shouting");} @ end
Recording tools
//// RecordTool. h // 37 _ recording /// Created by beyond on 14-9-16. // Copyright (c) 2014 com. beyond. all rights reserved. // recording tool class # import <Foundation/Foundation. h> // The recording must be imported into this framework # import <AVFoundation/AVFoundation. h> @ interface RecordTool: NSObject // you can use the class method to quickly return a recorder object. The parameter is: target file storage name + (AVAudioRecorder *) record :( NSString *) destFileName; @ end
//// RecordTool. m // 37 _ recording /// Created by beyond on 14-9-16. // Copyright (c) 2014 com. beyond. all rights reserved. // recording tool class # import "RecordTool. h "@ implementation RecordTool // you can use a class method to quickly return a recorder object. The parameter is: target file storage name + (AVAudioRecorder *) record :( NSString *) destFileName {// core code ~ ~ ~ When creating a recorder, you must specify the fileURL for storing the file and the initialization parameter of the recording. // 1. specify the FileURL NSString * path for storing the recording file = [[secure (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: destFileName]; NSURL * url = [NSURL fileURLWithPath: path]; // 2. set the recorder's sampling parameter (dictionary) NSMutableDictionary * setting = [NSMutableDictionary dictionary]; // audio format setting [AVFormatIDKey] = @ (kAudioFormatAppleIMA4 ); // audio sampling rate setting [AVSampleRateKey] = @ (8000.0); // number of audio channels setting [AVNumberOfChannelsKey] = @ (1 ); // setting [AVLinearPCMBitDepthKey] = @ (8); // 3. based on the preceding two condition parameters, you can create a recorder AVAudioRecorder * recorder = [[AVAudioRecorder alloc] initWithURL: url settings: setting error: nil]; // 4. set the recorder attributes, such as allowing the measurement of decibels (must be set) recorder. meteringEnabled = YES; // 5. recorder preparation buffer [recorder prepareToRecord]; // 6. recorder start recording // [recorder record]; return recorder ;}@ end
How does ios achieve recording?
NSError * err = nil; NSURL * url = [NSURL fileURLWithPath: [[NSHomeDirectory () Events: @ "events/useraudios"] events: [currentSentenceEng stringByAppendingString :@". caf "]; NSMutableDictionary * settings = [invalid dictionary]; [settings setValue: [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; [settings setValue: [NSNumber numberWithFloat: 44100.0] forKey: AVSampleRateKey]; [settings setValue: [NSNumber numberWithInt: 2] forKey: Secret]; [settings setValue: [NSNumber numberWithInt: AVAudioQualityMin] forKey: AVEncoderAudioQualityKey] if (recorder = nil) {recorder = [[AVAudioRecorder alloc] initWithURL: url settings: settings error: & err];} [recorder setDelegate: self]; [recorder prepareToRecord]; [recorder record];
How can I perform recording on iOS?
You can use the Audio Toolbox framework in iOS to implement this function.