iOS即时语音聊天技术实践.pdf

上传人:小小飞 文档编号:3330411 上传时间:2019-08-13 格式:PDF 页数:34 大小:2.78MB
返回 下载 相关 举报
iOS即时语音聊天技术实践.pdf_第1页
第1页 / 共34页
iOS即时语音聊天技术实践.pdf_第2页
第2页 / 共34页
iOS即时语音聊天技术实践.pdf_第3页
第3页 / 共34页
iOS即时语音聊天技术实践.pdf_第4页
第4页 / 共34页
iOS即时语音聊天技术实践.pdf_第5页
第5页 / 共34页
点击查看更多>>
资源描述

《iOS即时语音聊天技术实践.pdf》由会员分享,可在线阅读,更多相关《iOS即时语音聊天技术实践.pdf(34页珍藏版)》请在三一文库上搜索。

1、 iOSiOSiOSiOS即时语音聊天技术实践即时语音聊天技术实践 张天虹张天虹张天虹张天虹 爱图腾科技爱图腾科技爱图腾科技爱图腾科技 DemoDemoDemoDemo 录制播放 解压压缩 便于网络传输 语音录制 iOS默认支持语音录制格式 简单的录音API - AVAudioRecorder 录音时显示话筒音量 面向音频流的录音方式 AACAACAACAAC (MPEG-4 Advanced Audio Coding) ALAC ALAC ALAC ALAC (Apple Lossless) iLBCiLBCiLBCiLBC (internet Low Bitrate Codec, anot

2、her format for speech) IMA4IMA4IMA4IMA4 (IMA/ADPCM) Linear PCMLinear PCMLinear PCMLinear PCM (uncompressed, linear pulse- code modulation) -law and a-law-law and a-law-law and a-law-law and a-law iOS默认支持语音录制格式 简单的录音API - AVAudioRecorder 1.设置录音的目标文件 2.设置录音文件信息 3.实例化AVAudioRecorder 4.创建录音文件,准备录音 5.开始录

3、音 AVFormatIDKey: 录音格式(kAudioFormatLinearPCM , kAudioFormatULaw ) AVSampleRateKey: 录音采样率 (单位Hz, 8000, 44100, 96000) AVNumberOfChannelsKey: 录音通道数 (1, 2) AVLinearPCMBitDepthKey: 线性采样位数 (8, 16, 24, 32) 简单的录音API - AVAudioRecorder /语音录制目标文件 NSURL *audioRecordUrl = NSURL alloc initFileURLWithPath: RECORDER

4、_TEMP_FILE_PATH; /录音设置 NSMutableDictionary *recordSetting = NSMutableDictionary alloc init autorelease; /录音格式 recordSetting setValue :NSNumber numberWithInt:kAudioFormatLinearPCM forKey: AVFormatIDKeyAVFormatIDKeyAVFormatIDKeyAVFormatIDKey; /采样率 recordSetting setValue :NSNumber numberWithFloat:8000

5、forKey: AVSampleRateKeyAVSampleRateKeyAVSampleRateKeyAVSampleRateKey; /通道数 recordSetting setValue :NSNumber numberWithInt:1 forKey: AVNumberOfChannelsKeyAVNumberOfChannelsKeyAVNumberOfChannelsKeyAVNumberOfChannelsKey; /线性采样位数 recordSetting setValue :NSNumber numberWithInt:16 forKey: AVLinearPCMBitDe

6、pthKeyAVLinearPCMBitDepthKeyAVLinearPCMBitDepthKeyAVLinearPCMBitDepthKey; /实例化AVAudioRecorder AVAudioRecorder *recorder = AVAudioRecorder alloc initWithURL:audioRecordUrl settings:recordSetting error: /创建录音文件,准备录音 recorder prepareToRecord; /开始录音 recorder record; 录音时捕捉话筒音量 /开启音量检测开启音量检测 recorder. met

