【问题标题】:Pushing Sound files to an Array with Swift使用 Swift 将声音文件推送到数组
【发布时间】:2015-11-18 03:16:01
【问题描述】:

我正在开发一个 Iphone 键盘扩展程序,当点击一个字符时键盘会播放声音。我想为我的 buttonPressed 函数随机化声音。到目前为止,我只能为该功能发出一种声音,并且能够让 spacePressed 功能播放另一种声音。本质上,我希望 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.


    }
}

【问题讨论】:

标签: arrays swift


【解决方案1】:

我建议将声音添加到 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)
    }
}

【讨论】:

  • 谢谢,但是当我尝试在声音下方添加该数组时,我得到“实例成员“sound1”不能用于键盘视图控制器类型”的想法?我正在尝试与此类似的事情,但不断收到该实例成员消息错误。
  • 您可能需要进一步研究是否可以在键盘按下时播放其他声音。操作系统已经有点击声音,所以如果不制作自定义键盘可能无法实现。
  • 我可以通过我的自定义键盘播放声音。只是想弄清楚如何随机化在代表“a-z”键的 buttonPressed 函数上播放的声音......所以现在当我按下 a-z 时,我得到了我的声音,当我按下空格键时,我得到了另一个声音。只希望从数组列表中播放 a-z 键,所以它并不总是相同的声音。对不起,如果我不清楚。我想我可以尝试为 q-p、a-l 和 z-m 制作视图,并为每个动作分配不同的声音。
  • 当我再次回到我的办公桌时我会再次测试以确保我没有犯错,但上面的代码应该给你想要随机化数组的内容。给我20分钟!
  • 我在快速连续播放声音时遇到的一件事是每次创建声音时的延迟。与其将 URL 存储在数组中,不如为数组中的每个 URL 存储一个 AVAudioPlayer,然后将 AVAudioPlayer 从数组中拉出并播放。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 2016-06-19
  • 1970-01-01
  • 2015-03-22
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多