[TOC] # **本地播放** ## PlayStreamVoice 流式播放音频 **流式播放音频** 1.接口说明 设置本地播放数据回调,流式播放音频数据,每10ms引擎回调取10ms音频数据。如若填充了数据,在回调函数中返回true,如若播放完毕,在回调函数中返回false 2.函数原型 ~~~ public delegate bool NeedAudioData(byte[] data, int samplesNumPerChannel , SampleRate sampleRate, ChannelFormat channelFmt, Format fmt); int PlayStreamVoice(NeedAudioData needAudio); ~~~ * 参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | date | byte[] | 音频流数据 | | samplesNumPerChannel | int | 音频流单通道10ms数据长度,值为sampleRate/100 | | sampleRate | SampleRate | 音频流的采样率 | | channelFmt| ChannelFormat | 音频流通道类型,单声道或双声道 | | fmt | Format | 音频流数据格式与位深| * SampleRate | 类型 | 值 | | --- | --- | | SAMPLERATE_8KHZ | 8000 | | SAMPLERATE_16KHZ | 16000 | | SAMPLERATE_32KHZ | 32000 | | SAMPLERATE_48KHZ | 48000 | | SAMPLERATE_481KHZ | 44100 | * ChannelFormat | 类型 | 值 | | --- | --- | | MONO | 1 | | STEREO | 2 | * Format | 类型 | 值 | | --- | --- | | FORMAT_S16 | 1,交织,short,单样点字节数:2 | | FORMAT_FLT | 3,交织,float,单样点字节数:4 | | FORMAT_S16P | 6,平面,short,单样点字节数:2 | | FORMAT_FLTP | 8,平面,float,单样点字节数4 | 3.示例代码 ~~~ client.PlayStreamVoice(NeedAudioData); bool NeedAudioData(byte[] data, int samplesNumPerChannel, SampleRate sampleRate, ChannelFormat channelFmt, Format fmt) { int size = samplesNumPerChannel * (int)channelFmt * (fmt == Format.FORMAT_S16 ? 2 : 4); if (data.Length >= size) { var tmp = asyncBuffer.Pop(size); if(tmp != null) { Array.Copy(tmp, data, size); } } return true; } ~~~ 4.返回值 错误返回错误码,成功返回0。 ## SetExternMixer 外部播放音频 **外部扩展播放音频** 1.接口说明 设置外部扩展播放数据回调,在回调函数中返回true,则播放内部lark数据,返回false则不播放内部数据。 2.函数原型 ~~~ public delegate bool RecvAudioData(string ssrc, byte[] data, int samplesNumPerChannel, SampleRate sampleRate, ChannelFormat channelFmt, Format fmt); void SetExternMixer(RecvAudioData recvAudioData); ~~~ * 参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | ssrc | string | 音频流id | | data | byte[] | 音频流数据 | | samplesNumPerChannel | int | 音频流单通道10ms数据长度,值为sampleRate/100 | | sampleRate | SampleRate | 音频流的采样率 | | channelFmt| ChannelFormat | 音频流通道类型,单声道或双声道 | | fmt | Format | 音频流数据格式与位深| * SampleRate | 类型 | 值 | | --- | --- | | SAMPLERATE_8KHZ | 8000 | | SAMPLERATE_16KHZ | 16000 | | SAMPLERATE_32KHZ | 32000 | | SAMPLERATE_48KHZ | 48000 | | SAMPLERATE_481KHZ | 44100 | * ChannelFormat | 类型 | 值 | | --- | --- | | MONO | 1 | | STEREO | 2 | * Format | 类型 | 值 | | --- | --- | | FORMAT_S16 | 1,交织,short,单样点字节数:2 | | FORMAT_FLT | 3,交织,float,单样点字节数:4 | | FORMAT_S16P | 6,平面,short,单样点字节数:2 | | FORMAT_FLTP | 8,平面,float,单样点字节数4 | 3.示例代码 ~~~ client.SetExternMixer(HandleRecvAudioData); bool HandleRecvAudioData(string ssrc, byte[] data, int samplesNumPerChannel, SampleRate sampleRate, ChannelFormat channelFmt, Format fmt) { int size = samplesNumPerChannel * (int)channelFmt * (fmt == Format.FORMAT_S16 ? 2 : 4); if (data.Length >= size) { byte[] tmp = new byte[size]; Array.Copy(data, tmp, size); asyncBuffer.Push(tmp); } return false; } ~~~ 4.返回值 无