IOS development-audio recording, ios audio --

Source: Internet
Author: User

IOS development-audio recording, ios audio --

This article introduces the recording.

 

Content outline:

1. Recording application scenarios.

2. recording function implementation.

3. Concept supplement.

4. development experience.

 

Body:

 

1. Recording application scenarios

① Language chat: in instant messaging apps, such as instant messaging apps, such as QQ, etc., all have the voice sending function.

② Voice memo: recording an audio clip to record a specific event.

 

2. recording function implementation

① Import the AVFoundation framework

This framework is basically used for multimedia processing.

② Use AVAudioRecorder for recording

<1> Create a recording file storage path

<2> set the recording attachment settings (# import <AVFoundation/AVAudioSettings. h>)

2-1. NSMutableDictionary * recordSettings = [[NSMutableDictionary alloc] init];

2-2. Set the encoding format:

[RecordSettings setValue: [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];

2-3. Sampling Rate:

[Recordsetasksetvalue: [NSNumber numberWithFloat: 11025.0] forKey: AVSampleRateKey];

2-4. Number of channels:

[Recordsetasksetvalue: [NSNumber numberWithInt: 2] forKey: AVNumberOfChannelsKey];

2-5. Audio Quality and sampling quality:

[RecordSettings setValue: [NSNumber numberWithInt: AVAudioQualityMin] forKey: AVEncoderAudioQualityKey];

<3> Create a recording object based on the path and settings

_ AudioRecorder = [[AVAudioRecorder alloc] initWithURL: url settings: recordset1_error: nil];

<4> prepare the recording

[Self. audioRecorder prepareToRecord];

<5> Start recording

[Self. audioRecorder record];

<6> pause recording

[Self. audioRecorder pause];

<7> stop recording

[Self. audioRecorder stop];

③ Code implementation:

<1> first, three button listeners are implemented on the storyboard:

      

<2> implementation code:

1 # import "ViewController. h "2 # import <AVFoundation/AVFoundation. h> 3 4 @ interface ViewController () 5 6/** recording object */7 @ property (nonatomic, strong) AVAudioRecorder * recorder; 8 9 @ end10 11 @ implementation ViewController12 13-(void) viewDidLoad {14 [self createRecord]; 15} 16 17-(void) createRecord {18 // 0.1 create a recording file storage path 19 NSString * path = [[NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "test. caf "]; 20 NSLog (@" % @ ", path); 21 NSURL * url = [NSURL URLWithString: path]; 22 23 // 0.2 create a recording set 24 NSMutableDictionary * recordsetiterator = [[audio alloc] init]; 25 // set the encoding format 26 [recordSettings setValue: [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; 27 // sampling rate 28 [recordset1_setvalue: [NSNumber numberWithFloat: 11025.0] forKey: AVSampleRateKey]; 29 // number of channels 30 [recordset1_setvalue: [NSNumber numberWithInt: 2] forKey: AVNumberOfChannelsKey]; 31 // audio quality, sampling quality 32 [recordset1_setvalue: [NSNumber numberWithInt: AVAudioQualityMin] forKey: AVEncoderAudioQualityKey]; 33 34 35 // 1. create a recording object 36 self. recorder = [[AVAudioRecorder alloc] initWithURL: url settings: recordsetaskerror: nil]; 37 38 39 // 2. prepare the recording (the system allocates some recording resources) 40 [self. recorder prepareToRecord]; 41} 42 43-(IBAction) beginRecord44 {45 NSLog (@ "Start recording"); 46 [self. recorder record]; // directly recording, you need to manually stop 47 // [self. recorder recordForDuration: 3]; // starts recording from the current execution of this line of code, recording for 5 seconds 48 // [recorder recordAtTime: recorder. deviceCurrentTime + 2]; // 2 s, you need to manually stop 49 // [self. recorder recordAtTime: self. recorder. deviceCurrentTime + 2 forDuration: 3]; // 2 s 3s50} 51 52-(IBAction) pauseRecord :( id) sender {53 NSLog (@ "Pause recording"); 54 [self. recorder pause]; 55} 56 57-(IBAction) stopRecord {58 NSLog (@ "stop recording"); 59 [self. recorder stop]; 60} 61 62 @ end

<3> after the notification recording, you can find the corresponding recording file through the printed path. You can test it by yourself.

 

3. Concept supplement

① Encoding

Concept: encoding refers to the process of converting information from one form to another.

Encoding format:

> PCM: pulse-Coded Modulation (PCM) is a non-Compressed digital audio technology and a non-compressed original audio reproduction. In digital mode, the initial audio signal is PCM.

> MP3

> AAC: AAC is short for "advanced audio coding". It is designed to replace the MP3 format.

> HE-AAC: HE-AAC is a superset of AAC, which represents "High efficiency ". HE-AAC is an audio encoding format specially optimized for Low Bit Rate

> AMR: The full name of AMR is "Adaptive Multi-Rate". It is also another encoding format specially optimized for "speech" and is also suitable for Low Bit Rate environments.

> ALAC: The full name is "Apple Lossless". This is an audio encoding method without any quality loss, that is, Lossless compression.

> IMA4: This is a 16-bit audio file compressed.

② File format (different file formats can save files encoded in different encoding formats)

> WAV

Features: the best sound quality format, corresponding to PCM encoding.

Applicable to: multimedia development and storage of music and sound effects.

> MP3

Features: sound quality, high compression ratio, and supported by a large number of software and hardware.

Applicable: Suitable for music appreciation with high requirements.

> Caf

Features: Applicable to almost all encoding formats in iOS.

③ Reference material address:

Http://baike.baidu.com/link? Url = Response

 

Reprinted with the source: http://www.cnblogs.com/goodboy-heyang/p/5374322.html, respect for labor success.

 

 

 

 

4. development experience

Caf file format, because of some encoding settings, the file may be very large, and caf, the format is not very common, so in the development process, usually compressed transcoding, MP3.

Learning Materials: http://blog.csdn.net/ysy441088327/article/details/7392842

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.