ios - 按下按钮后立即停止声音并立即开始

标签 ios swift audio

我正在制作一个小应用程序来测试我的学习情况,大部分情况下我已经掌握了它,但我想出的解决方案有一点需要注意。

每次您点击屏幕时,该应用程序都应播放喇叭声。但这应该允许您连续快速连续按下它,每次都会播放喇叭声。它还应该停止之前的喇叭声,这样就不会有重叠的声音。

这是我到目前为止所管理的

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var horn:AVAudioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareAudios()
    }
    @IBAction func buttonPressed(sender: AnyObject) {
        horn.stop()
        horn.currentTime = 0
        horn.play()
    }

    func prepareAudios() {
        var path = NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")
        horn = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn.prepareToPlay()
    }

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

}

它确实有效。它会停止声音并将其设置回开头,但如果您单击得非常快,它有时会跳过并且不播放。我想尝试解决这个问题,但我在谷歌上找不到太多。有没有更好的解决方案可以让我像这样真正快速地播放这种声音?

更新

根据 Duncan 的回答,我尝试创建两个声音实例来播放并在它们之间切换,但我似乎无法让它们在每次触摸屏幕时立即开始播放声音快速连续。这是我试过的代码

class ViewController: UIViewController {

    var horn1:AVAudioPlayer = AVAudioPlayer()
    var horn2:AVAudioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareAudios()
    }
    var bool = true
    @IBAction func butonPressed(sender: AnyObject) {
        if(bool){
            horn1.play()
            horn2.stop()
            horn2.currentTime = 0
            bool = false
        }else{
            horn2.play()
            horn1.stop()
            horn1.currentTime = 0
            bool = true
        }
    }

    func prepareAudios() {
        var path = NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")
        horn1 = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn2 = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn1.prepareToPlay()
        horn2.prepareToPlay()
    }

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

}

每次按下按钮时,声音播放前都会有轻微的延迟。我希望声音立即重新开始。我希望喇叭能够像这个视频中那样发出声音

https://www.youtube.com/watch?v=Ks5bzvT-D6I

最佳答案

比这简单多了!如果您希望音频在按下按钮后准确开始,您应该在将按钮连接到代码时将事件更改为“Touch Down”。而已!对于按钮,这就足够了:

@IBAction func buttonPressed(sender: AnyObject) {

    horn.currentTime = 0
    horn.play()

}

关于ios - 按下按钮后立即停止声音并立即开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491986/

相关文章:

iOS 框架在没有 "linking"到应用程序的情况下可以正常工作,为什么需要它呢?

ios - 阿拉伯文本的 URL 解析错误

java - TargetDataLine 和 Xuggler 用屏幕视频录制音频

ios - Swift:SWReavealController 无法滑动以关闭菜单 "only open with swipe"

ios - ObservableObject 的问题

ios - reloadData() fatal error : unexpectedly found nil while unwrapping an Optional value

swift - 符合 Swift 协议(protocol) - 'Value of type XX has no member YY'

ios - 在 cocos2d v3 中播放音频

javascript - 我可以使用 Javascript 将 MP3 文件拆分成更小的 MP3 文件吗?

iOS:返回不属于导航 Controller 的入口点 View Controller