xcode 需要帮助快速将音频文件添加到按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26579890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:00:42  来源:igfitidea点击:

Need help adding an audio file to a button in swift

xcodeswiftxcode6

提问by Giovanni Portogallo

can anyone help me get this to work I am a noob programer and I could not find a way to do this.

谁能帮我让它工作我是一个菜鸟程序员,我找不到办法做到这一点。

Code below

下面的代码

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()

    }
}

回答by Steve Rosenberg

There is a lot of pretty good code on Stack Overflow:

Stack Overflow 上有很多不错的代码:

Playing a sound with AVAudioPlayer

使用 AVAudioPlayer 播放声音

Here is some example code:

下面是一些示例代码:

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()
    }
}

from this location:

从这个位置:

http://www.reddit.com/r/swift/comments/28izxl/how_do_you_play_a_sound_in_ios/

http://www.reddit.com/r/swift/comments/28izxl/how_do_you_play_a_sound_in_ios/

回答by Leo Dabus

First drag your sound to your project and choose to copy for your destination if needed and check "add to target" to your app. Create a function to play your sound and you need also to add import AVFoundation at the beginning of your code as bellow:

首先将您的声音拖到您的项目中,并根据需要选择复制到您的目的地,然后选中“添加到目标”到您的应用程序。创建一个函数来播放你的声音,你还需要在代码的开头添加 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()
}

回答by MiladiuM

Swift 3

斯威夫特 3

the syntax is now as follows:

语法如下:

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()
    }
}