IOS-recording function

Source: Internet
Author: User

Initialize AVAudioRecorder and set the recording parameters and storage address:


[Cpp]
// Recording settings
NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init] autorelease];
// Set the recording format AVFormatIDKey = kAudioFormatLinearPCM
[RecordSetting setValue: [NSNumber numberWithInt: kAudioFormatMPEG4AAC] forKey: AVFormatIDKey];
// Set the recording sampling rate (Hz), for example, AVSampleRateKey = 8000/44100/96000 (which affects the audio quality)
[RecordSetting setValue: [NSNumber numberWithFloat: 44100] forKey: AVSampleRateKey];
// Number of recording channels: 1 or 2
[RecordSetting setValue: [NSNumber numberWithInt: 1] forKey: AVNumberOfChannelsKey];
// Linear sampling digits: 8, 16, 24, and 32
[RecordSetting setValue: [NSNumber numberWithInt: 16] forKey: AVLinearPCMBitDepthKey];
// Recording quality
[RecordSetting setValue: [NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];

NSString * strUrl = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL * url = [NSURL fileURLWithPath: [NSString stringWithFormat: @ "% @/lll. aac", strUrl];
UrlPlay = url;

NSError * error;
// Initialization
Recorder = [[AVAudioRecorder alloc] initWithURL: url settings: recordSetting error: & error];
// Enable Volume Detection
Recorder. meteringEnabled = YES;
Recorder. delegate = self;

// Recording settings
NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init] autorelease];
// Set the recording format AVFormatIDKey = kAudioFormatLinearPCM
[RecordSetting setValue: [NSNumber numberWithInt: kAudioFormatMPEG4AAC] forKey: AVFormatIDKey];
// Set the recording sampling rate (Hz), for example, AVSampleRateKey = 8000/44100/96000 (which affects the audio quality)
[RecordSetting setValue: [NSNumber numberWithFloat: 44100] forKey: AVSampleRateKey];
// Number of recording channels: 1 or 2
[RecordSetting setValue: [NSNumber numberWithInt: 1] forKey: AVNumberOfChannelsKey];
// Linear sampling digits: 8, 16, 24, and 32
[RecordSetting setValue: [NSNumber numberWithInt: 16] forKey: AVLinearPCMBitDepthKey];
// Recording quality
[RecordSetting setValue: [NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];

NSString * strUrl = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL * url = [NSURL fileURLWithPath: [NSString stringWithFormat: @ "% @/lll. aac", strUrl];
UrlPlay = url;

NSError * error;
// Initialization
Recorder = [[AVAudioRecorder alloc] initWithURL: url settings: recordSetting error: & error];
// Enable Volume Detection
Recorder. meteringEnabled = YES;
Recorder. delegate = self;

 

Identify the volume size to display different images to show fluctuations in the volume chart


[Cpp]
[Recorder updateMeters]; // refresh the volume data
// Obtain the average volume [recorder averagePowerForChannel: 0];
// Maximum volume [recorder peakPowerForChannel: 0];

Double lowPassResults = pow (10, (0.05 * [recorder peakPowerForChannel: 0]);
NSLog (@ "% lf", lowPassResults );
// Up to 50 0
// Small image-large
If (0 <lowPassResults <= 0.06 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_01.png"];
} Else if (0.06 <lowPassResults <= 0.13 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_02.png"];
} Else if (0.13 <lowPassResults <= 0.20 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_03.png"];
} Else if (0.20 <lowPassResults <= 0.27 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_04.png"];
} Else if (0.27 <lowPassResults <= 0.34 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_05.png"];
} Else if (0.34 <lowPassResults <= 0.41 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_06.png"];
} Else if (0.41 <lowPassResults <= 0.48 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_07.png"];
} Else if (0.48 <lowPassResults <= 0.55 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_08.png"];
} Else if (0.55 <lowPassResults <= 0.62 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_09.png"];
} Else if (0.62 <lowPassResults <= 0.69 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_10.png"];
} Else if (0.69 <lowPassResults <= 0.76 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_11.png"];
} Else if (0.76 <lowPassResults <= 0.83 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_12.png"];
} Else if (0.83 <lowPassResults <= 0.9 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_13.png"];
} Else {
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_14.png"];
}

[Recorder updateMeters]; // refresh the volume data
// Obtain the average volume [recorder averagePowerForChannel: 0];
// Maximum volume [recorder peakPowerForChannel: 0];

Double lowPassResults = pow (10, (0.05 * [recorder peakPowerForChannel: 0]);
NSLog (@ "% lf", lowPassResults );
// Up to 50 0
// Small image-large
If (0 <lowPassResults <= 0.06 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_01.png"];
} Else if (0.06 <lowPassResults <= 0.13 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_02.png"];
} Else if (0.13 <lowPassResults <= 0.20 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_03.png"];
} Else if (0.20 <lowPassResults <= 0.27 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_04.png"];
} Else if (0.27 <lowPassResults <= 0.34 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_05.png"];
} Else if (0.34 <lowPassResults <= 0.41 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_06.png"];
} Else if (0.41 <lowPassResults <= 0.48 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_07.png"];
} Else if (0.48 <lowPassResults <= 0.55 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_08.png"];
} Else if (0.55 <lowPassResults <= 0.62 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_09.png"];
} Else if (0.62 <lowPassResults <= 0.69 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_10.png"];
} Else if (0.69 <lowPassResults <= 0.76 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_11.png"];
} Else if (0.76 <lowPassResults <= 0.83 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_12.png"];
} Else if (0.83 <lowPassResults <= 0.9 ){
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_13.png"];
} Else {
[Self. imageView setImage: [UIImage imageNamed: @ "record_animate_14.png"];
}

 

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.