使用 AVAudioplayer 进行流式传输
为了加载声音文件,我的应用程序中有以下代码:
- (id) init:(NSString*)a_filename ext:(NSString*)a_ext
{
...
NSString *t_soundFilePath = [CFileLoader getPathForResource:filename WithExtension:ext];
NSURL *t_fileURL = [[[NSURL alloc] initFileURLWithPath: t_soundFilePath] autorelease];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: t_fileURL error: nil];
[player prepareToPlay];
...
}
我加载的所有声音都在我的包中,所以我想知道方法“initwithcontentsofurl”是否流式传输声音文件,或者是否所有文件都在缓存。
我的应用程序中有很多精灵,因此我想最大限度地减少用于声音的内存空间。
谢谢你的帮助
In order to load a sound file, I have the following code in my application :
- (id) init:(NSString*)a_filename ext:(NSString*)a_ext
{
...
NSString *t_soundFilePath = [CFileLoader getPathForResource:filename WithExtension:ext];
NSURL *t_fileURL = [[[NSURL alloc] initFileURLWithPath: t_soundFilePath] autorelease];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: t_fileURL error: nil];
[player prepareToPlay];
...
}
All the sounds that I load are in my bundle, so I would like to know if the method "initwithcontentsofurl" stream the sound file or if all the file is cached.
I have a lot of sprites in my app so I want to minimize memory space used for sounds.
thx for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用 AVPlayer (不是 AVAudioPlayer)来传输背景音频。
I used AVPlayer (not AVAudioPlayer) to stream background audio.
使用 AVAudioPlayer 播放的声音是从存储中实时传输的。缺点是您偶尔会在启动时注意到轻微的延迟。如果您使用 OpenAL,声音将完全加载到内存中。
ObjectAL 文档中对此类事情进行了很好的讨论- 一个旨在简化 iPhone 上声音和效果播放的库。
Sounds that are played using the AVAudioPlayer are streamed real time from storage. The drawback being that you can occasionally notice a small lag when it is due to start up. If you use OpenAL the sounds are loaded entirely into memory.
There's a good discussion of this sort of thing in the ObjectAL documentation - a library that is meant to simplify the playing of sounds and effects on the iPhone.
如果您想在应用程序中流式传输音频,可以使用以下播放器而不是使用 AVAudioPlayer
https://github.com/ mattgallagher/AudioStreamer
对于此播放器,您不需要将声音文件放入包中,您可以将它们放在服务器上并使用 url 来传输音频。
希望这会对您有所帮助。
If you want to stream audio in your app you can use the following player instead of using AVAudioPlayer
https://github.com/mattgallagher/AudioStreamer
For this player you don't need to put sound files in your bundle, you can put them at server and use url to stream audio.
Hope, this wil help you.
https://developer.apple.com /library/ios/qa/qa1634/_index.html
但你可以解决 - 例如异步写入本地文件,并使用此文件 URL 初始化 AVAudioPlayer,它将起作用。
https://developer.apple.com/library/ios/qa/qa1634/_index.html
But you can work around - for example write asynchronously to local file, and init AVAudioPlayer with this file URL, it will work.