IOS-錄音功能

來源:互聯網
上載者:User

初始化AVAudioRecorder並設定錄音的參數和儲存地址:

//錄音設定    NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease];    //設定錄音格式  AVFormatIDKey==kAudioFormatLinearPCM    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];    //設定錄音採樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音訊品質)    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];    //錄音通道數  1 或 2    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];    //線性採樣位元  8、16、24、32    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];    //錄音的品質    [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;    //初始化    recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];    //開啟音量檢測    recorder.meteringEnabled = YES;    recorder.delegate = self;

識別音量的大小,從而顯示不同的圖片,來呈現出音量圖波動

[recorder updateMeters];//重新整理音量資料    //擷取音量的平均值  [recorder averagePowerForChannel:0];    //音量的最大值  [recorder peakPowerForChannel:0];        double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0]));    NSLog(@"%lf",lowPassResults);    //最大50  0    //圖片 小-》大    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"]];    }

demo 的: 點擊去下載。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.