根据文本字段中列出的次数播放声音

时间:2018-04-28 13:53:30

标签: ios swift loops int textfield

我的代码下面是一个文本字段和一个声音函数。我希望用户在按下按钮时播放声音文件的次数是int在文本字段中的次数。

import UIKit
import AVFoundation

class ViewController: UIViewController {
    @IBOutlet var sam : UITextField!
    var bombSoundEffect: AVAudioPlayer?

    func judo(){
        let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)!
        let url = URL(fileURLWithPath: path)

        do {
            bombSoundEffect = try AVAudioPlayer(contentsOf: url)
            bombSoundEffect?.play()
        } catch {
            // couldn't load file :(
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

请使用numberOfLoops:        " numberOfLoops"是声音在到达结尾时返回到开头的次数。       值为零意味着只播放一次声音。       值为1将导致播放声音两次,依此类推。       任何负数都会无限循环直到停止。

bombSoundEffect.numberOfLoops = 1

你可以用另一种方式做一些延迟:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    var player = AVAudioPlayer()
    var timer = Timer()
    var count: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func playAction(_ sender: Any) {

        let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "stereo", 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))
            self.present(alert, animated: true, completion: nil)
        }
    }
    func playWith(repeatCount: Int) {

        player.play()
        self.timer = Timer.scheduledTimer(withTimeInterval: 0.36, 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()
            }
        })
    }
}

这正是您想要的。在此演示中,您必须在文本字段中输入输入编号,之后声音将输入输入次数。

如果你想要那个时间间隔也会动态意味着时间间隔也会来自textField2然后使用这个方法:

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