所以我想要做的是快速创建并播放声音,当我按下按钮时会播放,我知道如何在Objective-C中执行此操作,但有人知道如何在Swift中播放吗?
对于Objective-C,它会是这样的:
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mysoundname" ofType:@"wav"]]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &mySound);
然后玩它我会做:
AudioServicesPlaySystemSound(Explosion);
有谁知道我怎么做到这一点?
这与其他一些答案类似,但也许更多"Swifty":
// Load "mysoundname.wav" if let soundURL = Bundle.main.url(forResource: "mysoundname", withExtension: "wav") { var mySound: SystemSoundID = 0 AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound) // Play AudioServicesPlaySystemSound(mySound); }
请注意,这是重现问题中代码效果的一个简单示例.你需要确保import AudioToolbox
,加上这种代码的一般模式是在你的应用程序启动时加载你的声音,将它们保存在SystemSoundID
某个地方的实例变量中,在整个应用程序中使用它们,然后AudioServicesDisposeSystemSoundID
在你完成时调用跟他们.
以下是我添加到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() } }
这段代码适合我.使用Try和Catch for AVAudioPlayer
import UIKit import AVFoundation class ViewController: UIViewController { //Make sure that sound file is present in your Project. var CatSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Meow-sounds.mp3", ofType: "mp3")!) var audioPlayer = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() do { audioPlayer = try AVAudioPlayer(contentsOfURL: CatSound) audioPlayer.prepareToPlay() } catch { print("Problem in getting File") } // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func button1Action(sender: AnyObject) { audioPlayer.play() } }
使用class&AudioToolbox:
import AudioToolbox class Sound { var soundEffect: SystemSoundID = 0 init(name: String, type: String) { let path = NSBundle.mainBundle().pathForResource(name, ofType: type)! let pathURL = NSURL(fileURLWithPath: path) AudioServicesCreateSystemSoundID(pathURL as CFURLRef, &soundEffect) } func play() { AudioServicesPlaySystemSound(soundEffect) } }
用法:
testSound = Sound(name: "test", type: "caf") testSound.play()
这SystemSoundID
将从一个名为的文件创建一个Cha-Ching.aiff
.
import AudioToolbox let chaChingSound: SystemSoundID = createChaChingSound() class CashRegisterViewController: UIViewController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) AudioServicesPlaySystemSound(chaChingSound) } } func createChaChingSound() -> SystemSoundID { var soundID: SystemSoundID = 0 let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "Cha-Ching", "aiff", nil) AudioServicesCreateSystemSoundID(soundURL, &soundID) CFRelease(soundURL) return soundID }
import AVFoundation var audioPlayer = AVAudioPlayer() class GameScene: SKScene { override func didMoveToView(view: SKView) { let soundURL = NSBundle.mainBundle().URLForResource("04", withExtension: "mp3") audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil) audioPlayer.play() } }
Handy Swift扩展:
import AudioToolbox extension SystemSoundID { static func playFileNamed(fileName: String, withExtenstion fileExtension: String) { var sound: SystemSoundID = 0 if let soundURL = NSBundle.mainBundle().URLForResource(fileName, withExtension: fileExtension) { AudioServicesCreateSystemSoundID(soundURL, &sound) AudioServicesPlaySystemSound(sound) } } }
然后,从你的应用程序的任何地方(记得import AudioToolbox
),你可以打电话
SystemSoundID.playFileNamed("sound", withExtenstion: "mp3")
玩"sound.mp3"