简易的OC录音机
简易的OC录音机
CBETA-陈 发表于2年前
简易的OC录音机
  • 发表于 2年前
  • 阅读 6
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

 

//类库

CoreGraphics.framework

Foundation.framework

AVFoundation.framework

UIKit.framework

 

 

 

 

 

//控制器.h

 

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioPlayerDelegate,AVAudioRecorderDelegate>//协议

@property (strong, nonatomic) AVAudioRecorder *audioRecorder;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

@property   UIButton *recordButton;//录音按钮
@property   UIButton *playButton;//播放按钮

-(void)recordButtonAction:(id)sender;//方法
-(void)playButtonAction:(id)sender;

@end

 

 

 

 

 

 

 

 

//控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//初始化按钮
    self.recordButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    self.recordButton.frame = CGRectMake(80, 202, 200, 200);
    [self.recordButton setImage:[UIImage imageNamed:@"record"] forState:UIControlStateNormal];
    [self.recordButton  addTarget:self action:@selector(recordButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.recordButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.recordButton];
    
    
    
    
    self.playButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    self.playButton.frame = CGRectMake(100, 102, 200, 200);
    [self.playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    [self.playButton  addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playButton];
    self.playButton.backgroundColor = [UIColor blackColor];
    self.playButton.hidden = YES;
    
    //配置Recorder,
    NSDictionary *recordSetting = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey,
                                   [NSNumber numberWithInt:16],AVEncoderBitRateKey,
                                   [NSNumber numberWithInt:2],AVNumberOfChannelsKey,
                                   [NSNumber numberWithFloat:44100.0],AVSampleRateKey,
                                   nil];
    //录音文件保存地址的URL
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.caf", [[NSBundle mainBundle] resourcePath]]];
    NSError *error = nil;
    self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
    
    if (error != nil) {
        NSLog(@"Init audioRecorder error: %@",error);
    }else{
        //准备就绪,等待录音,注意该方法会返回Boolean,最好做个成功判断,因为其失败的时候无任何错误信息抛出
        if ([self.audioRecorder prepareToRecord]) {
            NSLog(@"Prepare successful");
        }
    }
    

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//方法

-(void)recordButtonAction:(id)sender
{
    if (!self.audioRecorder.recording) {
        self.playButton.hidden = YES;
        [self.audioRecorder record];
        [self.recordButton setImage:[UIImage imageNamed:@"MicButtonPressed.png"] forState:UIControlStateNormal];
    }else {
        self.playButton.hidden = NO;
        [self.audioRecorder stop];
        [self.recordButton setImage:[UIImage imageNamed:@"MicButton.png"] forState:UIControlStateNormal];
    }
}

-(void)playButtonAction:(id)sender
{
    if (!self.audioPlayer.playing) {
        self.recordButton.hidden = YES;
        NSError *error;
        NSLog(@"%@",self.audioRecorder.url);
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioRecorder.url error:&error];
        self.audioPlayer.delegate = self;
        if (error != nil) {
            NSLog(@"Wrong init player:%@", error);
        }else{
            [self.audioPlayer play];
        }
        [self.playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    }else {
        self.recordButton.hidden = NO;
        [self.audioPlayer pause];
        [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    }
}

#pragma mark audio delegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"Finsh playing");
    self.recordButton.hidden = NO;
    [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    NSLog(@"Finish record!");
}

-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}


@end

 

共有 人打赏支持
粉丝 0
博文 5
码字总数 3395
×
CBETA-陈
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: