本文介绍了使用Swift将声音文件推送到阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的Iphone键盘扩展,单击一个字符时键盘会播放声音。我想将我的buttonPresssed函数的声音随机化。到目前为止,我只能对该功能使用一种声音,并且可以使spacePressed函数播放另一种声音。本质上,我希望buttonPressed函数可以播放数组中的随机声音。谢谢进阶...

I have an Iphone keyboard extension that I am working on and the keyboard plays sounds when a character is clicked. I would like to randomize the sounds for my buttonPresssed function. So far I am only able to have one sound for that function and was able to get the spacePressed function to play another. Essentialy I would like the buttonPressed function to play random sounds from an array. Thank you in advanced...

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.


    }
}


推荐答案

我建议像这样将声音添加到数组

I would recommend adding the sounds to an array like so:

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

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

Then create a random sound generator:

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

这篇关于使用Swift将声音文件推送到阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:20