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

腾讯云 技术升级10大核心产品年终让利>>>   

  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(@"录制完成!!");
    }
}

 

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