Xcode 需要帮助将音频文件添加到swift中的按钮吗

Xcode 需要帮助将音频文件添加到swift中的按钮吗,xcode,swift,xcode6,Xcode,Swift,Xcode6,有人能帮我把这个工作,我是一个noob程序,我找不到一个方法来做这件事 代码如下 import UIKit class ViewController: UIViewController { @IBAction func pistolButton(sender: AnyObject) { } override func viewDidLoad() { // I want my audio file to play when button is tapped

有人能帮我把这个工作,我是一个noob程序,我找不到一个方法来做这件事

代码如下

import UIKit

class ViewController: UIViewController {

    @IBAction func pistolButton(sender: AnyObject) {

    }

    override func viewDidLoad() { // I want my audio file to play when button is tapped
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

关于堆栈溢出,有很多非常好的代码:

下面是一些示例代码:

import AVFoundation
var audioPlayer: AVAudioPlayer?

if let path = NSBundle.mainBundle().pathForResource("mysound", ofType: "aiff") {
    audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), fileTypeHint: "aiff", error: nil)

    if let sound = audioPlayer {
        sound.prepareToPlay()
        sound.play()
    }
}
从这个位置:


关于堆栈溢出,有很多非常好的代码:

下面是一些示例代码:

import AVFoundation
var audioPlayer: AVAudioPlayer?

if let path = NSBundle.mainBundle().pathForResource("mysound", ofType: "aiff") {
    audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), fileTypeHint: "aiff", error: nil)

    if let sound = audioPlayer {
        sound.prepareToPlay()
        sound.play()
    }
}
从这个位置:


首先将您的声音拖动到项目中,如果需要,选择为您的目的地进行复制,并选中应用程序中的“添加到目标”。创建一个播放声音的函数,您还需要在代码开头添加import AVFoundation,如下所示:

import AVFoundation

let beepSoundURL =  NSBundle.mainBundle().URLForResource("beep", withExtension: "aif")!
var beepPlayer = AVAudioPlayer()
func playMySound(){
   beepPlayer = AVAudioPlayer(contentsOfURL: beepSoundURL, error: nil)
   beepPlayer.prepareToPlay()
   beepPlayer.play()
}

@IBAction func pistolButton(sender: AnyObject) {
   playMySound()
}

首先将声音拖到项目中,如果需要,选择复制到目的地,并选中应用程序中的“添加到目标”。创建一个播放声音的函数,您还需要在代码开头添加import AVFoundation,如下所示:

import AVFoundation

let beepSoundURL =  NSBundle.mainBundle().URLForResource("beep", withExtension: "aif")!
var beepPlayer = AVAudioPlayer()
func playMySound(){
   beepPlayer = AVAudioPlayer(contentsOfURL: beepSoundURL, error: nil)
   beepPlayer.prepareToPlay()
   beepPlayer.play()
}

@IBAction func pistolButton(sender: AnyObject) {
   playMySound()
}

Swift 3

现在的语法如下所示:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()
        // address of the music file
        let music = Bundle.main.path(forResource: "Music", ofType: "mp3")

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
        }
        catch{
            print(error)
        }   
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func play(_ sender: AnyObject) {
        audioPlayer.play()   
    }
    @IBAction func stop(_ sender: AnyObject) {
        audioPlayer.stop()
    }
}

Swift 3

现在的语法如下所示:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()
        // address of the music file
        let music = Bundle.main.path(forResource: "Music", ofType: "mp3")

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
        }
        catch{
            print(error)
        }   
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func play(_ sender: AnyObject) {
        audioPlayer.play()   
    }
    @IBAction func stop(_ sender: AnyObject) {
        audioPlayer.stop()
    }
}

请编辑您的代码,使其具有某种意义,过多的空白和空函数并不是一个好问题。请参阅帮助中心,以获取关于提出好问题的指导。请编辑您的代码,使其具有某种意义,过多的空白和空函数并不是一个好问题。请参阅帮助中心,以获取关于提出好问题的指导。