我已将AVFoundation
和AudioToolbox
框架添加到我的项目中。在我想要播放系统声音的课程中,我和#include <AudioToolbox/AudioToolbox.h>
和AudioServicesPlaySystemSound(1007);
。我在运行iOS 8
的设备上进行测试,声音已打开且音量足够高,但我在运行应用程序时听不到任何系统声音,并且调用了AudioServicesPlaySystemSound(1007);
...我可能会丢失什么?AudioServicesPlaySystemSound不能在iOS 8设备中播放任何声音
回答
这将播放系统声音。
但记住系统声音不会播放更长的声音。
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
你可以给出一些建议,如何发挥更长的声音或重复音?我必须播放重复的声音,直到通话未被选中。 – Aditya
根据文档:
此功能(
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系统偏好静音,确保“播放用户界面声音效果”被选中)
你能链接到文档吗?我找不到你的报价。 – Suragch
这是来自SDK头文件。尝试使用已弃用的方法,XCode会显示警告消息。 –
随着iOS10这样播放音频不起作用:
SystemSoundID audioID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID);
AudioServicesPlaySystemSound(audioID);
使用这个代替:
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID);
AudioServicesPlaySystemSoundWithCompletion(audioID, ^{
AudioServicesDisposeSystemSoundID(audioID);
});
我刚刚测试了运行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);
的迅速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)
})
}
- 1. AudioServicesPlaySystemSound不播放声音
- 2. iPhone - 声音不能播放AudioServicesPlaySystemSound和AVAudioPlayer
- 3. 用AudioServicesPlaySystemSound一次播放1个声音
- 4. 在特定设备中播放声音
- 5. 可以阻止iOS AudioServicesPlaySystemSound直到完成播放声音?
- 6. 设备上不播放声音[Titanium]
- 7. AVAudioPlayer不播放声音IOS
- 8. 在Flex(Flash)中播放声音并听不到任何声音
- 9. mediaplayer未知不能在某个设备上播放声音
- 10. UILocalNotification声音不能在设备上播放
- 11. iOS有可能在设备静音时播放本地通知声音
- 12. 在IOS上播放声音
- 13. 如何在iOS中播放声音?
- 14. 如何选择声音设备播放声音?
- 15. 不管设备音量如何,播放声音
- 16. 在iOS设备上同时播放声音
- 17. AVQueuePlayer不能播放声音
- 18. 无法在Windows 8中播放声音
- 19. 声音在模拟器中播放,但不在设备上
- 20. 声音在模拟器中播放但不在设备上
- 21. UILocalNotifications在模拟器中播放声音,但不在设备上
- 22. 不能在Java中播放声音
- 23. 声音不能在Titanium中播放
- 24. AVAudioPlayer不播放任何声音
- 25. .wav文件不播放任何声音
- 26. 声音没有在iOS设备上播放,但在iOS模拟器中工作
- 27. 如何在所有音频设备上播放声音
- 28. 我的.aif短声音文件只能在iOS模拟器上播放,而不是在设备上播放?
- 29. avplayer不能在IOS中播放音频
- 30. Ionic/Cordova应用程序在模拟器中播放声音,但不在Android设备上播放声音
你是否检查了无声开关? –
@JasonNam是的,声音在... – AppsDev
其他音频听起来不错吗? –