2016-06-23 12 views
0

画面がテープされるたびにサウンドを再生するにはどうすればよいですか?これは、画面がタッチされた場合、私がチェックし、私が今持っているコード、であり、それはいくつかのことを...よりもSWIFT画面がタッチされたときにサウンドを再生する方法

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    if TapsValid == true { 
     Score++ 

     if BallRight == true { 

      BallChange = false 

     } else { 

      BallChange = true 

     } 

    } 
} 
+2

[作成と迅速にサウンドを再生]の可能重複します(http:/ /stackoverflow.com/questions/24043904/creating-and-playing-a-sound-in-swift) – Grimxn

答えて

0
import AVFoundation 

/// You **must** keep reference to this... 
private var audioPlayer: AVAudioPlayer! 

/** 
    Tap 
*/ 
public func tapSound() -> Void 
{ 
    let bundle: NSBundle = NSBundle(forClass: SoundMachine.self) 
    if let file_url: NSURL = bundle.URLForResource("tap_sounf", withExtension:"caf") 
    { 
     self.playSoundFromURL(file_url) 
    } 
} 

/** 
    Reproduce un archivo de sonido que se encuentra 
    en el Bundle de nuestra app 

    - parameter resource_url: La URL al archivo 
*/ 
private func playSoundFromURL(resource_url: NSURL) -> Void 
{ 
    self.audioPlayer = try! AVAudioPlayer(contentsOfURL:resource_url) 
    self.audioPlayer.prepareToPlay() 
    self.audioPlayer.play() 
} 
関連する問題