直播app动态录音功能,是怎样在手机直播app制作时实现的

有很多朋友喜欢在玩直播app的时候发布动态,因为直播平台内的动态是大家实现动态社交的一种方式,不仅可以让用户更加真实地了解主播,还可以与主播实现直播以外的互动交流。那么这个深受大家喜爱的功能是如何在手机直播app制作时实现的呢?
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];

    }
}

以上就是在手机直播app制作时,实现动态录音的大概流程,目前该功能已经被众多平台采用,从而有助于平台内部形成闭环社交圈,实现更大用户流量和更好用户留存,如果对文章内容感兴趣可持续关注。
声明:本文由作者原创,转载请注明出处及原文链接。

实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值