所以我想做的就是快速创建并播放声音,当我按下按钮时会播放声音,我知道如何在Objective-C中做到这一点,但是有人知道如何在Swift中做到吗?
对于Objective-C来说是这样的:
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mysoundname" ofType:@"wav"]]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &mySound);
然后播放它,我会做:
AudioServicesPlaySystemSound(Explosion);
有人知道我该怎么做吗?
这是我添加到FlappySwift的一些有效的代码:
import SpriteKit import AVFoundation class GameScene: SKScene { // Grab the path, make sure to add it to your project! var coinSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "coin", ofType: "wav")!) var audioPlayer = AVAudioPlayer() // Initial setup override func didMoveToView(view: SKView) { audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil) audioPlayer.prepareToPlay() } // Trigger the sound effect when the player grabs the coin func didBeginContact(contact: SKPhysicsContact!) { audioPlayer.play() } }