iOS中录音和播放实现

//首先导入框架后,导入头文件.以下内容为托控件,在storyboard中拖出两个按钮为录音和播放按钮


//创建一个UIViewController在.h文件中写

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController


//录音存储路径

@property (nonatomic, strong)NSURL *tmpFile;

//录音

@property (nonatomic, strong)AVAudioRecorder *recorder;

//播放

@property (nonatomic, strong)AVAudioPlayer *player;

//录音状态(是否录音)

@property (nonatomic, assign)BOOL isRecoding;


@end


//在.m文件中写

#import "ViewController.h"

//添加代理

@interface ViewController ()<AVAudioPlayerDelegate>


//录音按钮

@property (weak, nonatomic) IBOutlet UIButton *recordButton;


//播放按钮

@property (weak, nonatomic) IBOutlet UIButton *playButton;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    

    //刚打开的时候录音状态为不录音

    self.isRecoding = NO;

    

    //播放按钮不能被点击

    [self.playButton setEnabled:NO];

    //播放按钮设置成半透明

    self.playButton.titleLabel.alpha = 0.5;

    

    //创建临时文件来存放录音文件

    self.tmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"TmpFile"]];

    

    //设置后台播放

    AVAudioSession *session = [AVAudioSession sharedInstance];

    

    NSError *sessionError;

    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    

    //判断后台有没有播放

    if (session == nil) {

        

        NSLog(@"Error creating sessing:%@", [sessionError description]);

        

    } else {

    

        [session setActive:YES error:nil];

    

    }

    

}


//录音按钮方法的实现

- (IBAction)startStopRecord:(id)sender {

    

    //判断当录音状态为不录音的时候

    if (!self.isRecoding) {

        //将录音状态变为录音

        self.isRecoding = YES;

        

        //将录音按钮变为停止

        [self.recordButton setTitle:@"停止" forState:UIControlStateNormal];

        

        //播放按钮不能被点击

        [self.playButton setEnabled:NO];

        self.playButton.titleLabel.alpha = 0.5;

        

        //开始录音,将所获取到得录音存到文件里

        self.recorder = [[AVAudioRecorder alloc] initWithURL:_tmpFile settings:nil error:nil];

        

        //准备记录录音

        [_recorder prepareToRecord];

        

        //启动或者恢复记录的录音文件

        [_recorder record];

        

        _player = nil;

        

    } else {

    

        //录音状态 点击录音按钮 停止录音

        self.isRecoding = NO;

        [self.recordButton setTitle:@"录音" forState:UIControlStateNormal];

        

        //录音停止的时候,播放按钮可以点击

        [self.playButton setEnabled:YES];

        [self.playButton.titleLabel setAlpha:1];

        

        //停止录音

        [_recorder stop];

        

        _recorder = nil;

        

        

        NSError *playError;

        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:_tmpFile error:&playError];

        //当播放录音为空, 打印错误信息

        if (self.player == nil) {

            NSLog(@"Error crenting player: %@", [playError description]);

        }

        self.player.delegate = self;

    

    }

    

}


//播放按钮方法的实现

- (IBAction)playPause:(id)sender {

    

    //判断是否正在播放,如果正在播放

    if ([self.player isPlaying]) {

        //暂停播放

        [_player pause];

        

        //按钮显示为播放

        [self.playButton setTitle:@"播放" forState:UIControlStateNormal];

        

    } else {

    

        //开始播放

        [_player play];

        

        //

        [self.playButton setTitle:@"暂停" forState:UIControlStateNormal];

    

    }

    

}


//当播放结束后调用这个方法

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    //按钮标题变为播放

    [self.playButton setTitle:@"播放" forState:UIControlStateNormal];

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值