Giter Club home page Giter Club logo

qnqvsvoicecall_ios's Introduction

一、demo运行说明

替换项目中宏定义

#define QiNiuToken   @"从后台获取toekn"
#define nameSpaceId  @"空间ID"
#define deviceId     @"设备国标ID"

二、主要代码

录制音频文件

  self.path = [NSTemporaryDirectory() stringByAppendingString:@"recording.m4a"];
  self.recorder = [[AudioRecorder alloc] initWithFileUrl:[NSURL fileURLWithPath:self.path]];
  //开始录制
  [self.recorder startRecording];

提取PCM

//获取pcm数据
- (NSData *)extractPCM:(NSURL *)url {
    AVAsset *asset = [AVAsset assetWithURL:url];
    if (asset == nil) {
        NSLog(@"asset is not defined!");
        return nil;
    }
    NSLog(@"asset duration:%f",CMTimeGetSeconds(asset.duration));
    
    NSError *error;
    AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:asset error:&error];
    if (error) {
        NSLog(@"AVAssetReader init error:%@",error);
        return nil;
    }
    // 注意 需要转成采样率8K的 16bit
    // g711a采样率是8k 8bit
    NSDictionary *outputSetting = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                   [NSNumber numberWithFloat:8000], AVSampleRateKey,
                                   [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
                                   [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                   [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                   [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
                                   [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
                                   nil];;
    
    
    AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:[asset tracksWithMediaType:AVMediaTypeAudio].firstObject outputSettings:outputSetting];
    
    // 添加输出
    if([assetReader canAddOutput:output]) {
        [assetReader addOutput:output];
    } else {
        NSLog(@"add output failed!");
        return nil;
    }
    
    if(![assetReader startReading]) {
        NSLog(@"reading failed!");
    }
    
    NSMutableData *data = [[NSMutableData alloc]init];
    while (assetReader.status ==  AVAssetReaderStatusReading) {
        CMSampleBufferRef  sampleBuffer = [output copyNextSampleBuffer]; //读取到数据
        if (sampleBuffer) {
            CMBlockBufferRef blockBUfferRef = CMSampleBufferGetDataBuffer(sampleBuffer);//取出数据
            size_t length = CMBlockBufferGetDataLength(blockBUfferRef);   //返回一个大小,size_t针对不同的品台有不同的实现,扩展性更好
            SInt16 sampleBytes[length];
            CMBlockBufferCopyDataBytes(blockBUfferRef, 0, length, sampleBytes); //将数据放入数组
            [data appendBytes:sampleBytes length:length];                 //将数据附加到data中
            CMSampleBufferInvalidate(sampleBuffer);  //销毁
            CFRelease(sampleBuffer);                 //释放
        }
    }
    
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingString:@"recording.pcm"];
    if([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
    [data writeToFile:path atomically:NO];
    NSLog(@"reading pcm complete");
    return data;
}

PCM 转成 G711a

// pcm转成g711a
// pcm 的采样率 8k  16bit
// g711a 采样率 8k  8bit
- (NSData *)Pcm2G711a:(NSData *)pcmData {
    [self setStatus:@"convert g711  start"];
    char *pcmCdata = (char *)[pcmData bytes];
    char *g711data = malloc(pcmData.length/2);
    pcm16_alaw_tableinit();
    pcm16_to_alaw((int)pcmData.length, pcmCdata, g711data);
    
    NSData *data = [NSData dataWithBytes:g711data length:pcmData.length/2];
    NSString *path = [NSTemporaryDirectory() stringByAppendingString:@"recording.g711"];
    
    if([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
    [data writeToFile:path atomically:YES];
    
    [self setStatus:@"convert g711  end"];
    
    return data;
}

qnqvsvoicecall_ios's People

Contributors

hugolipeng avatar shawn-qiniu avatar

Watchers

Bai Long avatar  avatar maolifu avatar  avatar  avatar

Forkers

shawn-qiniu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.