使用Swift将声音文件推送到数组

时间:2015-11-17 16:39:32

标签: arrays swift

我正在使用Iphone键盘扩展,当单击一个角色时键盘会播放声音。我想将buttonPresssed函数的声音随机化。到目前为止,我只能为该功能发出一个声音,并且能够让spacePressed函数播放另一个。 Essentialy我想使用buttonPressed函数播放数组中的随机声音。先谢谢你...

import UIKit
import AVFoundation

class KeyboardViewController: UIInputViewController {

    var keyboardView: UIView!

    //sounds
    var sound1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound1", ofType: "mp3")!)

    var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound2", ofType: "mp3")!)

    var sound3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound3", ofType: "mp3")!)


    var audioPlayer = AVAudioPlayer()

    @IBOutlet var nextKeyboardButton: UIButton!

    override func updateViewConstraints() {
        super.updateViewConstraints()

        // Add custom view sizing constraints here
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadInterface()

        //prepare sounds
        //audioPlayer.prepareToPlay()
    }

    func loadInterface(){

        let keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)

        self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView

        view.addSubview(self.keyboardView)

        view.backgroundColor = self.keyboardView.backgroundColor

        self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)

    }

    @IBAction func buttonPressed (sender: UIButton){
       let title = sender.titleForState(.Normal)

       let proxy = textDocumentProxy as UITextDocumentProxy

       proxy.insertText(title!)
       audioPlayer = try! AVAudioPlayer(contentsOfURL: sound2)

        //play sound
        audioPlayer.play()
    }

    @IBAction func spacePressed (sender: UIButton){
        let proxy = textDocumentProxy as UITextDocumentProxy

        proxy.insertText(" ")

        audioPlayer = try! AVAudioPlayer(contentsOfURL: sound3)
        audioPlayer.play()

    }

    @IBAction func deletePressed (sender: UIButton){

        let proxy = textDocumentProxy as UITextDocumentProxy

        proxy.deleteBackward()

    }

    @IBAction func capsLockPressed (sender: UIButton){


    }

    @IBAction func returnKeyPressed (sender: UIButton){


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated
    }

    override func textWillChange(textInput: UITextInput?) {
        // The app is about to change the document's contents. Perform any preparation here.
    }

    override func textDidChange(textInput: UITextInput?) {
        // The app has just changed the document's contents, the document context has been updated.


    }
}

1 个答案:

答案 0 :(得分:2)

我建议将声音添加到array,如下所示:

var sound1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound1", ofType: "mp3")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound2", ofType: "mp3")!)
var sound3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound3", ofType: "mp3")!)

var soundArray : [NSURL] = [sound1, sound2, sound3]
var audioPlayer = AVAudioPlayer()

然后创建一个随机声音生成器:

func playRandomSound() {
    let randNo = Int(arc4random_uniform(UInt32(soundArray!.count))) // 0...ArrayCount

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
        try audioPlayer = AVAudioPlayer(contentsOfURL: soundArray![randNo])
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        print(error)
    }
}