HomeiOS Developmentios - Why are AVPlayer pause an play actions delayed with the...

ios – Why are AVPlayer pause an play actions delayed with the display screen locked?


Why when AVPlayer is taking part in with the display screen locked and I pause taking part in, it execute solely after few milliseconds? In my English studying utility the audio will play and pause managed by a timer. However after I shut the display screen off the AVPlayer won’t pause as scheduled (it’s managed by lyrics that are correct), however is delayed by a whole bunch of milliseconds.

Why and the way can I resolve it? My code runs properly when the display screen is on even with the display screen locked or when the app is working within the background. My iPhone isn’t in low battery mode. The audio file is saved on the iPhone itself.

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);
        }
    })
}

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 = 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 

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;
}

Code in AppDelegate:

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments