简易录音机

  1.  

@interface ViewController : UIViewController<AVAudioRecorderDelegate>

@property (strong, nonatomic) IBOutlet UISegmentedControl *sampleRateSeg;
@property (strong, nonatomic) IBOutlet UISegmentedControl *bitDeptSeg;
@property (strong, nonatomic) IBOutlet UISwitch *stereoSwitch;
@property (strong, nonatomic) IBOutlet UIButton *recordBn;
// 该属性代表了该应用的Documnts目录
@property (nonatomic, copy) NSString* documentsPath;
- (IBAction)clicked:(id)sender;
- (IBAction)play:(id)sender;
@end

2.

#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
AVAudioRecorder* audioRecorder;
UIImage* recordImage;
UIImage* stopImage;
AVAudioPlayer* audioPlayer;
- (void)viewDidLoad
{
    [super viewDidLoad];
    recordImage = [UIImage imageNamed:@"record.png"];
    stopImage = [UIImage imageNamed:@"stop.png"];
                                  
    [self.recordBn setImage:recordImage forState:UIControlStateNormal];
    // 获取当前应用的音频会话
    AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    // 设置音频类别,PlayAndRecord——这说明当前音频会话即可播放、也可录制
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
    // 激活当前应用的音频会话
    [audioSession setActive:YES error: nil];
}
//获取document目录的路径
- (NSString*) documentsPath {
    if (! _documentsPath) {
        NSArray *searchPaths =
        NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
        _documentsPath = [searchPaths objectAtIndex: 0];
    }
    return _documentsPath;
}
- (IBAction)clicked:(id)sender
{
    if(audioRecorder != nil && audioRecorder.isRecording)
    {
        [audioRecorder stop];
        [self.recordBn setImage:recordImage forState:UIControlStateNormal];
    }
    else
    {
        // 获取音频文件的保存路径
        NSString *destinationString = [[self documentsPath]
            stringByAppendingPathComponent:@"sound.wav"];
        NSURL *destinationURL = [NSURL fileURLWithPath:destinationString];
        // 创建一个NSDictionary,用于保存录制属性
        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
        // 设置录制音频的格式
        [recordSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM]
            forKey: AVFormatIDKey];
        NSString* sampleRate = [self.sampleRateSeg titleForSegmentAtIndex:
            self.sampleRateSeg.selectedSegmentIndex];
        // 设置录制音频的采样率
        [recordSettings setObject:[NSNumber numberWithFloat:
            sampleRate.floatValue] forKey: AVSampleRateKey];
        // 设置录制音频的通道数
        [recordSettings setObject:
            [NSNumber numberWithInt:(self.stereoSwitch.on ? 2 : 1)]
            forKey:AVNumberOfChannelsKey];
        NSString* bitDepth = [self.bitDeptSeg titleForSegmentAtIndex:
            self.bitDeptSeg.selectedSegmentIndex];
        // 设置录制音频的每个样点的位数
        [recordSettings setObject: [NSNumber numberWithInt:bitDepth.integerValue]
            forKey:AVLinearPCMBitDepthKey];
        // 设置录制音频采用高位优先的记录格式
        [recordSettings setObject:[NSNumber numberWithBool:YES]
            forKey:AVLinearPCMIsBigEndianKey];
        // 设置采样信号采用浮点数
        [recordSettings setObject:[NSNumber numberWithBool:YES]
            forKey:AVLinearPCMIsFloatKey];
        NSError *recorderSetupError = nil;
        // 初始化AVAudioRecorder
        audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL
            settings:recordSettings  error:&recorderSetupError];
        audioRecorder.delegate = self;
        [audioRecorder record];
        [self.recordBn setImage:stopImage forState:UIControlStateNormal];
    }
}
- (IBAction)play:(id)sender
{
    // 获取音频文件的保存路径
    NSString *destinationString = [[self documentsPath]
        stringByAppendingPathComponent:@"sound.wav"];
    NSURL *url = [NSURL fileURLWithPath:destinationString];
    // 创建AVAudioPlayer对象
    audioPlayer = [[AVAudioPlayer alloc]
        initWithContentsOfURL:url error: nil];
    // 开始播放
    [audioPlayer play];
}
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
{
    NSLog(@"被中断!");
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder
    successfully:(BOOL)flag
{
    if(flag)
    {
        NSLog(@"录制完成!!");
    }
}

 

转载于:https://my.oschina.net/chenchen9654321/blog/679477

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值