如何播放声音文件“OnTouch”

时间:2012-04-18 14:33:13

标签: ios audio

触摸屏幕时我需要播放文件。

我尝试了以下方法,但它无效:

-(IBAction)play;
{
    CFBundleRef mainBundle = CFBundleGetMainBundle();

    CFURLRef soundFileURLRef;

    soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
    (CFStringRef) @"sound1", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

1 个答案:

答案 0 :(得分:1)

您是否在窗口或主视图上添加了触控处理程序?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [yourObject play];
}