如何使用倒数计时器(日期选择器)制作计时器

时间:2019-03-21 22:16:25

标签: ios swift3

因此,我正在开发一个允许您在计时器上聆听睡眠/冥想声音的应用程序。我已经具有根据所选声音播放和暂停声音的功能,但是我希望屏幕上的日期选择器充当该播放器的计时器。因此,无论用户将倒数计时器设置为什么,声音都将播放多长时间。任何帮助将不胜感激。

@IBAction func playBtnAction(_ sender: Any) {
    if playBtn.currentBackgroundImage == UIImage(named: "play") && Sounds.selectedSegmentIndex == 0
    {
        playBtn.setBackgroundImage(UIImage(named: "pause"), for: .normal)
        let path = Bundle.main.path(forResource: "rain", ofType: "mp3")!
        let url = URL(fileURLWithPath : path)

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player.play()
        } catch {
            print("error")
        }
    } else
    if playBtn.currentBackgroundImage == UIImage(named: "play") && Sounds.selectedSegmentIndex == 1
    {
        playBtn.setBackgroundImage(UIImage(named: "pause"), for: .normal)
        let path = Bundle.main.path(forResource: "ocean", ofType: "mp3")!
        let url = URL(fileURLWithPath : path)

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player.play()
        } catch {
            print("error")
        }
    }else
    if playBtn.currentBackgroundImage == UIImage(named: "play") && Sounds.selectedSegmentIndex == 2
    {
        playBtn.setBackgroundImage(UIImage(named: "pause"), for: .normal)
        let path = Bundle.main.path(forResource: "crickets", ofType: "mp3")!
        let url = URL(fileURLWithPath : path)

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player.play()
        } catch {
            print("error")
        }
    } else
    if playBtn.currentBackgroundImage == UIImage(named: "play") && Sounds.selectedSegmentIndex == 3
    {
        playBtn.setBackgroundImage(UIImage(named: "pause"), for: .normal)
        let path = Bundle.main.path(forResource: "peaceful", ofType: "mp3")!
        let url = URL(fileURLWithPath : path)

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player.play()
        } catch {
            print("error")
        }
    }
    else
    {
        player.pause()
        playBtn.setBackgroundImage(UIImage(named: "play"), for: .normal)
    }
}

0 个答案:

没有答案