This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging
preface
Last time we encapsulated audio playback, now we’re going to use it. Before this, we have to deal with a problem, that is, when we return to the home page, the audio can continue to play, and in the top right corner of the navigation bar of the home page will be an audio playback button, so we make the audio playback page a singleton
I’ll leave out the page setup
Logical processing
1. Create a new oneAudioPlayerViewController
The controller
- Create a singleton
static let share = AudioPlayerViewController()
Copy the code
- Add some attributes
Var playerItemModel = AudioItemModel() var didAudioItemModeChange: () -> Void = {} var dataSource = [AudioItemModel]() var playerIndex: Int = 0 private var isSingle: Bool = false /// Single loopCopy the code
- Initialize the
AudioPlayer
private lazy var player: AudioPlayer = { let player = AudioPlayer() player.delegate = self player.playbackPausesWhenResigningActive = false player.playbackPausesWhenBackgrounded = false player.playbackResumesWhenBecameActive = false player.playbackResumesWhenEnteringForeground = false return player }() extension AudioPlayerViewController: AudioPlayerDelegate { func playerStateDidChange(_ player: AudioPlayer) { bottomView.playState = player.playState } func playerCurrentTimeDidChange(_ player: AudioPlayer) { if player.playState == .playing { bottomView.currentTime = player.currentTimeInterval bottomView.maxTime = player.maximumDuration bottomView.sliderValue = Float(player.currentTimeInterval / player.maximumDuration) } } func PlayerPlaybackDidEnd (_ player: AudioPlayer) {/// playerIndex +1 if! isSingle { playerIndex += 1 } playerURL() } func player(_ player: AudioPlayer, didFailWithError error: Error?) {}}Copy the code
- Add a
playerURL()
Method to play
If playerIndex < 0 {playerIndex = datasource.count - 1} if playerIndex > datasource.count -1 {playerIndex = 0} let model = dataSource[playerIndex] /// if model.id == PlayerItemModel.id { return } iconImageView.kf_set(audioModel.album.thumbs.large_thumb) titleLabel.text = model.title playerItemModel = model didAudioItemModeChange() player.player(model.sd_url) }Copy the code
- Initialize to control playback
private lazy var bottomView: AudioPlayerBottomView = {let view = AudioPlayerBottomView () return the view} () / / / play/pause bottomView. DidClickPlayer = { [weak self] in guard let 'self' = self else {return} self.player.changePlay()} /// Slide the progress bar fast forward/backward bottomView.sliderValueDidChange = { [weak self] value in guard let `self` = self else { return } let sliderTime = value * self.player.maximumDuration self.player.seek(to: CMTime(seconds: sliderTime, preferredTimescale: CMTimeScale(1 * NSEC_PER_SEC)))} SliderTouch = {[weak self] value in guard let 'self' = self else {return} if value ==.down { /// bottom view. didNext = {[weak self] in guard let 'self' = self else { return } self.playerIndex += (self.isSingle ? Zero: /// bottomView.didUp = {[weak self] in guard let 'self' = self else {return} self.playerIndex -= (self.isSingle ? Zero: /// play mode bottomView.didPlayerType = {[weak self] isSingle in guard let 'self' = self else { return } self.isSingle = isSingle }Copy the code
- Handle the navigation bar show/hide audio playback button
private func showAudioIcon() { if ! AudioPlayerViewController.share.playerItemModel.id.isEmpty { navigation.item.rightBarButtonItem = UIBarButtonItem(image: "icon_nav_audio".image, closure: { [weak self] _ in guard let `self` = self else { return } self.push(AudioPlayerViewController.share) }) } else { navigation.item.rightBarButtonItem = nil } }Copy the code
- in
AudioPlayerViewController
Add one insidepause()
Method, which is used to pause the audio while the video plays and then call it on the video play page
func pause() { if ! playerItemModel.id.isEmpty { player.pause() } }Copy the code
The effect