用xcode播放声音

时间:2014-07-14 21:03:10

标签: ios objective-c audio

我寻找答案,但我找不到我想要的答案。我想在我的iphone项目上播放声音。我可以使用哪些文件类型?怎么样的代码?我对此一无所知,我需要你的帮助?谢谢你的到来......

1 个答案:

答案 0 :(得分:1)

首先在ViewController.h中#import <AudioToolbox/AudioToolbox.h>

然后在你的ViewController.m中:

@property SystemSoundID theSound;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"Sound"
                                                    withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(soundURL), &_theSound);
}

- (IBAction)playSound
{
    AudioServicesPlaySystemSound(_theSound);
}

- (void)dealloc
{
    AudioServicesDisposeSystemSoundID(_theSound);
}