ios – Why AVPlayer is enjoying beneath the display has locked, and I pause the play however it execute after few milliseconds?

0
1
ios – Why AVPlayer is enjoying beneath the display has locked, and I pause the play however it execute after few milliseconds?


Why AVPlayer is enjoying beneath the display has locked, and I pause the play however it execute after few milliseconds?

I’m creating an English studying software, the audio will play and pause which is managed by Timer.

However once I shut the display, the AVPlayer is not going to pause as scheduled(It’s managed by lyrics, and the lyrics is correct.), it can delay tons of of milliseconds. That’s not my expectation, I ponder why and tips on how to clear up it.

My codes run effectively when the display is on regardless that the display is locked or the app is operating within the background.My iPhone isn’t beneath low battery mode.

By the way in which, the audio file is saved in iPhone itself.

Relative codes are under:

First I add a observer in AVPlayer subclass:

if observer == nil {
            weak var wself = self;
            observer =  self.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.05, preferredTimescale: CMTimeScale(NSEC_PER_SEC)), queue: nil, utilizing: {cmtime in
                
                if wself != nil {
                    wself!.onTimerTick(cmTime: cmtime);
                }
            })
        }

Secondly, the onTimerTick() methodology:

@objc func onTimerTick(cmTime:CMTime) {
        if self.openAutoPlayAndReadTimer == false {
            if self.autoPlayAndReadTimer != nil {
                if self.autoPlayAndReadTimer!.isValid {
                    self.autoPlayAndReadTimer?.invalidate();
                }
                self.autoPlayAndReadTimer = nil;
            }
            return
        }
        
        let index = UserDefaults.getAL_WhiteCircleTime_In_ReadAndFollowView();
        if index == 0 {
            return
        }
        
        var i = 0;
        for merchandise in self.realPlayTimeList! {
            if merchandise.time < cmTime.seconds  && abs(merchandise.time - cmTime.seconds) < 0.5 {
                break
            }
            i = i + 1;
        }
        
        if i >= self.realPlayTimeList!.depend {
            return
        }
        
        let matchItem = self.realPlayTimeList![i];
        let time = matchItem.time;
        if time == lastTickTime {
            return
        }
        
        //If the duation is zero, break
        if matchItem.stopDuration * matchItem.scale < 0.05 {
            return
        }
                
        self.pause();
        if self.delegate != nil {
            self.delegate?.onSongPauseByAutoPlayAndReadTimer();
        }
        lastTickTime = time;
        
        autoPlayAndReadTimer?.invalidate();
        autoPlayAndReadTimer = nil;
        
        if autoPlayAndReadTimer == nil || autoPlayAndReadTimer?.isValid == false {
            let length = matchItem.stopDuration * matchItem.scale;
            autoPlayAndReadTimer = Timer.scheduledTimer(timeInterval: length, goal: self, selector: #selector(onAutoPlayAndReadTimerTick), userInfo: nil, repeats: true);
        }
        autoPlayAndReadTimer?.fireplace()
        
    }

Third, Timer onAutoPlayAndReadTimerTick() methodology:

   @objc func onAutoPlayAndReadTimerTick() {
        if hasEnter == false {
            hasEnter = true
            return
        } else {
            hasEnter = false
        }
        self.play();
        if self.delegate != nil {
            self.delegate?.onSongPlayByAutoPlayAndReadTimer();
        }
        autoPlayAndReadTimer?.invalidate();
        autoPlayAndReadTimer = nil;
    }

Forth, I add some code in AppDelegate:

let audioSession = AVAudioSession.sharedInstance();
        do {
            attempt audioSession.setCategory(.playback, mode: .default, choices: []) // 或者使用 .ambient
            attempt audioSession.setActive(true)
        } catch {
            print("Setting class to AVAudioSessionCategoryPlayback failed.")
        }

LEAVE A REPLY

Please enter your comment!
Please enter your name here