简体   繁体   English

iOS:从流中播放PCM缓冲区

[英]iOS: Playing PCM buffers from a stream

I'm receiving a series of UDP packets from a socket containing encoded PCM buffers. 我从包含编码PCM缓冲区的套接字收到一系列UDP数据包。 After decoding them, I'm left with an int16 * audio buffer, which I'd like to immediately play back. 解码后,我留下了一个int16 *音频缓冲区,我想立即播放。

The intended logic goes something like this: 预期的逻辑是这样的:

init(){
   initTrack(track, output, channels, sample_rate, ...);
}

onReceiveBufferFromSocket(NSData data){
   //Decode the buffer
   int16 * buf = handle_data(data);

   //Play data
   write_to_track(track, buf, length_of_buf, etc);
}

I'm not sure about everything that has to do with playing back the buffers though. 我不确定与播放缓冲区有关的所有内容。 On Android, I'm able to achieve this by creating an AudioTrack object, setting it up by specifying a sample rate, a format, channels, etc... and then just calling the "write" method with the buffer (like I wish I could in my pseudo-code above) but on iOS I'm coming up short. 在Android上,我可以通过创建一个AudioTrack对象来实现这一点,通过指定采样率,格式,通道等来设置它...然后只需用缓冲区调用“write”方法(就像我希望的那样)我可以在上面的伪代码中)但在iOS上我很快就会出现。

I tried using the Audio File Stream Services, but I'm guessing I'm doing something wrong since no sound ever comes out and I feel like those functions by themselves don't actually do any playback. 我尝试使用音频文件流服务,但我猜我做错了,因为没有声音出现,我觉得这些功能本身并没有实际播放。 I also attempted to understand the Audio Queue Services (which I think might be close to what I want), however I was unable to find any simple code samples for its usage. 我还试图理解音频队列服务(我认为它可能接近我想要的),但是我无法找到任何简单的代码示例来使用它。

Any help would be greatly appreciated, specially in the form of example code. 任何帮助将不胜感激,特别是以示例代码的形式。

You need to use some type of buffer to hold your incoming UDP data. 您需要使用某种类型的缓冲区来保存传入的UDP数据。 This is an easy and good circular buffer that I have used. 是我用过的一个简单而好的循环缓冲区。

Then to play back data from the buffer, you can use Audio Unit framework. 然后,要从缓冲区回放数据,您可以使用Audio Unit框架。 Here is a good example project. 是一个很好的示例项目。

Note: The first link also shows you how to playback using Audio Unit. 注意:第一个链接还向您显示如何使用音频单元进行播放。

You could use audioQueue services as well, make sure your doing some kind of packet re-ordering, if your using ffmpeg to decode the streams there is an option for this. 您也可以使用audioQueue服务,确保您进行某种数据包重新排序,如果您使用ffmpeg对流进行解码,则可以选择此选项。

otherwise audio queues are easy to set up. 否则音频队列很容易设置。

https://github.com/mooncatventures-group/iFrameExtractor/blob/master/Classes/AudioController.m https://github.com/mooncatventures-group/iFrameExtractor/blob/master/Classes/AudioController.m

You could also use AudioUnits, a bit more complicated though. 你也可以使用AudioUnits,虽然有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM