[英]system sound on iphone not playing
I want to play a system sound but I don't hear anything: 我想播放系统声音,但听不到任何声音:
- (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");
}
I don't hear anything. 我什么也没听到。 What am I doing wrong?
我究竟做错了什么?
After AudioServicesPlaySystemSound(soundID)
,don't call AudioServicesDisposeSystemSoundID(soundID)
immediately. 在
AudioServicesPlaySystemSound(soundID)
,请勿立即调用AudioServicesDisposeSystemSoundID(soundID)
。
Try this : 尝试这个 :
- (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);
}
I don't know if you have solved the problem, but for me, on iOS 7, sometimes the (__bridge CFURLRef)
casting won't work, in which case, if you print out the result of (__bridge CFURLRef)[NSURL fileURLWithPath:path]
, it will be 0x0
. 我不知道您是否已解决问题,但对我而言,在iOS 7上,有时
(__bridge CFURLRef)
强制转换将不起作用,在这种情况下,如果您打印(__bridge CFURLRef)[NSURL fileURLWithPath:path]
,它将为0x0
。 This leads to failure of AudioServicesCreateSystemSoundID()
. 这导致
AudioServicesCreateSystemSoundID()
失败。 The return value of the function is type of OSStatus
. 该函数的返回值是
OSStatus
类型。 If it fails, the function will return -50
, which means one or more parameters are invalid. 如果失败,函数将返回
-50
,这意味着一个或多个参数无效。 (If it executes successfully, it returns 0
.) (如果执行成功,则返回
0
)
I solved this by using the C style function getting the path of the file: 我通过使用C样式函数获取文件的路径来解决此问题:
CFBundleRef mainBundle = CFBundleGetMainBundle ();
CFURLRef soundFileUrl;
soundFileUrl = CFBundleCopyResourceURL(mainBundle, CFSTR("fileName"), CFSTR("wav"), NULL);
AudioServicesCreateSystemSoundID(soundFileUrl, &soundID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.