如何编写一个始终显示文本,具有播放,暂停,下一个,上一个功能的程序?

时间:2019-06-30 16:11:19

标签: ios swift

我制作了一个文本播放器,该播放器随后会在一段时间后以一种语言显示该短语,并进行英语翻译,并使用mp3播放该短语。有播放,暂停,下一个,上一个按钮。

我写了代码,其中有一些错误:

class Sentence: Codable {
    var id: Int
    var rus: String
    var eng: String
    var link: String

    init(id: Int, rus: String, eng: String, link: String) {
        self.id = id
        self.rus = rus
        self.eng = eng
        self.link = link
    }
}

class WordsViewController: UIViewController {

    var sentences: [Sentence] = [Sentence]()
    var isPlayTouch = false
    var isIncrement = false
    var speed: Double = 1.5
    var position: Int = 0

    @objc func playAction() {
        self.isPlayTouch = true
        if sentences.count > 0 {

            let duration = self.getDurationAudio(link: self.sentences[self.position].link)

            DispatchQueue.global().async {
                if self.isPlayTouch {
                    Thread.sleep(forTimeInterval: duration * self.speed)
                    if self.isPlayTouch {
                        DispatchQueue.main.async {
                            self.playSound()
                            self.wordsView.sentence.text = self.sentences[self.position].eng
                            DispatchQueue.global().async {
                                if self.isPlayTouch {
                                    Thread.sleep(forTimeInterval: duration)
                                    DispatchQueue.main.async {
                                        if self.isPlayTouch {
                                            if self.position == self.sentences.count - 1 {
                                                self.pauseAction()
                                                if !self.isIncrement {
                                                    self.isIncrement = true
                                                }
                                            } else {
                                                if self.isPlayTouch {
                                                    Thread.sleep(forTimeInterval: 0.5)
                                                    if self.isPlayTouch {
                                                        self.position = self.position + 1
                                                        self.setText()
                                                        if self.isPlayTouch {
                                                            self.playAction()
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } 
}
  • 按下暂停按钮达延迟时间后,立即按下播放列表,一次播放了两个乐句,这是由于检查了isPlayTouch变量

告诉我如何编写此功能?

0 个答案:

没有答案