AudioServicesPlaySystemSound在iOS 8设备中没有播放任何声音

App*_*Dev 5 audio system-sounds avfoundation audiotoolbox ios

我已经AVFoundationAudioToolbox框架添加到我的项目中.在我想要播放系统声音的课堂上,我#include <AudioToolbox/AudioToolbox.h>和我打电话AudioServicesPlaySystemSound(1007);.我正在测试设备运行iOS 8,声音是否打开,音量是否足够高,但是当我运行应用程序时我听不到任何系统声音,并且AudioServicesPlaySystemSound(1007);被称为......我可能缺少什么?

Ame*_*ikh 6

根据文档:

AudioServicesPlaySystemSound()在以后的版本中将不推荐使用此功能()。请改用AudioServicesPlaySystemSoundWithCompletion。

使用以下代码片段播放声音:

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; //filename can include extension e.g. @"bang.wav"
if (fileURL)
{
    SystemSoundID theSoundID;
    OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
    if (error == kAudioServicesNoError)
    { 
        AudioServicesPlaySystemSoundWithCompletion(theSoundID, ^{
            AudioServicesDisposeSystemSoundID(theSoundID);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

而且,完成块确保声音播放在被处置之前已经完成。

如果这不能解决问题,则可能您的问题与代码无关,而与设置有关(从“ MAC系统偏好设置”中使静音/模拟器上的设备声音静音,请确保选中了“播放用户界面声音效果”)

  • 这来自SDK头文件。尝试使用不建议使用的方法,XCode将显示警告消息。 (2认同)

Tia*_*ida 6

使用iOS10播放这样的音频不起作用:

SystemSoundID audioID;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID);
AudioServicesPlaySystemSound(audioID);
Run Code Online (Sandbox Code Playgroud)

请改用:

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID);

AudioServicesPlaySystemSoundWithCompletion(audioID, ^{
    AudioServicesDisposeSystemSoundID(audioID);
});
Run Code Online (Sandbox Code Playgroud)


Shu*_*ang 2

这将播放系统声音。

但请记住系统声音不会播放更长的声音。

NSString *pewPewPath  = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
Run Code Online (Sandbox Code Playgroud)