快速的音频播放速度
Posted
技术标签:
【中文标题】快速的音频播放速度【英文标题】:Swift Audio Playback speed 【发布时间】:2017-06-04 03:54:41 【问题描述】:我正在开发一个游戏,其中主要的精灵是一个旋转的***。我想将音频与随着车轮减速而改变音调的旋转车轮相关联。当车轮停止旋转时,我希望音效停止。有人能指出我正确的方向吗?
在我的游戏场景中,我有以下代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
for touch in touches
let location = touch.location(in: self)
//Start spinning the wheel
if atPoint(location).name == "play_button"
player?.physicsBody?.angularVelocity = 0
player?.rotatePlayer()
//Click back button
else if atPoint(location).name == "back_button"
let play_scene = CharacterSelectScene(fileNamed: "CharacterSelect")
play_scene?.scaleMode = .aspectFill
self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
else if atPoint(location).name == "settings_button"
let play_scene = SettingsScene(fileNamed: "SettingsScene")
play_scene?.scaleMode = .aspectFill
self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
override func didSimulatePhysics()
let speed = player?.physicsBody?.angularVelocity
if (speed != CGFloat(0.0))
if (speed! <= CGFloat(0.1))
finishedRotation = true
override func didFinishUpdate()
if (finishedRotation == true)
let play_scene = QuestionScene(fileNamed: "QuestionScene")
play_scene?.scaleMode = .aspectFill
self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
func didBegin(_ contact: SKPhysicsContact)
let defaults = UserDefaults.standard
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if (contact.bodyA.node?.name == "pin")
firstBody = contact.bodyA
secondBody = contact.bodyB
else
firstBody = contact.bodyB
secondBody = contact.bodyA
if (firstBody.node?.name == "pin" && secondBody.node?.name == "geography")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "maths")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "history")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "science")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
Player 类中的 rotatePlayer 方法定义如下:
func rotatePlayer()
let random = GKRandomDistribution(lowestValue: 20, highestValue: 120)
let r = random.nextInt()
self.physicsBody?.angularVelocity = 0
self.physicsBody?.angularVelocity = CGFloat(r)
self.physicsBody?.angularDamping = 1.02
【问题讨论】:
关于 AVAudioPlayer 的 rate 属性,Xcode 文档告诉你什么? 除了默认费率和启用费率之外,文档没有说明太多。如何将速率与纺车的速度联系起来以使其听起来逼真? 您的问题是因为一些原因而违反了 *** 的规则。 1)缺乏信息(例如,我们不知道你的***是如何工作的,所以我们不能确定如何用旋转速度映射速率/俯仰),2)到目前为止你尝试了什么?热心帮助的人可以提供帮助,但没有人愿意编写完整的解决方案......好吧,它发生了,但仍然,这不是这个网站的工作方式;)请参阅ask a good question 关于球场:developer.apple.com/reference/avfoundation/avaudiounittimepitch 但这可能不是你想要的。正如我所说,您必须提供更多信息。理想情况下,您提供可以重现问题的 MVP。 感谢旋风。我会尽快提供更多细节 【参考方案1】:我已经解决了这个问题,代码如下:
func playSound()
let url = Bundle.main.url(forResource: "clicker", withExtension: "m4a")!
do
SoundEffect = try AVAudioPlayer(contentsOf: url)
guard let players = SoundEffect else return
players.prepareToPlay()
players.play()
catch let error as NSError
print(error.description)
func didEnd(_ contact: SKPhysicsContact)
let defaults = UserDefaults.standard
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if (contact.bodyA.node?.name == "pin")
firstBody = contact.bodyA
secondBody = contact.bodyB
else
firstBody = contact.bodyB
secondBody = contact.bodyA
if (firstBody.node?.name == "pin" && secondBody.node?.name == "geography")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
playSound()
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "maths")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
playSound()
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "history")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
playSound()
else if (firstBody.node?.name == "pin" && secondBody.node?.name == "science")
let set_topic = secondBody.node?.name
defaults.set(set_topic, forKey: "Topic")
playSound()
【讨论】:
然而,这会导致严重的延迟并冻结游戏。在设备上和模拟器中。 不确定是什么导致了延迟。当您真的只想要 1 次时,也许您会收到多次didEnd
的调用。您可以尝试设置一个自定义标志以跳过 didEnd
直到它关闭一次后的下一帧...看看这是否有效。以上是关于快速的音频播放速度的主要内容,如果未能解决你的问题,请参考以下文章