您的位置:首页 > 移动开发 > IOS开发

iOS 录音并播放,使用AFN发送文件

2015-07-16 19:03 549 查看
//

// ViewController.m

// recordAudio0716

//

// Created by tianshangrenjian on 15/7/16.

// Copyright (c) 2015年 tianshangrenjian. All rights reserved.

//

#import "ViewController.h"

//#import "UIKit+AFNetworking.h"

#import "AFNetworking.h"

#import <AVFoundation/AVFoundation.h>

#import "SVProgressHUD.h"

@interface
ViewController ()

@property (strong,
nonatomic) AVAudioRecorder *audioReorder;

@property (strong,
nonatomic) AVAudioPlayer *audioPlayer;

@property (strong,
nonatomic)
AFHTTPRequestOperationManager *httpMgr;

@end

@implementation ViewController
- (IBAction)btrecord:(id)sender {

SVProgressHUD *hud=[[SVProgressHUD
alloc]
init];

if (self.audioReorder.recording)
{
[self.audioReorder
stop];

NSLog(@"recording is stop");
}

else
{
[self.audioReorder
record];

NSLog(@"begin record!");
}
}

- (IBAction)btPlay:(id)sender {

if (self.audioPlayer.playing) {
[self.audioPlayer
stop];
}

else
{

NSError *error=nil;

self.audioPlayer=[[AVAudioPlayer
alloc] initWithContentsOfURL:self.audioReorder.url
error:&error];
[self.audioPlayer
play];

NSLog(@"begin play!");

NSLog(@"%@",self.audioReorder.url);
}

}
- (IBAction)btUpload:(id)sender {

[SVProgressHUD
showWithStatus:@"加载中..."
maskType:SVProgressHUDMaskTypeBlack];

[self.httpMgr
POST:@"http://xuyingtest.sinaapp.com/uploadfile.php"
parameters:nil
constructingBodyWithBlock:^
void(id<AFMultipartFormData> formData) {

[formData appendPartWithFileURL:self.audioReorder.url
name:@"file"
error:nil];

}
success:^ void(AFHTTPRequestOperation * operation,
id responseObject) {

NSDictionary *dict=[NSJSONSerialization
JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments
error:nil];

if (dict[@"error"]) {

NSLog(@" up success error :%@",dict[@"error"]);
}

else
{

NSLog(@"upload file ok!:%@",dict[@"success"]);
}

}
failure:^void(AFHTTPRequestOperation * operation,
NSError * error) {

NSLog(@"%@",error.localizedDescription);

}];

[NSThread
sleepForTimeInterval:6];

[SVProgressHUD
dismiss];

}

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_httpMgr =[[
AFHTTPRequestOperationManager alloc]
init];

//设置返回数据的类型

_httpMgr.responseSerializer=[AFHTTPResponseSerializer
serializer];

//音频设备

AVAudioSession *session=[AVAudioSession
sharedInstance];

NSError *sessionError;

//控制当前录音播放的时候其他的音频是关闭状态。否则无法播放

[session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&sessionError];

if (sessionError) {

NSLog(@"error:%@",sessionError.localizedDescription);
}

else
{
[session
setActive:YES
error:nil];
}

//录音文件保存路径

//获取一个沙盒的Document的路径。。lastObject指是进去我们的Document文件夹。。NSUserDomainMask指当前的APP的沙盒中去查找。

NSURL *url=[[[NSFileManager
defaultManager]URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask]
lastObject];

//给我们录音文件起个名字

NSURL *voiceUrl=[url
URLByAppendingPathComponent:@"voice_corder.caf"];

//配置audioRecorder

NSDictionary *recordSet=[NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber
numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey,//录音质量
[NSNumber
numberWithInt:16],AVEncoderBitRateKey,//编码率
[NSNumber
numberWithInt:2],AVNumberOfChannelsKey,//
[NSNumber
numberWithFloat:44100.0],AVSampleRateKey,

nil];

NSError *error;

self.audioReorder=[[AVAudioRecorder
alloc] initWithURL:voiceUrl
settings:recordSet
error:&error];

if (error) {

NSLog(@" audiorecorder error:%@",error.localizedDescription);
}

else
{

if ([self.audioReorder
prepareToRecord]) {

NSLog(@"recorder is ready!");
}
}

}

- (void)didReceiveMemoryWarning {

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: