I try to handle loop audio in my app. I use AVPlayer to play mp3 file and handle loop. That work perfectly when app active. In the background mode, It doesn't work.
I try to handle loop by this function which work perfectly in active mode. When I tap home and switch to background mode, the music play to the end and loop video is called. This function print afffffffff but videoPlayer?.play() is not work.
func loopVideo(videoPlayer: AVPlayer, index: Int) {
currentSoundIDs[index]["observer"] = NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { [weak videoPlayer] _ in
videoPlayer?.seek(to: CMTime.zero)
print("afffffffff")
videoPlayer?.play()
}
}
I want to make videoPlayer?.play() work in background or the solution to handle loop with AVPlayer in both background and active mode. Thanks!!!
Did you try enabling the option
Audio, AirPlay, and Picture in PictureinBackground Modeconfiguration yet?Ref: Enabling Background Audio