//开始录制的方法
-(void)startRecoder{
//设置label状态显示 显示为正在录制
label.textColor = [UIColor redColor];
label.text = @"录制中...";
label.textAlignment = NSTextAlignmentCenter;
//判断当前的录制状态和播放状态
if (recoder.isRecording) {
[recoder stop];
}
if (player.isPlaying) {
[player stop];
}
NSError *err = nil;
//设定录制信息
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
//设置采样的详细数据
NSMutableDictionary *settings = [NSMutableDictionary dictionary];
[settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; //采样率
[settings setValue:[NSNumber numberWithInt:1]
forKey:AVNumberOfChannelsKey];//通道的数目
[settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];//采样位数 默认 16
[settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式
[settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数
//定义路径,设定要保存的位置 /BDEIDJDFDSF-SDfDS4232/document
NSString *dir = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];
//设定路径
savePath = [NSString stringWithFormat:@"%@/testAudio.aif",dir];
NSLog(@"savaPath:%@",savePath);
//定义URL
NSURL *fileUrl = [NSURL fileURLWithPath:savePath];
if (err) {
NSLog(@"录制之前配置出错了!");
return;
}
//初始化了录制的类
recoder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:settings error:&err];
//开始录制
[recoder record];
}
//停止录制的方法
-(void)stopRecoder{
//设置label状态显示 显示为正在录制
label.textColor = [UIColor greenColor];
label.text = @"已停止...";
label.textAlignment = NSTextAlignmentCenter;
//正在录制的时候,要停止录制,正在播放的时候,要停止播放
if (recoder.isRecording) {
[recoder stop];
}
if(player.isPlaying){
[player stop];
}
}
//开始播放
-(void)startRecoderPlay{
//设置label状态显示 显示为正在录制
label.textColor = [UIColor purpleColor];
label.text = @"正在播放...";
label.textAlignment = NSTextAlignmentCenter;
NSError *err = nil;
//获得录制的文件的路径
//定义路径,设定要保存的位置 /BDEIDJDFDSF-SDfDS4232/document
NSString *dir = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];
//设定路径
savePath = [NSString stringWithFormat:@"%@/testAudio.aif",dir];
//定义URL
NSURL *fileUrl = [NSURL fileURLWithPath:savePath];
//设定后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err];
//设定为激活状态
[[AVAudioSession sharedInstance] setActive:YES error:&err];
//使用播放器进行播放
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&err];
//-------------------------------------------
//http://10.2.145.29/2012/audio/upload.php
// [self network];
[player play];
}
-(void)network
{
NSLog(@"fuck");
NSURL *url=[NSURL URLWithString:@"http://10.2.145.29/2012/audio/upload.php"];
// 导入的包中的类 用于上传
ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];
// 设置代理
[request setDelegate:self];
//上传类型 只有这一中方式
[request setRequestMethod:@"POST"];
// 为上传对象添加数据 数据
//定义路径,设定要保存的位置 /BDEIDJDFDSF-SDfDS4232/document
NSString *dir = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];
//设定路径
savePath = [NSString stringWithFormat:@"%@/testAudio.aif",dir];
NSData *data=[NSData dataWithContentsOfFile:savePath];
// 上传后保存的名字 // 保存类型 // 表单名 和 相应的php 文件相对
I=arc4random()%1000;
[request addData:data withFileName:[NSString stringWithFormat:@"%d.aif",I] andContentType:@"audio/aif" forKey:@"file"];
//
[request startAsynchronous];//开始。异步
[request setDidFinishSelector:@selector(headPortraitSuccess)];//当成功后会自动触发 headPortraitSuccess 方法
[request setDidFailSelector:@selector(headPortraitFail)];//如果失败会 自动触发 headPortraitFail 方法
[request release];
}