直播系统源代码实现之iOS端语音录制、合成与播放

发布来源:云豹科技
发布人:云豹科技
2022-08-25 10:00:13

语音录制功能对直播系统源代码非常重要,实现语音录制以后,方便用户发布语音动态和私聊发送语音消息。直播系统源代码进行录制音频使用的是XHSoundRecorder,此三方封装了录制、合并以及播放等功能,可以在开发中直接调使用。

1.语音录制

点击录音开始,判断是不是正在录音,如果正在录音就暂停录制,否则开启录音并开始计时,录制一段暂停之后,合并录音文件,并可以回放。


#pragma mark  开始录音
-(void)recordClick{
    isRecording = !isRecording;
    if (isRecording) {
        sureBtn.hidden = YES;
        [self hideLisenAddDelete:YES];
        //开始录音
//        _oldlistenetime = 0;//开始录音试听归零
        _animationView.hidden = NO;
        _recordNormalImg.hidden = YES;
        tipsLb.text = @"点击可暂停";
        YBWeakSelf;
        if ((int)_oldtime != 0) {
            minValue = _oldtime;
        }
        if (!audioTimer) {
            audioTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(audiodaojishi) userInfo:nil repeats:YES];
        }else{
            [self timerBegin];
        }
        //  准备录音
        [self prepareToRecord];
        //  录音记录
        BOOL isSuccess = [self.recoder record];
        if (isSuccess) {
            NSLog(@"开始录音成功");
        }else{
            NSLog(@"开始录音失败");
        }
 
        [[XHSoundRecorder sharedSoundRecorder] startRecorder:^(NSString *filePath) {
 
            NSLog(@"录音文件路径---:%@",filePath);
            NSLog(@"录音结束");
            if (weakSelf.filePath && weakSelf.filePath.length > 0) {
                recordCount++;
                NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
                NSString *destPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ddest.m4a",recordCount]];
                
 
                NSError *error = nil;
                //  如果目标文件已经存在删除目标文件
                if ([[NSFileManager defaultManager] fileExistsAtPath:destPath]) {
 
                    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:destPath error:&error];
                    if (!success) {
                        NSLog(@"删除文件失败:%@",error);
                    }else{
                        NSLog(@"删除文件:%@成功",destPath);
                    }
                }
                //  目录文件URL
                self.destURL = [NSURL fileURLWithPath:destPath];
//                self.destURL = [NSURL fileURLWithPath:destPath];
 
                NSURL *oldUrl = [NSURL fileURLWithPath:weakSelf.filePath];
                NSURL *newUrl = [NSURL fileURLWithPath:filePath];
                NSArray * sourceURLs = @[oldUrl,newUrl];
                [HMAudioComposition sourceURLs:sourceURLs composeToURL:self.destURL completed:^(NSError *error) {
                    if (error) {
                        NSLog(@"合并音频文件失败:%@",error);
                    }else{
                        NSLog(@"合并音频文件成功");
                        _filePath =destPath;
 
                    }
                }];
 
            }else{
                weakSelf.filePath = filePath;
 
            }
 
//            minValue = 0;
            if (minValue == 60) {
                recordBtn.hidden = YES;
                tipsLb.hidden = YES;
            }else{
                recordBtn.hidden = NO;
                tipsLb.hidden = NO;
            }
            sureBtn.hidden = NO;
 
            _animationView.hidden = YES;
            _recordNormalImg.hidden = NO;
            auditionBtn.hidden = NO;
            deleteBtn.hidden= NO;
 
 
        }];
 
    }else{
        sureBtn.hidden = NO;
//        auditionBtn.hidden = NO;
        [self hideLisenAddDelete:NO];
        //记录上次录制时间
        _oldtime = minValue;
        [[XHSoundRecorder sharedSoundRecorder] stopRecorder];
 
        tipsLb.text = @"点击可录音";
        _animationView.hidden = YES;
        _recordNormalImg.hidden = NO;
        auditionBtn.hidden = NO;
        deleteBtn.hidden= NO;
        [self timerPause];
    }
}


2.试听功能


#pragma mark  试听
-(void)auditionClick{
    
    isAudition = !isAudition;
    if (isAudition) {
        [auditionBtn setImage:[UIImage imageNamed:@"audio暂停"] forState:0];
        recordBtn.hidden = YES;
        tipsLb.hidden = YES;
        deleteBtn.hidden = YES;
        [[XHSoundRecorder sharedSoundRecorder] playsound:self.filePath withFinishPlaying:^{
            NSLog(@"播放结束");
            if (minValue == 60) {
                recordBtn.hidden = YES;
                tipsLb.hidden = YES;
            }else{
                recordBtn.hidden = NO;
                tipsLb.hidden = NO;
            }
            sureBtn.hidden = NO;
 
            deleteBtn.hidden = NO;
            isAudition = NO;
            [auditionBtn setImage:[UIImage imageNamed:@"audio试听"] forState:0];
            [self timerPause];
 
        }];
 
        
    }else{
        [[XHSoundRecorder sharedSoundRecorder] pausePlaysound];
        if (minValue == 60) {
            recordBtn.hidden = YES;
            tipsLb.hidden = YES;
        }else{
            recordBtn.hidden = NO;
            tipsLb.hidden = NO;
        }
        deleteBtn.hidden = NO;
        [auditionBtn setImage:[UIImage imageNamed:@"audio试听"] forState:0];
 
    }
}


本文简单介绍了iOS端的直播系统源代码,如何实现语音录制功能,以上代码出自于云豹直播系统源代码,关注我们可以了解更多音视频开发知识。

声明:以上内容为云豹科技原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com

声明:
以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任
立即查看