2015-09-10 153 views
4

我已将AVFoundationAudioToolbox框架添加到我的项目中。在我想要播放系统声音的课程中,我和#include <AudioToolbox/AudioToolbox.h>AudioServicesPlaySystemSound(1007);。我在运行iOS 8的设备上进行测试,声音已打开且音量足够高,但我在运行应用程序时听不到任何系统声音,并且调用了AudioServicesPlaySystemSound(1007); ...我可能会丢失什么?AudioServicesPlaySystemSound不能在iOS 8设备中播放任何声音

+2

你是否检查了无声开关? –

+0

@JasonNam是的,声音在... – AppsDev

+0

其他音频听起来不错吗? –

回答

2

这将播放系统声音。

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

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

你可以给出一些建议,如何发挥更长的声音或重复音?我必须播放重复的声音,直到通话未被选中。 – Aditya

3

根据文档:

此功能(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); 
     }); 
    } 
} 

此外,完成块确保其设置的前播放声音完成。

如果这不能解决问题,也许你的问题不是代码相关的,而是相关的设置(静音/模拟器的设备声音从MAC系统偏好静音,确保“播放用户界面声音效果”被选中)

+0

你能链接到文档吗?我找不到你的报价。 – Suragch

+1

这是来自SDK头文件。尝试使用已弃用的方法,XCode会显示警告消息。 –

6

随着iOS10这样播放音频不起作用

SystemSoundID audioID; 

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

使用这个代替:

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

AudioServicesPlaySystemSoundWithCompletion(audioID, ^{ 
    AudioServicesDisposeSystemSoundID(audioID); 
}); 
0

我刚刚测试了运行iOS 8的iPad和iPhone上的代码,并且它正在使用真实设备。

对于一些非常奇怪的原因,它不适用于任何设备的iOS 8模拟器,即使它适用于iOS 7和7.1模拟器。

否则下面的代码在所有实际设备中都可以正常工作。

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

的迅速3.x和Xcode中8:

var theSoundID : SystemSoundID = 0 
let bundleURL = Bundle.main.bundleURL 
let url = bundleURL.appendingPathComponent("Invitation.aiff") 

let urlRef = url as CFURL 

let err = AudioServicesCreateSystemSoundID(urlRef, &theSoundID) 
if err == kAudioServicesNoError{ 
    AudioServicesPlaySystemSoundWithCompletion(theSoundID, { 
     AudioServicesDisposeSystemSoundID(theSoundID) 
    }) 
} 
相关问题