ios - iPhone上的系统声音无法播放

标签 ios system audio

我想播放系统声音,但听不到任何声音:

- (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");
}

我什么也没听到。我究竟做错了什么?

最佳答案

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);
}

关于ios - iPhone上的系统声音无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960757/

相关文章:

ios - 如何使用 RxAlamofire 在帖子正文中传递 json 进行帖子调用

ios - userNotificationCenter didReceive 响应包含空通知(watchOS)

c - 如何在 C 中使用读写过去的 BUFSIZ

java - 使用 System() 从 Perl 调用 Java .jar 程序

audio - ALSA Hooks——在播放时修改音频

actionscript-3 - ActionScript 3 setInterval在10之后停止。(音频)

ios - 有没有一种巧妙的方法可以将完成 block 附加到 Swift 中的 NSURLSessionDataDelegate 回调?

ios - 如何在 Xcode 4.3 中成功使用 BOOL

c - 如何用c语言运行一个可执行文件?

c++ - 处理使用ffmpeg解码的音频包