在线直播系统源码中IOS开发关于声音的录制及播放声音

之前IOS在线直播系统源码当中,播放声音文件,只是实现了声音的播放,下面主要实现声音的录制,同时播放声音。采用AVAudioRecorder进行声音的录制。下面是实现的代码:
1、IOS在线直播系统源码中导入AVFoundation框架

	#import <AVFoundation/AVFoundation.h>

2、IOS在线直播系统源码中声明全局的录音控制器

	//录音路径
@property (nonatomic, copy, readwrite) NSString *recordPath;
//录音控制器
@property (nonatomic, strong) AVAudioRecorder *recorder;

3、IOS在线直播系统源码中创建开始录音的按钮

	   recordBtn = [UIButton buttonWithType:0];
    [recordBtn setImage:[UIImage imageNamed:@"edit_voice_nor"] forState:0];
    [recordBtn setImage:[UIImage imageNamed:@"edit_voice_encoding"] forState:UIControlStateHighlighted];
    [recordBtn addTarget:self action:@selector(holdDownButtonTouchDown) forControlEvents:UIControlEventTouchDown];
    [recordBtn addTarget:self action:@selector(holdDownButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
    [recordBtn addTarget:self action:@selector(holdDownButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [recordBtn addTarget:self action:@selector(holdDownDragOutside) forControlEvents:UIControlEventTouchDragExit];
    [recordBtn addTarget:self action:@selector(holdDownDragInside) forControlEvents:UIControlEventTouchDragEnter];
    [recordView addSubview:recordBtn];

4、IOS在线直播系统源码中实现按钮各个状态的方法

- (void)holdDownButtonTouchDown {
    [self startRecord];
- (void)holdDownButtonTouchUpOutside {
    [self finishRecorded];   
}
- (void)holdDownButtonTouchUpInside {
    [self finishRecorded];
}
- (void)holdDownDragOutside {
    [self finishRecorded]; 
}
- (void)holdDownDragInside {
    [self finishRecorded];
}
//开始录音
- (void)startRecord{
    self.recordPath = [self getRecorderPath];
    NSError *error = nil;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&error];
    NSMutableDictionary * recordSetting = [NSMutableDictionary dictionary];
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
        [recordSetting setObject:@16 forKey:AVLinearPCMBitDepthKey];//采样的位数
        [recordSetting setObject:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
        [recordSetting setObject:[NSNumber numberWithFloat:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    error = nil;
    if (self.recorder) {
        _recorder = nil;
    }
    _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:self.recordPath] settings:recordSetting error:&error];
    _recorder.delegate = self;
    _recorder.meteringEnabled = YES;
    [_recorder recordForDuration:(NSTimeInterval) MAX_RECORD_TIME];
    BOOL isRecorder = [_recorder prepareToRecord];
    NSLog(@"isRecorder = %d",isRecorder);
    [_recorder record];
    if (isRecorder) {
        [self resetTimer];
        count = 0;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateMeters) userInfo:nil repeats:YES];
    }
}
- (void)finishRecorded{
    [self cancelRecording];
}
- (void)updateMeters{
    count ++;
    tipsLabel.text = [NSString stringWithFormat:@"%d\"",count];
    tipsLabel.textColor = normalColors;
    if (count == MAX_RECORD_TIME) {
        [self resetTimer];
        [self cancelRecording]; }
//获取录音存储路径
- (NSString *)getRecorderPath {
    NSString *recorderPath = nil;
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yy-MMMM-dd";
    recorderPath = [[NSString alloc] initWithFormat:@"%@/Documents/", NSHomeDirectory()];
    dateFormatter.dateFormat = @"yyyy-MM-dd-hh-mm-ss";
    recorderPath = [recorderPath stringByAppendingFormat:@"%@-voiceComment.wav", [dateFormatter stringFromDate:now]];
    return recorderPath;
//录音结束
- (void)cancelRecording {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];  //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
    if (self.recorder.isRecording) {
        [self.recorder stop];
        if ([self getVoiceDuration:_recordPath] < 3) {
            [self resetTimer];
            [self delegateBtnClick];
            [MBProgressHUD showError:YZMsg(@"录音时间太短")];
            return;
        }
        if ([self getVoiceDuration:_recordPath] >= 3 && [self getVoiceDuration:_recordPath] <= 15)  {
            playView.hidden = NO;
            playTimeL.text = [NSString stringWithFormat:@"%.0f\"",[self getVoiceDuration:_recordPath]];
            recordView.hidden = YES;
        [self resetTimer];
    self.recorder = nil;
- (void)resetTimer { 
    if (_timer) {
        count = 0;
        [_timer invalidate];
        _timer = nil;
    }
}
//获取音频时长
- (CGFloat)getVoiceDuration:(NSString*)recordPath {
    AVAudioPlayer *play = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recordPath] error:nil];
    NSLog(@"时长:%f", play.duration);
    return play.duration;
}

以上就是IOS在线直播系统源码当中,播放声音文件,实现了声音的播放,实现声音的录制,同时播放声音的解决方案,采用AVAudioRecorder进行声音的录制。参看此文献资料会帮助开发者大大节约时间成本。关注本博主,后期会定期更新更多干活。
声明:本文文章为小编原创文章,转载请注明出处及作者。

发布了119 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/yun_bao_2144899870/article/details/99682137