我AVFoundation
的AudioToolbox
项目中添加了框架。在我想播放系统声音的班级中,我#include <AudioToolbox/AudioToolbox.h>
和我打电话给AudioServicesPlaySystemSound(1007);
. 我正在运行的设备中进行测试iOS 8
,声音已打开并且音量足够高,但是当我运行应用程序并被AudioServicesPlaySystemSound(1007);
调用时我没有听到任何系统声音......我可能会错过什么?
问问题
9244 次
5 回答
7
使用iOS10 播放这样的音频不起作用:
SystemSoundID audioID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID);
AudioServicesPlaySystemSound(audioID);
改用这个:
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID);
AudioServicesPlaySystemSoundWithCompletion(audioID, ^{
AudioServicesDisposeSystemSoundID(audioID);
});
于 2016-09-23T13:14:20.550 回答
5
根据文档:
此函数 (
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 系统偏好设置中静音,请确保选中“播放用户界面音效”)
于 2016-08-16T08:25:57.877 回答
2
这将播放系统声音。
但请记住系统声音不会播放更长的声音。
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
于 2015-09-10T09:33:19.123 回答
1
我刚刚在运行 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);
于 2017-01-05T08:48:57.587 回答
1
对于 swift 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)
})
}
于 2017-02-04T12:24:46.880 回答