oc开发笔记1 录音和播放
来源:互联网 发布:mysql事务处理例子 编辑:程序博客网 时间:2024/06/03 17:25
简单尝试了下用objective-c 实现录音和播放功能:
界面很简单,点击录制按钮就开始录制,再点击就停止 并播放刚才录制的声音。
参考:http://blog.csdn.net/rhljiayou/article/details/15339335
//// ViewController.m// SnoringTest//// Created by bai on 16/4/5.// Copyright © 2016年 bai. All rights reserved.//#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController ()@end@implementation ViewControllerNSDictionary *recorderSettingsDict;//录音名字NSString *playName;//定时器NSTimer *timer;AVAudioRecorder *recorder;double lowPassResults;AVAudioPlayer *player;- (void)viewDidLoad { [super viewDidLoad]; // if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending) // { AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *sessionError; //AVAudioSessionCategoryPlayAndRecord用于录音和播放 [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError]; if(session == nil) NSLog(@"Error creating session: %@", [sessionError description]); else [session setActive:YES error:nil]; // } // Do any additional setup after loading the view, typically from a nib. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; playName = [NSString stringWithFormat:@"%@/play.aac",docDir]; //录音设置 格式AVFormatIDKey 通道数AVNumberOfChannelsKey 采样率AVSampleRateKey 线性采样位数AVLinearPCMBitDepthKey recorderSettingsDict =[[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, [NSNumber numberWithInt:1000.0],AVSampleRateKey, [NSNumber numberWithInt:2],AVNumberOfChannelsKey, [NSNumber numberWithInt:8],AVLinearPCMBitDepthKey, [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, nil];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. recorder= nil; player= nil;}- (IBAction)touchUp:(UIButton *)sender { if ([self.recordbut.titleLabel.text isEqual: @"录制中"]){ [self.recordbut setTitle:@"录制" forState:UIControlStateNormal]; //录音停止 [recorder stop]; recorder = nil; //结束定时器 [timer invalidate]; timer = nil; NSError *playerError; //从budle路径下读取音频测试文件 这个文件名是你的歌曲名字,mp3是你的音频格式 // NSString *playName = [[NSBundle mainBundle] pathForResource:@"q" ofType:@"mp3"]; //播放 // player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName ] error:&playerError]; //音量 player.volume = 5.0; //切换至扬声器 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; if (player == nil) { NSLog(@"ERror creating player: %@", [playerError description]); }else{ [player play]; NSLog(@" 正在播放 音量:%f",player.volume ); } }else{ //切换至录音 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; //按下录音 NSError *error = nil; //暂时没发现模拟器上会崩溃 recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error]; if (recorder) { //是否允许刷新电平表,默认是off recorder.meteringEnabled = YES; //创建文件,并准备录音 [recorder prepareToRecord]; //开始录音 [recorder record]; //启动定时器,为了更新电平 timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES]; } else { NSLog(@"Error: %@ )" , [error localizedDescription]); } [self.recordbut setTitle:@"录制中" forState:UIControlStateNormal]; }}-(void)levelTimer:(NSTimer*)timer_{ //call to refresh meter values刷新平均和峰值功率,此计数是以对数刻度计量的,-160表示完全安静,0表示最大输入值 [recorder updateMeters]; const double ALPHA = 0.05; double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0])); lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults; NSLog(@"音量均值: %f 峰值: %f 电平: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0], lowPassResults);}- (IBAction)exitAction:(id)sender { exit(0);}@end
遇到问题一:播放不出声音
一开始播放时没有声音,后来百度后知道播放器*player不能设置为局部变量,得设置成全局变量才能正常播放。
问题二:Home键返回或锁屏后不能持续录音
如果不做任何设置,homo键后或锁屏会暂停录音,解决办法是修改info.plist,增加Application does not run in background和Required background modes:
问题三:使用扬声器
//切换至扬声器
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:nil];
//切换至录音
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryRecorderror:nil];
0 0
- oc开发笔记1 录音和播放
- oc开发笔记2 AUGraph 完成同时录音与播放
- android开发录音和播放录音
- android开发录音和播放录音的例子
- android开发录音和播放录音的例子
- android 录音和播放录音
- android录音和播放录音
- Android录音和播放录音
- Android 录音和播放录音
- AudioQueue 播放和录音
- AudioQueue 播放和录音
- Android 录音和播放
- iOS录音和播放
- iOS录音和播放
- Android开发笔记(五十七)录像录音与播放
- 使用Android开发录音和播放功能小程序示例
- 微信小程序开发例子-录音和播放声音
- iOS开发之录音和播放问题AVAudioPlayer
- shell脚本之echo命令
- tableView怎么刷新置顶
- Windows消息机制概述
- 使用iptables实现外网转发内网
- [leetcode] 89. Gray Code
- oc开发笔记1 录音和播放
- Bison眼中的iOS开发多线程是这样的(三)
- 实现图像剪裁 jquery.Jcrop
- Dataguard DBLOG FILE NAME CONVERT has been set but files are created in a different directory (文档 ID
- 大型网站架构知识框架
- 冒泡排序
- javascript 升序和降序
- C#格式转换注意事项
- Qt 给QWidget添加工具栏