ios - 更改swift4中while循环的声音文件

标签 ios swift audio while-loop counter

我的代码有 2 个文本字段。一个表示声音将播放多少次,另一个表示声音之间的间隔时间。我希望每隔一段时间就改变一次声音。所以现在声音只是播放“aaaaaaaa”。我想让它播放 ababababababa

import UIKit
import AVFoundation

class ViewController: UIViewController {
    @IBOutlet weak var whiste: UIImageView!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var textfield2: UITextField!
    var arrPlayer: [AVAudioPlayer] = []
    var player = AVAudioPlayer()
    var timer = Timer()
    var count: Int = 0
    var judo = 0

    @IBAction func i() {
        let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "regularWHistle", ofType: "wav")!)

        do {
            player = try AVAudioPlayer(contentsOf: alertSound)
        } catch {
            print("No sound found by URL")
        }

        if let textValue = self.textField.text, let inputNumber = Int(textValue), inputNumber > 0 {
            playWith(repeatCount: inputNumber)
        } else {
            let alert = UIAlertController(title: "Alert", message: "Please enter number.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))

        }}

    func playWith(repeatCount: Int) {
        var timeInterval = 0.36
        if let textValue = self.textfield2.text, let inputNumber = Double(textValue), inputNumber > 0 {
            timeInterval = inputNumber
        }

        player.play()
        self.timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { (timer) in
            self.count += 1
            print(self.count)
            if self.count != repeatCount {
                self.player.play()
            } else {
                self.count  = 0
                self.player.stop()
                self.timer.invalidate()
            }
        })
    }
}

最佳答案

由于您希望在 playWith 内的每次播放中交替播放音频,因此您需要有两个音频播放器,每个声音一个。

然后根据计数交替玩哪一个。

class ViewController: UIViewController {
    @IBOutlet weak var whiste: UIImageView!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var textfield2: UITextField!
    var arrPlayer: [AVAudioPlayer] = []
    var playerA: AVAudioPlayer!
    var playerB: AVAudioPlayer!
    var judo = 0

    @IBAction func i() {
        let alertSoundA = Bundle.main.url(forResource: "regularWHistle", withExtension: "wav")!
        let alertSoundB = Bundle.main.url(forResource: "otherWHistle", withExtension: "wav")!

        do {
            playerA = try AVAudioPlayer(contentsOf: alertSoundA)
            playerB = try AVAudioPlayer(contentsOf: alertSoundB)
        } catch {
            print("No sound found by URL")
        }

        if let textValue = self.textField.text, let inputNumber = Int(textValue), inputNumber > 0 {
            playWith(repeatCount: inputNumber)
        } else {
            let alert = UIAlertController(title: "Alert", message: "Please enter number.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))

        }}

    func playWith(repeatCount: Int) {
        var timeInterval = 0.36
        if let textValue = self.textfield2.text, let inputNumber = Double(textValue), inputNumber > 0 {
            timeInterval = inputNumber
        }

        var count = 0
        let timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { (timer) in
            print(count)
            if count != repeatCount {
                let player = count % 2 == 0 ? playerA : playerB
                player.play()
                count += 1
            } else {
                timer.invalidate()
            }
        })
        timer.fire()
    }
}

关于ios - 更改swift4中while循环的声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306955/

相关文章:

objective-c - 函数调用后 XCode 3.2.6 堆栈损坏

ios - 如何使用 Objective-C 从安装在设备上的 native twitter 应用程序获取用户详细信息

JSON 请求并不总是返回相同的响应类型(对象和数组)

android - 并行使用MediaRecorder.AudioSource.CAMCORDER

python - 播放 WAV 歌曲时机器人不跳舞

ios - 如何在 iOS 应用程序中添加 Kaltura 播放器

ios - 禁用 Storyboard 跳转

ios - UIActivityViewController 自动关闭

ios - NSObject Delegate类,将Delegate传递给UIViewcontroller类

c++ - wav文件中奇怪的滴答声