在Objective-C/C++ocoa中播放声音最简单的代码是什么?请注意,我的声音文件是.aif.
此外,我将项目中的声音文件放在哪里?
谢谢.
解决方法 要在Mac上播放音频,您可以使用NSSound
进行基本活动,也可以使用更强大的 CoreAudio
框架来实现更复杂的任务. 如果您使用NSSound来实现此目标,那么您的代码将如下所示:
Nsstring *resourcePath = [[NSBundle mainBundle] pathForResource:@"replace-with-file-name" ofType:@"aif"];NSSound *sound = [[NSSound alloc] initWithContentsOffile:resourcePath byReference:YES];// Do something with sound,like [sound play] and release.
要么
NSSound *sound = [NSSound soundnamed:@"replace-with-file-name-without-the-extension"];// Do something with sound,like [sound play],but don't release (it's autoreleased).
如果要使用第二个片段,则必须确保声音文件的扩展名为aiff而不仅仅是aif,因为soundnamed:查找非常具体的文件扩展名;我不认为aif是在这个名单上.
使用CoreAudio更复杂,所以如果你只想使用非常简单的设置来播放它,并使用NSSound
总结以上是内存溢出为你收集整理的objective-c – 播放声音文件的简单方法(.aif)全部内容,希望文章能够帮你解决objective-c – 播放声音文件的简单方法(.aif)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)