Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping iOS14 VideoPlayer

Tags:

swift

swiftui

I'm fairly new to SwiftUI and I'm not massively sure how to loop a video using VideoPlayer on iOS 14 and I've found little documentation around it. Most of the solutions I have found out there are super complex and confusing in many ways. I'd be ever so grateful if anyone can help.

VideoPlayerHelper.swift File

import Foundation
import AVKit
//AutoPlay Video
var videoPlayer: AVPlayer?

func playVideo(fileName: String, fileFormat: String) -> AVPlayer {
    if Bundle.main.url(forResource: fileName, withExtension: fileFormat) != nil {
        videoPlayer = AVPlayer(url: Bundle.main.url(forResource: fileName, withExtension: fileFormat)!)
//        videoPlayer?.play()
        
    }
    return videoPlayer!
}

VideoPlayerView.swift File

import SwiftUI
import AVKit

struct VideoPlayerView: View {
    // MARK: - PROPERTIES
    var videoSelected: String
    var videoTitle: String
    
    // MARK: - BODY
    var body: some View {
        VStack {
            VideoPlayer(player: playVideo(fileName: videoSelected, fileFormat: "mp4"))
} }

Many thanks!

like image 348
screamingchimps Avatar asked Feb 04 '26 14:02

screamingchimps


2 Answers

I travelled the Internet far and wide in search of the rumored SwiftUI looping video. I thought I'd share the culmination of all my Stack Overflowing (including this post) and searchengining.

Note that playerLooper needs to be retained or the video won't loop.

import AVKit
import SwiftUI

struct LoopingVideoPlayer: View {
    private var player: AVQueuePlayer
    private var playerLooper: AVPlayerLooper
    
    var body: some View {
        VideoPlayer(player: player)
            .onAppear { player.play() }
            .onDisappear{ player.pause() }
    }
    
    init(videoURL: URL) {
        let asset = AVAsset(url: videoURL)
        let item = AVPlayerItem(asset: asset)
        
        player = AVQueuePlayer(playerItem: item)
        playerLooper = AVPlayerLooper(player: player, templateItem: item)
    }
}

#Preview {
    LoopingVideoPlayer(videoURL: URL(string: "https://www.apple.com/105/media/us/airpods/2022/dc1310af-8cb9-4218-8d40-26bbe6a1d307/anim/hero/large_2x.mp4")!)
        .disabled(true) // Hides iOS video controls
        .aspectRatio(contentMode: .fill)
        .frame(maxWidth: .infinity)
}
like image 148
Kokaubeam Avatar answered Feb 06 '26 05:02

Kokaubeam


Basically, you add a Notification observer onAppear and everytime the player ends, you set the time to 0 and start playing again.

struct VideoPlayerView: View {
    @State var player = AVPlayer(url: Bundle.main.url(forResource: "YourURL", withExtension: "mp4")!)
   var body: some View {
       VideoPlayer(player: player)
               .frame(width: 400, height: 300, alignment: .center)
               .onAppear {
                   NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: .main) { _ in
                       player.seek(to: .zero)
                       player.play()
                   }
               }
     
   }
}

Do tick the answer right if this helps

like image 35
Coffee and Waffles Avatar answered Feb 06 '26 06:02

Coffee and Waffles