7、eringEnabled = YES; /设置定时检测设置定时检测 NSTimer scheduledTimerWithTimeInterval: 0.05 target: self selector: selector(levelTimerCallback:) userInfo: nil repeats: YES; /音量检测音量检测 - (void)levelTimerCallback:(NSTimer *)timer /刷新音量数据 recorder updateMeters; /获取音量的平均值 CGFloat averagePower = recorder averagePowerF

8、orChannel:0; /获取音量的峰值 CGFloat peakPower = recorder peakPowerForChannel:0); /更改UI的图形效果 面向音频流的录音方式 SpeakHere demo https:/ roduction/Intro.html Audio Queue Services Audio File Services Audio Session Services AAC文件录制过程 Codec (coder-decoder) :Codec编码解码器主要作用是对音 频信号进行压缩和解压缩。 1.从话筒中录制PCM数据 2.Codec将PCM数据转换为A

9、AC数据格式 3.将AAC数据写如硬盘文件 我们应该如何选择Codec Apple Lossless iLBCIMA/ADPCM Linear PCMLaw and aLaw MP3WMAMIDIOggspeex 如何使用开源如何使用开源CodecCodecCodecCodec i386i386i386i386 iOS Simulator ARMv6ARMv6ARMv6ARMv6 (iPhone 2G/3G, iPod 1G/2G) ARMv7ARMv7ARMv7ARMv7 (iPhone3GS/4, iPod 3G, iPad) libCodec.a Xcode 获取PCM文件的语音数据 (

10、PCM文件默认格式为WAVE) Chunk ID “datadatadatadata” ChunkData Size Digital Audio Samples 调用Codec的Encode方法 = codec_encode() 组装编码后的文件 Codec Format Header Encoded Data AACAACAACAAC (MPEG-4 Advanced Audio Coding) ALAC ALAC ALAC ALAC (Apple Lossless) HE-AACHE-AACHE-AACHE-AAC (MPEG-4 High Efficiency AAC) iLBCiLBC

11、iLBCiLBC (internet Low Bitrate Codec, another format for speech) IMA4IMA4IMA4IMA4 (IMA/ADPCM) Linear PCMLinear PCMLinear PCMLinear PCM (uncompressed, linear pulse- code modulation) MP3MP3MP3MP3 (MPEG-1 audio layer 3) -law and a-law-law and a-law-law and a-law-law and a-law iOS默认支持语音播放格式 语音播放API - AV

12、AudioPlayer /实例化实例化AVAudioPlayerAVAudioPlayerAVAudioPlayerAVAudioPlayer AVAudioPlayer *player = AVAudioPlayer alloc initWithData:soundData error: /准备播放准备播放 player prepareToPlay; /开始播放开始播放 player play; AAC文件播放过程 1.读取硬盘文件中的AAC语音文件 2.Codec将AAC数据转换为PCM数据格式 3.播放PCM语音文件 获取使用Codec编码过的语音文件的语音数据 Chunk ID “da

13、tadatadatadata” ChunkData Size Digital Audio Samples 调用Codec的Decode方法 = codec_decode() 将Digital Audio Samples Data恢复为WAVE文件结构 Codec Format Header Encoded Data Audio Session 如何处理多个应用的音频输出? 锁屏和静音的情况是否继续播放声音? 是否支持语音录制? 是否支持语音播放? AVAudioSession:iOS中用于管理音频上下文的单例对象 应用中可以设置的几种音频模式 CategoryCategoryCategoryC

14、ategory identifiersidentifiersidentifiersidentifiers* * * * Silenced by the Silenced by the Silenced by the Silenced by the Ring/Silent switch Ring/Silent switch Ring/Silent switch Ring/Silent switch and by screen lockingand by screen lockingand by screen lockingand by screen locking Allows audio fr

15、om Allows audio from Allows audio from Allows audio from other applicationsother applicationsother applicationsother applications Allows audio input Allows audio input Allows audio input Allows audio input (recording) and (recording) and (recording) and (recording) and output (playback)output (playb

16、ack)output (playback)output (playback) AVAudioSessionCategoryAmbien t YesYesOutput only AVAudioSessionCategorySoloAm bient YesNoOutput only AVAudioSessionCategoryPlayba ck No No by default; yes by using override switch Output only AVAudioSessionCategoryRecordNoNoInput only AVAudioSessionCategoryPlay

17、An dRecord No (recording continues with the screen locked) No by default; yes by using override switch Input and output AVAudioSessionCategoryAudioP rocessing -No No input and no output AVAudioSession sharedInstance setCategory: AVAudioSessionCategoryAmbient error: 1.下载Speex Codec http:/www.speex.or

18、g/downloads/ 2.编译适合Xcode开发的Speex codec lib 编译支持编译支持iOSiOSiOSiOS Simulator Simulator Simulator Simulator的的liblibliblib / / / /configureconfigureconfigureconfigure -prefix-prefix-prefix-prefix=/Users/Rainbow/speex/i386 hosthosthosthost=i386-apple-darwin buildbuildbuildbuild=x86_64-apple-darwin10.8.0 C

19、CCCCCCC=”/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -stdstdstdstd=c99 -arch-arch-arch-arch i386 - - - -isysrootisysrootisysrootisysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/” 编译支持编译支持armv6armv6armv6armv6的的iOSiOSiOSiOS设备的设备的l

20、iblibliblib / / / /configureconfigureconfigureconfigure - - - -prefixprefixprefixprefix=/Users/Rainbow/speex/armv6 编译支持编译支持armv7armv7armv7armv7的的iOSiOSiOSiOS设备的设备的liblibliblib / / / /configureconfigureconfigureconfigure - - - -prefixprefixprefixprefix=/Users/Rainbow/speex/armv7 合并为合并为libspeex.alibsp

21、eex.alibspeex.alibspeex.a文文件件 l l l lipoipoipoipo -create-create-create-create i386/lib/libspeex.a armv6/lib/libspeex.a armv7/lib/libspeex.a -outputoutputoutputoutput libspeex.a 3.将libspeex.a引入Xcode开发环境中 将将libspeex.alibspeex.alibspeex.alibspeex.a文件添加到链接库中文件添加到链接库中 在在Header Search PathsHeader Search

22、PathsHeader Search PathsHeader Search Paths中加入中加入speexspeexspeexspeex的头文件目录的头文件目录 引入引入speex.hspeex.hspeex.hspeex.h #include 4.获取录制的PCM文件的音频数据 Chunk ID “datadatadatadata” ChunkData Size Digital Audio Samples /获获取取PCMPCMPCMPCM数据数据 NSData *pcmData = NSData dataWithContentsOfURL:soundFileUrl; /解析解析Chunk

23、 IDChunk IDChunk IDChunk ID和和ChunkDataChunkDataChunkDataChunkData Size Size Size Size char trunkId4; int length; pcmData getBytes: pcmData getBytes: NSString *trunkId = NSString alloc initWithBytes:trunkId length:4 encoding:NSASCIIStringEncoding; /获取获取Chunk IDChunk IDChunk IDChunk ID为为“ “ “ “datadat

24、adatadata” ” ” ”的数据,即纯录制的音频数据的数据,即纯录制的音频数据 if (“data“ isEqualToString:str) rawData = NSMutableData dataWithData:pcmData subdataWithRange:NSMakeRange(8, length); 5.将PCM数据编码为speex格式 6.在编码的speex数据前添加speex文件的头信息 7.通过Socket发送speex文件数据到服务器 8.另外一台设备接受speex文件并解码为PCM音频数据 9.播放PCM文件 speex_encode_int(enc_state, PCM_SAMPLE_FRAMEPCM_SAMPLE_FRAMEPCM_SAMPLE_FRAMEPCM_SAMPLE_FRAME, nbBytes = speex_bits_write( speex_bits_read_from( speex_decode_int(dec_state, JSpeex: Java port of the Speex speech codec http:/ 邮箱: 微博:http:/ 欢迎共同探讨交流欢迎共同探讨交流

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/环境 > 装饰装潢


经营许可证编号:宁ICP备18001539号-1