22.4 C
Canberra
Thursday, November 13, 2025

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


Why when AVPlayer is enjoying with the display screen locked and I pause enjoying, it execute solely after few milliseconds? In my English studying software the audio will play and pause managed by a timer. However once 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 tons of of milliseconds.

Why and the way can I clear up it? My code runs nicely 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() technique:

@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!.rely {
        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()

}

Timer onAutoPlayAndReadTimerTick() technique:

@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 {
        attempt audioSession.setCategory(.playback, mode: .default, choices: []) // 或者使用 .ambient
        attempt audioSession.setActive(true)
    } catch {
        print("Setting class to AVAudioSessionCategoryPlayback failed.")
    }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles