AudioServicesPlaySystemSound
我有这种不稳定的行为。
出于某种原因,通过我的(真实设备)iPhone 5/6一切都运行良好。但有时候我的(真实设备)iPhone 6 + S,我听到 - 有时候 - 只有3或2声音5我正在玩
我用来创建这个声音的功能:
if let soundURL = NSBundle.mainBundle().URLForResource(soundName, withExtension: "mp3") {
var mySound: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL, &mySound)
AudioServicesPlaySystemSound(mySound);
}
答案 0 :(得分:1)
你应该在代码中做一些调试并找出问题的真正原因,下面的代码将打印出OSStatus:
let result = AudioServicesCreateSystemSoundID(soundURL, &mySound)
print(result)
当结果不为0时你需要弄清楚什么是错的,最常见的问题是声音文件应该少于30秒,否则它将无法通过系统声音服务播放。
另一个问题是你应该在应用程序的开头创建系统声音,然后在需要时播放返回的mySound,每次播放时都不要调用AudioServicesCreateSystemSoundID。
import UIKit
import AudioToolbox
class ViewController: UIViewController {
var mySound: SystemSoundID = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let soundName = "mytest"
if let soundURL = NSBundle.mainBundle().URLForResource(soundName, withExtension: "mp3") {
let result = AudioServicesCreateSystemSoundID(soundURL, &mySound)
print(mySound)
print(result)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func play (sender: UIButton) {
AudioServicesPlaySystemSound(mySound);
}
}