1

我想播放系统声音,但我什么也没听到:

- (void) play_system_sound
{
    NSLog(@"Play system sound");

    SystemSoundID soundID;
    NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
    NSLog(@"path = %@", path );
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    AudioServicesPlaySystemSound(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
    NSLog(@"Play system sound DONE");
}

我什么也听不见。我究竟做错了什么?

4

2 回答 2

5

之后AudioServicesPlaySystemSound(soundID),不要AudioServicesDisposeSystemSoundID(soundID)立即打电话。

尝试这个 :

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
}

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

- (void)play_system_sound
{
    AudioServicesPlaySystemSound(soundID);
}
于 2012-08-15T08:23:40.327 回答
1

我不知道你是否解决了这个问题,但对我来说,在 iOS 7 上,有时(__bridge CFURLRef)强制转换不起作用,在这种情况下,如果你打印(__bridge CFURLRef)[NSURL fileURLWithPath:path]0x0. 这导致AudioServicesCreateSystemSoundID(). 函数的返回值是类型OSStatus。如果失败,该函数将返回-50,这意味着一个或多个参数无效。(如果执行成功,则返回0。)

我通过使用 C 样式函数获取文件的路径解决了这个问题:

    CFBundleRef mainBundle = CFBundleGetMainBundle ();
    CFURLRef soundFileUrl;

    soundFileUrl = CFBundleCopyResourceURL(mainBundle, CFSTR("fileName"), CFSTR("wav"), NULL);
    AudioServicesCreateSystemSoundID(soundFileUrl, &soundID);
于 2013-12-19T03:40:19.673 回答