音频的录制与播放

本文只为记录,若有侵犯,实属无意之举!
    

#import "ViewController.h"


#import <AVFoundation/AVFoundation.h>//注意导入引用库


@interface ViewController ()<AVAudioRecorderDelegate>

{

    AVAudioRecorder *recorder;

    AVAudioPlayer *audioPlayer;


    BOOL recording;

    NSURL *tmpFile;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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


    //录制

    UIButton *recording1 = [[UIButton alloc]init];

    recording1.backgroundColor = [UIColor lightGrayColor];

    recording1.frame = CGRectMake(50, 100, 70, 70);

    [recording1 setTitle:@"录制" forState:UIControlStateNormal];

    [recording1 addTarget:self action:@selector(recordingbtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:recording1];

    

    //播放

    UIButton *broadcast = [[UIButton alloc]init];

    broadcast.frame = CGRectMake(150, 100, 70, 70);

    broadcast.backgroundColor = [UIColor lightGrayColor];

    [broadcast setTitle:@"播放" forState:UIControlStateNormal];

    [broadcast addTarget:self action:@selector(broadcastBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:broadcast];

}


//录制

- (void)recordingbtnClick:(UIButton *)sender{

    //单列创建 音频会议

    AVAudioSession * audioSession = [AVAudioSession sharedInstance];

    if (!recording) {

        recording = YES;

        [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

        [audioSession setActive:YES error:nil];

        [sender setTitle:@"停止" forState:UIControlStateNormal];

        

        NSDictionary *setting = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithFloat: 44100.0],AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey, [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,nil];

        

        //URL 沙盒路径+文件名   这里文件名最好用时间  因为时间不会重复  避免覆盖

        tmpFile = [NSURL fileURLWithPath:

                   [NSTemporaryDirectory() stringByAppendingPathComponent:

                    [NSString stringWithFormat: @"%@.%@",

                     @"shishi",

                     @"wav"]]];

        recorder = [[AVAudioRecorder alloc] initWithURL:tmpFile settings:setting error:nil];

        [recorder setDelegate:self];

        [recorder prepareToRecord];

        [recorder record];

    } else {

        recording = NO;

        [audioSession setActive:NO error:nil];

        [recorder stop];

        [sender setTitle:@"录音" forState:UIControlStateNormal];

    }

}


//播放

- (void)broadcastBtnClick{

    NSError *error;

    

    audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:tmpFile

                                                      error:&error];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    

    audioPlayer.volume=1;

    if (error) {

        NSLog(@"error:%@",[error description]);

        return;

    }

    //准备播放

    [audioPlayer prepareToPlay];

    //播放

    [audioPlayer play];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值