音频处理(录音一)
来源:互联网 发布:mac os x sierra下载 编辑:程序博客网 时间:2024/06/09 23:34
简单录音功能实现可以录音和播放,根据声音大小显示相应的音量图片。
1.添加框架AVFoundation.framework与CoreAudio.framework
2.
创建两个按钮
startButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
startButton.frame =CGRectMake(50, 300,100, 50);
startButton.backgroundColor = [UIColorblackColor];
[startButtonaddTarget:selfaction:@selector(startStop)forControlEvents:UIControlEventTouchUpInside];
[startButtonsetTitle:@"开始录音" forState:UIControlStateNormal];
[self.viewaddSubview:startButton];
UIButton * playButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
playButton.frame = CGRectMake(200,300, 100, 50);
playButton.backgroundColor = [UIColorblackColor];
[playButton addTarget:selfaction:@selector(play)forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"开始播放" forState:UIControlStateNormal];
[self.view addSubview:playButton];
3. 创建一个url
recordedTempFile = [NSURLfileURLWithPath:[NSTemporaryDirectory()stringByAppendingString:@"RecordedFile"]];
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[session setActive:YESerror:nil];
4.创建一个imageview
self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(50,50, 100,100)];
[self.viewaddSubview:self.imageView];
具体代码如下:- (void)viewDidLoad
{
[superviewDidLoad];
[selfcreatButton];
startOrStopFlag = YES;
recordedTempFile = [NSURLfileURLWithPath:[NSTemporaryDirectory()stringByAppendingString:@"RecordedFile"]];
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[session setActive:YESerror:nil];
}
- (void)creatButton
{
startButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
startButton.frame =CGRectMake(50, 300,100, 50);
startButton.backgroundColor = [UIColorblackColor];
[startButtonaddTarget:selfaction:@selector(startStop)forControlEvents:UIControlEventTouchUpInside];
[startButtonsetTitle:@"开始录音" forState:UIControlStateNormal];
[self.viewaddSubview:startButton];
UIButton * playButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
playButton.frame = CGRectMake(200,300, 100, 50);
playButton.backgroundColor = [UIColorblackColor];
[playButton addTarget:selfaction:@selector(play)forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"开始播放" forState:UIControlStateNormal];
[self.view addSubview:playButton];
self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(50,50, 100,100)];
[self.viewaddSubview:self.imageView];
// UILabel * hzLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
// hzLabel.text = @"采样频率";
// [self.view addSubview:hzLabel];
//
// UILabel * voLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];
// voLabel.text = @"音量";
// [self.view addSubview:voLabel];
//
// hzTextField = [[UITextField alloc] initWithFrame:CGRectMake(200, 50, 100, 50)];
// hzTextField.borderStyle = UITextBorderStyleRoundedRect;
// hzTextField.text = @"44100.0";
// [self.view addSubview:hzTextField];
//
// voTextField = [[UITextField alloc] initWithFrame:CGRectMake(200, 150, 100, 50)];
// voTextField.borderStyle = UITextBorderStyleRoundedRect;
// voTextField.text = @"2";
// [self.view addSubview:voTextField];
activity = [[UIActivityIndicatorViewalloc] initWithFrame:CGRectMake(100,400, 50,50)];
activity.backgroundColor = [UIColorredColor];
[self.viewaddSubview:activity];
}
- (void)dealloc
{
// NSFileManager * fm = [NSFileManager defaultManager];
// [fm removeItemAtPath:[recordedTempFile path] error:nil];
// recorder = nil;
}
- (void)startStop
{
if (startOrStopFlag) {
[startButtonsetTitle:@"暂停录音" forState:UIControlStateNormal];
recorder = [[AVAudioRecorderalloc] initWithURL:recordedTempFilesettings:nilerror:nil];
//开启音量检测
recorder.meteringEnabled =YES;
recorder.delegate =self;
//设置定时检测
timer = [NSTimerscheduledTimerWithTimeInterval:0target:selfselector:@selector(detectionVoice)userInfo:nilrepeats:YES];
startOrStopFlag = NO;
[activitystartAnimating];
[recorderprepareToRecord];
[recorder record];
}
else
{
startOrStopFlag = YES;
[activitystopAnimating];
[startButtonsetTitle:@"开始录音" forState:UIControlStateNormal];
[recorder stop];
player = [[AVAudioPlayeralloc] initWithContentsOfURL:recordedTempFileerror:nil];
}
}
- (void)detectionVoice
{
[recorderupdateMeters];
double lowPassResults = pow(10, (0.05 * [recorderpeakPowerForChannel:0]));
if (0<lowPassResults<=0.06) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_01.png"]];
}else if (0.06<lowPassResults<=0.13) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_02.png"]];
}else if (0.13<lowPassResults<=0.20) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_03.png"]];
}else if (0.20<lowPassResults<=0.27) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_04.png"]];
}else if (0.27<lowPassResults<=0.34) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_05.png"]];
}else if (0.34<lowPassResults<=0.41) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_06.png"]];
}else if (0.41<lowPassResults<=0.48) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_07.png"]];
}else if (0.48<lowPassResults<=0.55) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_08.png"]];
}else if (0.55<lowPassResults<=0.62) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_09.png"]];
}else if (0.62<lowPassResults<=0.69) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_10.png"]];
}else if (0.69<lowPassResults<=0.76) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_11.png"]];
}else if (0.76<lowPassResults<=0.83) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_12.png"]];
}else if (0.83<lowPassResults<=0.9) {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_13.png"]];
}else {
[self.imageViewsetImage:[UIImage imageNamed:@"record_animate_14.png"]];
}
}
- (void)play
{
[playerprepareToPlay];
[player play];
}
- 音频处理(录音一)
- 音频处理(录音二)
- ios 录音音频后台问题处理
- Android音频捕获(录音)
- Android音频捕获(录音)
- Media播放(播放音频,录音等)
- 音频 音效 音频会话 录音
- WebRTC 音频处理流程(一)
- Android之音频录音
- 录音,播放音频
- 录音/播放音频
- 音频播放\录音
- 8926音频录音流程
- 录音项目随笔(一)
- Android之录音(一)
- ios音频队列实现录音和播音(转)
- ios 音频队列实现录音和播音(转)
- Android开发之录音MediaRecorder、播放音频(MediaPlayer、SoundPool)
- 如何平衡的紧张关系在一台缝纫机
- iOS实现图片的缩放和居中显示
- OJC++实现最大数的输出
- 如何将子弹分在你的网上文章投稿
- 如何完成你的海盗服装的奇装异服方
- 音频处理(录音一)
- HTML标签总结
- 如何在印度选择最好的移动游戏开发公司
- 复数类--重载运算符
- 基于Tomcat7、Java、WebSocket的服务器推送聊天室
- 商品销售
- 在resin配置参数实现JConsole远程监控JVM
- 如何创建一个iPhone或iPad的应用程序,并在App Store成功游戏
- 顺时针打印矩阵