【问题标题】:change sound file for while loop in swift4在swift4中更改while循环的声音文件
【发布时间】:2018-05-12 13:52:49
【问题描述】:

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

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

【问题讨论】:

  • 这两个音应该如何交替?您是希望每隔一次调用func i() 来选择其他声音,还是希望playWith 中的每次播放都交替使用每种声音?
  • playWith 中的每次播放以交替播放声音。谢谢

标签: ios swift audio while-loop counter


【解决方案1】:

由于您希望在 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()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-09
    • 2014-03-28
    • 2020-07-27
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2020-08-28
    • 2018-08-13
    相关资源
    最近更新 更多