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
// 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
相关文章推荐
- VT-x is disabled in the BIOS. (VERR_VMX_MSR_VMXON_DISABLED).
- iPhone 6/plus iOS Safari fieldset border 边框消失
- 【iOS学习】二、实现直播技术学习
- iOS_关联详解
- iOS 设置程序的图片 以及启动画面
- iOS开发系列--让你的应用“动”起来--动画
- iOS开发系列--并行开发其实很容易--多线程
- iOS开源项目 页面切换效果TransitionAnimation
- IOS 异或加密
- IOS代理
- 支持TableView上下拉刷新的库文件Demo
- iOS block的用法
- ios 中frame and bound
- iOS官方demo
- 仿照余额宝余额动态变动
- ios中图片选择及上传
- 小白学开发(iOS)基础C_字符串 (2015-07-16)
- 小白学开发(iOS)基础C_数组(2015-07-14)
- IOS数组NSArray与NSMutableArray知识点
- iOS如何实现国际化(本地化)