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

//

//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值