macos 播放声音文件 (.aif) 的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6022729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to play a sound file (.aif)
提问by Andy
I'm making an app for Mac OS X using Xcode, and I want it to play a alert sound when something happens.
我正在使用 Xcode 为 Mac OS X 制作一个应用程序,我希望它在发生某些事情时播放警报声。
What is the simplest code for playing a sound in Objective-C/Cocoa? Note that my sound file is an .aif
.
在 Objective-C/Cocoa 中播放声音的最简单代码是什么?请注意,我的声音文件是.aif
.
Also, where do I put the sound file inside my project?
另外,我应该将声音文件放在我的项目中的什么位置?
Thank you.
谢谢你。
回答by Itai Ferber
To play audio on the Mac, you can either use NSSound
for basic activities, or the more robust CoreAudio
framework for more complicated tasks.
要在 Mac 上播放音频,您可以使用NSSound
基本活动,也可以使用更强大的CoreAudio
框架来处理更复杂的任务。
If you were to use NSSound
to achieve this goal, your code would look something like this:
如果您要使用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.
OR
或者
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).
If you were to use the second snippet, you would have to ensure that the sound file's extension is aiff
and not just aif
, since +soundNamed:
looks for very specific file extensions; I don't think aif
is on that list.
如果要使用第二个代码段,则必须确保声音文件的扩展名是aiff
,而不仅仅是aif
,因为要+soundNamed:
查找非常具体的文件扩展名;我认为不在aif
那个名单上。
Using CoreAudio
is more complicated, so if you just want to play it and mess around with very simple settings, just use NSSound
使用起来CoreAudio
比较复杂,所以如果你只是想玩它并且用非常简单的设置乱搞,只需使用NSSound
回答by jscs
Basic playback of sound is quite easy.
声音的基本播放非常简单。
You can use the NSSound
class to load the file in a few different ways, for example, by name:
您可以使用NSSound
该类以几种不同的方式加载文件,例如,按名称:
NSSound * myAwesomeSound = [NSSound soundNamed:@"AwesomeSound"];
This will need to be retained if you want to keep it around.
如果您想保留它,则需要保留它。
in this case the class will search certain particular directories, including your application bundle, for a sound file with that name. One important note is that the file must have the .aiff
(with twof's) extension in order to be found by this method.*
在这种情况下,该类将在某些特定目录(包括您的应用程序包)中搜索具有该名称的声音文件。一个重要的注意事项是该文件必须具有.aiff
(带有两个f 的)扩展名才能通过此方法找到。*
You would probably store the sound file in the Resources folder of your project; that's where "media" files are commonly kept.
您可能会将声音文件存储在项目的 Resources 文件夹中;这就是通常保存“媒体”文件的地方。
Then you can play it very simply, perhaps using a button press:
然后你可以非常简单地播放它,也许使用按钮按下:
- (IBAction)playTheSound:(id)sender {
NSSound * myAwesomeSound = [NSSound soundNamed:@"AwesomeSound"];
[myAwesomeSound play];
}
It's also possible to do some basic transport control: pausing, stopping, checking whether the sound has finished, and so on. Please see the Sound Programming Topics Guidefor details.
还可以进行一些基本的传输控制:暂停、停止、检查声音是否结束等。有关详细信息,请参阅声音编程主题指南。
*It's possible to use other formats, too, of course.
*当然也可以使用其他格式。
回答by buzzkip
For example, add the AudioToolbox framework, the add the sound file to your resources / project, then add the below to your .h:
例如,添加 AudioToolbox 框架,将声音文件添加到您的资源/项目,然后将以下内容添加到您的 .h:
#import <AudioToolbox/AudioToolbox.h>
And then: (after @interface { blah } in your .h)
然后:(在你的 .h 中的 @interface { blah } 之后)
- (IBAction) playSound;
Add the following to your .m: (change 'Ping' and 'wav' to the respective file name and type)
将以下内容添加到您的 .m 中:(将“Ping”和“wav”更改为相应的文件名和类型)
- (IBAction) playSound
{
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Ping" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}
回答by Todd Hopkinson
You can copy and add your .aif files to your project in Xcode as follows:
您可以将 .aif 文件复制并添加到 Xcode 中的项目中,如下所示:
- Command-click on the Classes folder (from within xCode)
- Select Add > Existing Files
- Navigate to the directory containing your .aif and select it(them). Click Add. (optional: Check the “Copy items” checkbox & Click Add.)
- 按住 Command 键单击 Classes 文件夹(从 xCode 中)
- 选择添加 > 现有文件
- 导航到包含您的 .aif 的目录并选择它(它们)。单击添加。(可选:选中“复制项目”复选框并单击“添加”。)