CoreAudio - 如何确定播放aac文件的结尾
我正在 iPhone 上使用 CoreAudio,但我无法找到如何知道歌曲何时播放完毕。
我在 kAudioQueueProperty_IsRunning 上放置了一个属性侦听器,它在开始播放时起作用,但在文件末尾不起作用。当我停止 AudioQueue 时它就起作用了...
你有想法吗?
多谢。
PS:我正在使用优秀书籍《iPhone Cool Projects》中的示例代码:http://apress.com/book/ downloadfile/4453
编辑:
本书附带的代码中有一个错误。该修复需要对 -[AudioPlayer audioRequestDidFinish:] 进行小修改,以便它调用 [queue endOfStream]。
I am playing with CoreAudio on the iPhone, and I am unable to find how to know when the song has finished to play.
I put a property listener on kAudioQueueProperty_IsRunning
, it is working when starting playing but not at the end of the file. It's work when I stop AudioQueue...
Do you have an idea ?
Thanks a lot.
PS : I am using sample code from the excellent book iPhone Cool Projects : http://apress.com/book/downloadfile/4453
Edit :
there’s a bug in the code that went out with the book. The fix requires a small modification to -[AudioPlayer audioRequestDidFinish:] so that it calls [queue endOfStream].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Uli Kusterer 的此类清楚地说明了此功能。
标题如下:
实现如下:
当您调用
initWithContentsOfURL: (NSURL*)theURL
方法时,UKSoundAQPropertyListenerCallback
将添加到音频队列中。它被设置为响应kAudioQueueProperty_IsRunning
音频队列属性。调用play
方法并且文件播放完毕后,将调用UKSoundAQPropertyListenerCallback
,后者又调用notifyDelegatePlaybackStateChanged
方法。我已向此方法添加了一条 NSLog 消息,以说明文件何时停止播放。我很乐意分享一个充分演示此功能的 Xcode 项目。
This class from Uli Kusterer illustrates this functionality clearly.
Here's the header:
Here's the implementation:
When you call the
initWithContentsOfURL: (NSURL*)theURL
method, theUKSoundAQPropertyListenerCallback
is added to the audio queue. It is set to respond to thekAudioQueueProperty_IsRunning
Audio Queue Property. After theplay
method is called and the file finishes playing,UKSoundAQPropertyListenerCallback
is called which in turn calls thenotifyDelegatePlaybackStateChanged
method. I have added an NSLog message to this method to illustrate when the file stops playing.I'd be happy to share an Xcode project that fully demonstrates this functionality.
如果您使用 AudioQueue API,则需要手动填充缓冲区,因此您应该知道何时没有更多数据。然后,您可以使用
performSelectorOnMainThread
向您的应用发送消息,告知您已完成游戏。如果您需要知道 AudioConverter 何时位于文件末尾,它通常会使用零长度缓冲区发出信号。
If you're using the AudioQueue API, you're filling the buffers manually, so you should know when you have no more data. You can then use
performSelectorOnMainThread
to message your app that you're done playing.If you need to know when your AudioConverter is at the end of the file, it usually signals it with zero-length buffers.