我想播放系统声音,但我没有听到任何声音:
- (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");
}
Run Code Online (Sandbox Code Playgroud)
我什么都没听到.我究竟做错了什么?
之后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);
}
Run Code Online (Sandbox Code Playgroud)