使用 NSData 解决生产者-消费者问题(用于音频流)
我正在使用 AVAssetReader 将 PCM 数据从 iPod 轨道复制到缓冲区,然后使用 RemoteIO 音频单元播放。我正在尝试创建一个单独的线程来加载声音数据,以便我可以在加载缓冲区时访问和播放数据。
我目前有一个大型 NSMutableData 对象,它最终保存整首歌曲的数据。目前,我使用 NSOperation 在单独的线程中加载音频数据,如下所示:
- AVAssetReaderOutput 一次最多复制 8192 个字节到 CMBlockBuffer
- 将这些字节复制到 NSData 对象
- 将此 NSData 对象附加到更大的 NSMutableData 对象(该对象最终保存整首歌曲)
- 完成后,通过访问 NSMutableData 对象中的每个数据包来播放歌曲
我试图在复制这些字节的同时播放歌曲。我不确定同时写入和读取文件的好方法是什么。
我有一个简短的想法:
- 创建并填充 3 个 NSData 对象,每个对象长度为 8192 字节,作为缓冲区。
- 开始玩吧。当我完成第一个缓冲区的播放后,将新数据加载到第一个缓冲区中。
- 当我完成第二个缓冲区的播放后,将新数据加载到第二个缓冲区中。第三个缓冲区也是如此
- 再次从第一个缓冲区开始播放,填充第三个缓冲区。等等。
或者,创建一个包含 3 * 8192 PCM 单元的 NSData 对象,并以某种方式使用两个不同的线程同时写入和读取它。
我的代码现在在两个不同的线程上运行。我将数据附加到数组,直到我按下播放,此时它停止(可能是因为线程被阻塞,但我现在不知道)并播放,直到它到达我加载的内容的末尾并导致 EXC_BAD_ACCESS 异常。
简而言之,我想找到在复制 PCM 数据时播放 PCM 数据的正确方法,例如一次复制 8192 个字节。我可能必须使用另一个线程来执行此操作(我现在正在使用 NSOperation),但不清楚如何同时写入和读取缓冲区,最好使用一些更高级别的 Objective-C 方法。
I am using AVAssetReader to copy PCM data from an iPod track to a buffer, which is then played with a RemoteIO audio unit. I am trying to create a separate thread for loading sound data, so that I can access and play data from the buffer while it is being loaded into.
I currently have a large NSMutableData object that eventually holds the entire song's data. Currently, I load audio data in a separate thread using NSOperation like so:
- AVAssetReaderOutput copies, at most, 8192 bytes at a time to a CMBlockBuffer
- Copy these bytes to a NSData object
- Append this NSData object to a larger NSMutableData object (which eventually holds the entire song)
- When finished, play the song by accessing each packet in the NSMutableData object
I'm trying to be able to play the song WHILE copying these bytes. I am unsure what a good way to write to and read from a file from the same time is.
A short idea I had:
- Create and fill 3 NSData objects, each 8192 bytes in length, as buffers.
- Start playing. When I have finished playing the first buffer, load new data into the first buffer.
- When I have finished playing the second buffer, load new data into the second. Same for the third
- Start playing from the first buffer again, fill the third. And so on.
Or, create one NSData object that holds 3 * 8192 PCM units, and somehow write to and read from it at the same time with two different threads.
I have my code working on two different threads right now. I append data to the array until I press play, at which point it stops (probably because the thread is blocked, but I don't know right now) and plays until it reaches the end of whatever I loaded and causes an EXC_BAD_ACCESS exception.
In short, I want to find the right way to play PCM data while it is being copied, say, 8192 bytes at a time. I will probably have to do so with another thread (I am using NSOperation right now), but am unclear on how to write to and read from a buffer at the same time, preferably using some higher level Objective-C methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在做这件事。您肯定需要在不同的线程上播放音频(我正在使用 RemoteIO 执行此操作)。您还需要使用循环缓冲区。如果您不熟悉此数据结构,您可能需要查找它,因为您将大量使用它来执行此类操作。我的一般设置如下:
就线程而言,这应该是您所需要的一切。您需要在两个线程之间有某种互斥体或信号量,以确保您在写入文件的同时不会尝试读取文件(这是不好的形式,会导致您崩溃)。我只是让我的两个线程设置一个布尔值并休眠一段时间,直到它被取消设置。可能有一种更复杂的方法可以做到这一点,但它适合我的目的。
希望有帮助!
I'm doing this exact thing. You will definitely need to play your audio on a different thread (I am doing this with RemoteIO). You will also need to use a circular buffer. You probably want to look up this data structure if you aren't familiar with it as you will be using it a lot for this type of operation. My general setup is as follows:
This should be about all you need as far as threads. You will need to have some sort of mutex or semaphore between the two threads to ensure you aren't trying to read your file while you are writing into it at the same time (this is bad form and will cause you to crash). I just have both my threads set a boolean and sleep for a while until it is unset. There is probably a more sophisticated way of doing this but it works for my purposes.
Hope that helps!