下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 iOS即时语音聊天技术实践

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

iOS即时语音聊天技术实践

UC10503
2012-10-15 0人阅读 举报 0 0 暂无简介

简介:本文档为《iOS即时语音聊天技术实践pdf》,可适用于IT/计算机领域

iOSiOSiOSiOS即时语音聊天技术实践张天虹张天虹张天虹张天虹爱图腾科技爱图腾科技爱图腾科技爱图腾科技DemoDemoDemoDemo录制播放解压压缩便于网络传输语音录制iOS默认支持语音录制格式简单的录音APIAVAudioRecorder录音时显示话筒音量面向音频流的录音方式AACAACAACAAC(MPEGAdvancedAudioCoding)ALACALACALACALAC(AppleLossless)iLBCiLBCiLBCiLBC(internetLowBitrateCodec,anotherformatforspeech)IMAIMAIMAIMA(IMAADPCM)LinearPCMLinearPCMLinearPCMLinearPCM(uncompressed,linearpulsecodemodulation)µµµµlawandalawlawandalawlawandalawlawandalawiOS默认支持语音录制格式简单的录音APIAVAudioRecorder设置录音的目标文件设置录音文件信息实例化AVAudioRecorder创建录音文件准备录音开始录音AVFormatIDKey:录音格式(kAudioFormatLinearPCM,kAudioFormatULaw  )AVSampleRateKey:录音采样率(单位Hz,,,)AVNumberOfChannelsKey:录音通道数(,)AVLinearPCMBitDepthKey:线性采样位数(,,,)简单的录音APIAVAudioRecorder语音录制目标文件NSURL*audioRecordUrl=NSURLallocinitFileURLWithPath:RECORDERTEMPFILEPATH录音设置NSMutableDictionary*recordSetting=NSMutableDictionaryallocinitautorelease录音格式recordSettingsetValue:NSNumbernumberWithInt:kAudioFormatLinearPCMforKey:AVFormatIDKeyAVFormatIDKeyAVFormatIDKeyAVFormatIDKey采样率recordSettingsetValue:NSNumbernumberWithFloat:forKey:AVSampleRateKeyAVSampleRateKeyAVSampleRateKeyAVSampleRateKey通道数recordSettingsetValue:NSNumbernumberWithInt:forKey:AVNumberOfChannelsKeyAVNumberOfChannelsKeyAVNumberOfChannelsKeyAVNumberOfChannelsKey线性采样位数recordSettingsetValue:NSNumbernumberWithInt:forKey:AVLinearPCMBitDepthKeyAVLinearPCMBitDepthKeyAVLinearPCMBitDepthKeyAVLinearPCMBitDepthKey实例化AVAudioRecorderAVAudioRecorder*recorder=AVAudioRecorderallocinitWithURL:audioRecordUrlsettings:recordSettingerror:error创建录音文件准备录音recorderprepareToRecord开始录音recorderrecord录音时捕捉话筒音量开启音量检测recordermeteringEnabled=YES设置定时检测NSTimerscheduledTimerWithTimeInterval:target:selfselector:selector(levelTimerCallback:)userInfo:nilrepeats:YES音量检测(void)levelTimerCallback:(NSTimer*)timer{刷新音量数据recorderupdateMeters获取音量的平均值CGFloataveragePower=recorderaveragePowerForChannel:获取音量的峰值CGFloatpeakPower=recorderpeakPowerForChannel:)  更改UI的图形效果}面向音频流的录音方式SpeakHeredemohttps:developerapplecomlibraryios#samplecodeSpeakHereIntroductionIntrohtmlAudioQueueServicesAudioFileServicesAudioSessionServicesAAC文件录制过程Codec(coderdecoder):Codec编码解码器主要作用是对音频信号进行压缩和解压缩。从话筒中录制PCM数据Codec将PCM数据转换为AAC数据格式将AAC数据写如硬盘文件我们应该如何选择CodecAppleLosslessiLBCIMAADPCMLinearPCMµLawandaLawMPWMAMIDIOggspeex如何使用开源CodecCodecCodecCodeciiiiiOSSimulatorARMvARMvARMvARMv(iPhoneGG,iPodGG)ARMvARMvARMvARMv(iPhoneGS,iPodG,iPad)libCodecaXcode获取PCM文件的语音数据(PCM文件默认格式为WAVE)ChunkID“datadatadatadata”ChunkDataSizeDigitalAudioSamples调用Codec的Encode方法<EncodedDataEncodedDataEncodedDataEncodedData>=codecencode(<DigitalAudioSamplesDataDigitalAudioSamplesDataDigitalAudioSamplesDataDigitalAudioSamplesData>)组装编码后的文件CodecFormatHeaderEncodedDataAACAACAACAAC(MPEGAdvancedAudioCoding)ALACALACALACALAC(AppleLossless)HEAACHEAACHEAACHEAAC(MPEGHighEfficiencyAAC)iLBCiLBCiLBCiLBC(internetLowBitrateCodec,anotherformatforspeech)IMAIMAIMAIMA(IMAADPCM)LinearPCMLinearPCMLinearPCMLinearPCM(uncompressed,linearpulsecodemodulation)MPMPMPMP(MPEGaudiolayer)µµµµlawandalawlawandalawlawandalawlawandalawiOS默认支持语音播放格式语音播放APIAVAudioPlayer实例化AVAudioPlayerAVAudioPlayerAVAudioPlayerAVAudioPlayerAVAudioPlayer*player=AVAudioPlayerallocinitWithData:soundDataerror:error准备播放playerprepareToPlay开始播放playerplayAAC文件播放过程读取硬盘文件中的AAC语音文件Codec将AAC数据转换为PCM数据格式播放PCM语音文件获取使用Codec编码过的语音文件的语音数据ChunkID“datadatadatadata”ChunkDataSizeDigitalAudioSamples调用Codec的Decode方法<DigitalAudioSamplesDataDigitalAudioSamplesDataDigitalAudioSamplesDataDigitalAudioSamplesData>=codecdecode(<EncodedDataEncodedDataEncodedDataEncodedData>)将DigitalAudioSamplesData恢复为WAVE文件结构CodecFormatHeaderEncodedDataAudioSession如何处理多个应用的音频输出?锁屏和静音的情况是否继续播放声音?是否支持语音录制?是否支持语音播放?AVAudioSession:iOS中用于管理音频上下文的单例对象应用中可以设置的几种音频模式CategoryCategoryCategoryCategoryidentifiersidentifiersidentifiersidentifiers****SilencedbytheSilencedbytheSilencedbytheSilencedbytheRingSilentswitchRingSilentswitchRingSilentswitchRingSilentswitchandbyscreenlockingandbyscreenlockingandbyscreenlockingandbyscreenlockingAllowsaudiofromAllowsaudiofromAllowsaudiofromAllowsaudiofromotherapplicationsotherapplicationsotherapplicationsotherapplicationsAllowsaudioinputAllowsaudioinputAllowsaudioinputAllowsaudioinput(recording)and(recording)and(recording)and(recording)andoutput(playback)output(playback)output(playback)output(playback)AVAudioSessionCategoryAmbientYesYesOutputonlyAVAudioSessionCategorySoloAmbientYesNoOutputonlyAVAudioSessionCategoryPlaybackNoNobydefaultyesbyusingoverrideswitchOutputonlyAVAudioSessionCategoryRecordNoNoInputonlyAVAudioSessionCategoryPlayAndRecordNo(recordingcontinueswiththescreenlocked)NobydefaultyesbyusingoverrideswitchInputandoutputAVAudioSessionCategoryAudioProcessingNoNoinputandnooutputAVAudioSessionsharedInstancesetCategory:AVAudioSessionCategoryAmbienterror:error下载SpeexCodechttp:wwwspeexorgdownloads编译适合Xcode开发的Speexcodeclib编译支持iOSiOSiOSiOSSimulatorSimulatorSimulatorSimulator的liblibliblibconfigureconfigureconfigureconfigureprefixprefixprefixprefix=UsersRainbowspeexi––––hosthosthosthost=iappledarwin––––buildbuildbuildbuild=xappledarwinCCCCCCCC=”DeveloperPlatformsiPhoneSimulatorplatformDeveloperusrbingccstdstdstdstd=carcharcharcharchiisysrootisysrootisysrootisysrootDeveloperPlatformsiPhoneSimulatorplatformDeveloperSDKsiPhoneSimulatorsdk”编译支持armvarmvarmvarmv的iOSiOSiOSiOS设备的liblibliblibconfigureconfigureconfigureconfigureprefixprefixprefixprefix=UsersRainbowspeexarmv  编译支持armvarmvarmvarmv的iOSiOSiOSiOS设备的liblibliblibconfigureconfigureconfigureconfigureprefixprefixprefixprefix=UsersRainbowspeexarmv  合并为libspeexalibspeexalibspeexalibspeexa文件llllipoipoipoipocreatecreatecreatecreateiliblibspeexaarmvliblibspeexaarmvliblibspeexaoutputoutputoutputoutputlibspeexa将libspeexa引入Xcode开发环境中将libspeexalibspeexalibspeexalibspeexa文件添加到链接库中在HeaderSearchPathsHeaderSearchPathsHeaderSearchPathsHeaderSearchPaths中加入speexspeexspeexspeex的头文件目录引入speexhspeexhspeexhspeexh#include<speexspeexh>获取录制的PCM文件的音频数据ChunkID“datadatadatadata”ChunkDataSizeDigitalAudioSamples获取PCMPCMPCMPCM数据NSData*pcmData=NSDatadataWithContentsOfURL:soundFileUrl解析ChunkIDChunkIDChunkIDChunkID和ChunkDataChunkDataChunkDataChunkDataSizeSizeSizeSizechartrunkIdintlengthpcmDatagetBytes:trunkIdrange:NSMakeRange(,)pcmDatagetBytes:lengthrange:NSMakeRange(,sizeof(length))NSString*trunkId=NSStringallocinitWithBytes:trunkIdlength:encoding:NSASCIIStringEncoding获取ChunkIDChunkIDChunkIDChunkID为““““datadatadatadata””””的数据即纯录制的音频数据if("data"isEqualToString:str){rawData=NSMutableDatadataWithData:pcmDatasubdataWithRange:NSMakeRange(,length)}将PCM数据编码为speex格式在编码的speex数据前添加speex文件的头信息通过Socket发送speex文件数据到服务器另外一台设备接受speex文件并解码为PCM音频数据播放PCM文件speexencodeint(encstate,PCMSAMPLEFRAMEPCMSAMPLEFRAMEPCMSAMPLEFRAMEPCMSAMPLEFRAME,bits)nbBytes=speexbitswrite(bits,SPEEXDATASPEEXDATASPEEXDATASPEEXDATA,MAXNBBYTES)speexbitsreadfrom(bits,SPEEXDATASPEEXDATASPEEXDATASPEEXDATA,nbBytes)speexdecodeint(decstate,bits,PCMSAMPLEFRAMEPCMSAMPLEFRAMEPCMSAMPLEFRAMEPCMSAMPLEFRAME)JSpeex:JavaportoftheSpeexspeechcodechttp:jspeexsourceforgenet邮箱:rainbowzhangitotemcomcn微博:http:weibocomzhangtianhong欢迎共同探讨交流

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/34

iOS即时语音聊天技术实践

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利