iOS - 简单录音:

1.导入

AVFoundation框架


//

//  AVDIOViewController.m

//  AVDIO

//

//  Created by ifziv on 13-10-16.

//  Copyright (c) 2013 iChano. All rights reserved.

//


#import "AVDIOViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface AVDIOViewController ()

{

    UILabel         *label;

    AVAudioRecorder *recorder;    //专门进行录制的类

    AVAudioPlayer   *player;      //播放

}


@property(nonatomic, retain)AVAudioRecorder *recorder;

@property(nonatomic, retain)AVAudioPlayer *player;


@end


@implementation AVDIOViewController


@synthesize recorder,player;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.view.backgroundColor = [UIColor grayColor];

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)loadView

{

    [super loadView];

    

    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    startButton.frame = CGRectMake(100, 100, 100, 40);

    [startButton setTitle:@"开始录制" forState:UIControlStateNormal];

    [startButton addTarget:self action:@selector(startAvdio) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:startButton];

    

    UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    stopButton.frame = CGRectMake(100, 160, 100, 40);

    [stopButton setTitle:@"停止录制" forState:UIControlStateNormal];

    [stopButton addTarget:self action:@selector(stopAvdio) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:stopButton];

    

    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    playButton.frame = CGRectMake(100, 220, 100, 40);

    [playButton setTitle:@"播放录音" forState:UIControlStateNormal];

    [playButton addTarget:self action:@selector(playAvdio) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:playButton];

}


-(void)startAvdio

{

    //录制状态

    if (recorder.isRecording) {

        [recorder stop];

    }

    //播放状态

    if (player.isPlaying) {

        [player stop];

    }

    

    NSError *error = nil;

    //设置录制信息

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];

    [[AVAudioSession sharedInstance] setActive:YES error:&error];

    //设置采样的详细数据

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

//    [dic setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];   //PCM格式

    [dic setValue:[NSNumber numberWithInt:kAudioFormatMPEGLayer3] forKey:AVFormatIDKey];   //PCM格式

    [dic setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];            //采样率

    [dic setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];               //通道数目

    [dic setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];             //采样位数,默认16.

    [dic setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];         //是大端口还是小端口还是内存的组织方式

    [dic setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];             //采样信号是整数还是浮点数

    

    //定义路径,设置保存的位置。

    

//    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *savePath = [NSString stringWithFormat:@"%@/test.aif", path];   // kAudioFormatLinearPCM == .aif .caf .mav .wav   kAudioFormatMPEG4AAC == .m4a .acc  kAudioFormatMPEGLayer3 == .aif

    NSLog(@"savePath!: %@", savePath);

    

    //录制文件路径

    NSURL *fileURL = [NSURL fileURLWithPath:savePath];

    //初始化录制信息信息,进行录制。

    recorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:dic error:&error];

    [recorder record];

    NSLog(@"avdio error!: %@", error);

    NSLog(@"已开始录制..");

}


-(void)stopAvdio

{

    if (recorder.isRecording) {

        [recorder stop];

    }

    if (player.isPlaying) {

        [player stop];

    }

    NSLog(@"已停止录制..");

}


-(void)playAvdio

{

    NSError *error = nil;

    //获取到路径

//    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *savePath = [NSString stringWithFormat:@"%@/test.aif", path];   // kAudioFormatLinearPCM == .aif .caf .mav .wav    kAudioFormatMPEG4AAC == .m4a .acc   kAudioFormatMPEGLayer3 == .aif

    

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:savePath];

    //进行设定后台播放和激活状态

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

    [[AVAudioSession sharedInstance] setActive:YES error:&error];

    //使用播放器进行播放

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];

    [player play];

    NSLog(@"play error!: %@", error);

}



@end


你可能感兴趣的:(ios,play)