在我的Xcode应用程序中单击按钮后,如何播放自定义声音?

时间:2011-12-07 15:30:22

标签: objective-c xcode delay

我正在使用Xcode创建Mac OSX, Objective-C, cocoa app.我想让我的应用在一段时间后播放自定义声音。

这就是我的代码:

- (IBAction)clickToPlayCustomSoundButtonClicked {

[self performSelector:@selector(playCustomSound) withObject:nil afterDelay:1.0];

}

- (void)playCustomSound {

***This is where I want to put the code to play the custom sound***

}

我应该在(void)playCustomSound方法中使用哪些代码来播放自定义声音?

请帮助并谢谢你们:D

1 个答案:

答案 0 :(得分:0)

试试这个(假设你的声音文件在你的Ressources文件夹中):

NSSound *player = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MyCustomSound" ofType:@"mp3"] byReference:NO];
[player play];