简单的方法来播放声音文件(.AIF) [英] Simple way to play a sound file (.aif)

查看:1015
本文介绍了简单的方法来播放声音文件(.AIF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用X code将某个应用为Mac OS X的,我希望它有事时起到提示音。

I'm making an app for Mac OS X using Xcode, and I want it to play a alert sound when something happens.

什么是最简单的code代表在Objective-C /可可播放声音?请注意,我的声音文件是 .AIF

What is the simplest code for playing a sound in Objective-C/Cocoa? Note that my sound file is an .aif.

另外,我在哪里把声音文件我的项目里面?

Also, where do I put the sound file inside my project?

感谢您。

推荐答案

要在Mac上播放音频,您可以使用<一个href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html\"><$c$c>NSSound为基本活动,或更健壮的<一href=\"http://developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/CoreAudioOverview/Introduction/Introduction.html\"><$c$c>CoreAudio框架更复杂的任务。

To play audio on the Mac, you can either use NSSound for basic activities, or the more robust CoreAudio framework for more complicated tasks.

如果你使用 NSSound 来实现这一目标,您的code会是这个样子:

If you were to use NSSound to achieve this goal, your code would look something like this:

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 是名单上。

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.

使用 CoreAudio的是比较复杂的,所以如果你只是想用非常简单的设置围绕发挥它和食堂,只需要使用 NSSound

Using CoreAudio is more complicated, so if you just want to play it and mess around with very simple settings, just use NSSound

这篇关于简单的方法来播放声音文件(.AIF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