1

我已经为我的AVAudioPlayer. 一切都很好,除了控制中心的进度条,它会在AVAudioPlayer暂停时继续计数。我怎样才能解决这个问题?这是我到目前为止所拥有的:

    func updateNowPlayingCenter (title: String, artist: String, currentTime: AnyObject, songLength: AnyObject, PlaybackRate: Double){

            let image:UIImage = UIImage(named: title)!
            let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                return image
            })


            var songInfo: Dictionary <NSObject, AnyObject> = [

                MPMediaItemPropertyTitle as NSObject: title as AnyObject,

                MPMediaItemPropertyArtist as NSObject: artist as AnyObject,

                MPMediaItemPropertyArtwork as NSObject: artwork,

                MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: currentTime,

                MPMediaItemPropertyPlaybackDuration as NSObject: songLength,

                MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject



            ]


            MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]

        }

override func remoteControlReceived(with event: UIEvent?) {
        if event?.type == UIEventType.remoteControl {
            if event?.subtype == UIEventSubtype.remoteControlPlay {
                audioPlayer.play()
            } else if event?.subtype == UIEventSubtype.remoteControlPause {
                audioPlayer.pause
            } else if event?.subtype == UIEventSubtype.remoteControlNextTrack {
                nextButtonTapped()
            } else if event?.subtype == UIEventSubtype.remoteControlPreviousTrack {
                previousButtonTapped()
            }
        }
    }

在 viewDidLoad 中:

updateNowPlayingCenter(title: titleText[thisSong], artist: authorText[thisSong], currentTime: audioPlayer.currentTime as AnyObject, songLength: audioPlayer.duration as AnyObject, PlaybackRate: 1.0)

我正在使用 Swift 3 和 Xcode 8。

4

0 回答 0