gpt4 book ai didi

ios - AudioServicesPlaySystemSound 奇怪的行为

转载 作者:行者123 更新时间:2023-11-29 01:21:57 24 4
gpt4 key购买 nike

我的 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);

}

最佳答案

你应该在代码中做一些调试并找出问题的真正原因,下面的代码将打印出 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);
}
}

关于ios - AudioServicesPlaySystemSound 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com