{"id":26035269,"url":"https://github.com/ivan-magda/swiftui-background-video","last_synced_at":"2026-04-21T01:04:08.522Z","repository":{"id":280870248,"uuid":"943420435","full_name":"ivan-magda/swiftui-background-video","owner":"ivan-magda","description":"Seamless looping background videos for SwiftUI. AVPlayerLooper + lifecycle handling + asset caching. No config, no dependencies. iOS 13+.","archived":false,"fork":false,"pushed_at":"2026-03-03T19:05:10.000Z","size":2542,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-03T22:29:43.683Z","etag":null,"topics":["avfoundation","avplayer","background-video","ios","looping-video","swift","swift-package-manager","swiftui","uikit","video-player"],"latest_commit_sha":null,"homepage":"https://ivan-magda.github.io/swiftui-background-video/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivan-magda.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":"AGENTS.md","dco":null,"cla":null}},"created_at":"2025-03-05T17:19:14.000Z","updated_at":"2026-03-03T19:05:13.000Z","dependencies_parsed_at":"2025-03-05T19:25:27.708Z","dependency_job_id":"4560c513-f349-4715-91f8-6377c28faf5c","html_url":"https://github.com/ivan-magda/swiftui-background-video","commit_stats":null,"previous_names":["ivan-magda/swiftui-background-video"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/ivan-magda/swiftui-background-video","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivan-magda%2Fswiftui-background-video","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivan-magda%2Fswiftui-background-video/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivan-magda%2Fswiftui-background-video/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivan-magda%2Fswiftui-background-video/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivan-magda","download_url":"https://codeload.github.com/ivan-magda/swiftui-background-video/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivan-magda%2Fswiftui-background-video/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32072325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T21:26:33.338Z","status":"ssl_error","status_checked_at":"2026-04-20T21:26:22.081Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avfoundation","avplayer","background-video","ios","looping-video","swift","swift-package-manager","swiftui","uikit","video-player"],"created_at":"2025-03-07T03:14:30.625Z","updated_at":"2026-04-21T01:04:08.513Z","avatar_url":"https://github.com/ivan-magda.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftUIBackgroundVideo\n\n[![Swift](https://img.shields.io/badge/Swift-6.0+-orange?style=flat-square)](https://img.shields.io/badge/Swift-6.0+-orange?style=flat-square)\n[![Platforms](https://img.shields.io/badge/Platforms-iOS-yellowgreen?style=flat-square)](https://img.shields.io/badge/Platforms-iOS-green?style=flat-square)\n[![Swift Package Manager](https://img.shields.io/badge/Swift_Package_Manager-compatible-orange?style=flat-square)](https://img.shields.io/badge/Swift_Package_Manager-compatible-orange?style=flat-square)\n[![License](https://img.shields.io/badge/License-MIT-blue?style=flat-square)](LICENSE)\n\nSeamless looping background videos for SwiftUI-because `VideoPlayer` can't loop and nobody wants 80 lines of AVPlayerLooper boilerplate.\n\n\u003cp align=\"leading\"\u003e\n  \u003cimg src=\"demo/demo-basic.gif\" width=\"240\" alt=\"Basic Demo\"\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003cimg src=\"demo/demo-theme.gif\" width=\"240\" alt=\"Theme-Aware Demo\"\u003e\n\u003c/p\u003e\n\n## Why SwiftUIBackgroundVideo?\n\nSwiftUI's native `VideoPlayer` can't loop videos or hide controls-it's designed for interactive playback, not backgrounds. The standard fix requires wrapping `AVQueuePlayer` + `AVPlayerLooper` in `UIViewRepresentable`, handling app lifecycle (background/foreground), audio interruptions, and memory management.\n\nThis package does all of that in 3 lines:\n\n```swift\nBackgroundVideoView(resourceName: \"background\", resourceType: \"mp4\")\n```\n\n**Alternatives:**\n\n| Package                      | Verdict                                                                                          |\n| ---------------------------- | ------------------------------------------------------------------------------------------------ |\n| **swiftui-loop-videoPlayer** | Feature-heavy (subtitles, Metal shaders, PiP). Good if you need those; overkill for backgrounds. |\n| **SwiftVideoBackground**     | UIKit-only, last updated 2019, no SPM support.                                                   |\n| **DIY**                      | 80+ lines of boilerplate you'll copy from Stack Overflow anyway.                                 |\n\n## Features\n\n- **3-line integration** - Drop `BackgroundVideoView` into any SwiftUI view\n- **Truly seamless loops** - Uses `AVPlayerLooper`, not notification-based seeking (no 100ms gaps)\n- **Asset caching** - `NSCache`-backed, max 3 assets, auto-clears on memory warning\n- **Lifecycle-aware** - Auto-pauses on background, resumes on foreground\n- **Audio interruption handling** - Phone calls won't break your player\n- **UIKit support** - Use `BackgroundVideoUIView` directly if needed\n- **iOS 13+** - Works on 99%+ of devices in the wild\n\n## Requirements\n\n- iOS 13.0+\n- Swift 6.0+\n- Xcode 16+\n\n## Installation\n\n### Swift Package Manager\n\nAdd SwiftUIBackgroundVideo to your project by adding it as a dependency in your `Package.swift` file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/ivan-magda/swiftui-background-video.git\", from: \"1.3.0\")\n]\n```\n\nOr add it directly through Xcode:\n\n1. Go to **File → Add Packages...**\n2. Enter package URL: `https://github.com/ivan-magda/swiftui-background-video.git`\n3. Click **Add Package**\n\n## Usage\n\n### SwiftUI\n\n```swift\nimport SwiftUI\nimport SwiftUIBackgroundVideo\n\nstruct ContentView: View {\n    var body: some View {\n        ZStack {\n            BackgroundVideoView(\n                resourceName: \"background_video\",\n                resourceType: \"mp4\"\n            )\n            .ignoresSafeArea()\n\n            Text(\"Hello, World!\")\n                .foregroundStyle(.white)\n                .font(.largeTitle)\n                .padding()\n                .background(Color.black.opacity(0.5))\n                .clipShape(RoundedRectangle(cornerRadius: 10))\n        }\n    }\n}\n```\n\n### Handling State Changes\n\nMonitor loading, playback, and errors:\n\n```swift\nimport SwiftUI\nimport SwiftUIBackgroundVideo\n\nstruct ContentView: View {\n    @State private var isLoading = true\n    @State private var errorMessage: String?\n\n    var body: some View {\n        ZStack {\n            BackgroundVideoView(\n                resourceName: \"background_video\",\n                resourceType: \"mp4\"\n            ) { state in\n                switch state {\n                case .idle:\n                    break\n                case .loading:\n                    isLoading = true\n                case .playing:\n                    isLoading = false\n                case .paused:\n                    break\n                case .failed(let error):\n                    isLoading = false\n                    errorMessage = error.localizedDescription\n                }\n            }\n            .ignoresSafeArea()\n\n            if isLoading {\n                ProgressView()\n                    .scaleEffect(1.5)\n            }\n\n            if let error = errorMessage {\n                Text(\"Error: \\(error)\")\n                    .foregroundStyle(.red)\n            }\n        }\n    }\n}\n```\n\n### UIKit\n\n```swift\nimport UIKit\nimport SwiftUIBackgroundVideo\n\nclass ViewController: UIViewController {\n\n    private var videoView: BackgroundVideoUIView?\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        // Create and add the video view\n        videoView = BackgroundVideoUIView(\n            frame: view.bounds,\n            resourceName: \"background_video\",\n            resourceType: \"mp4\"\n        )\n        videoView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]\n\n        if let videoView = videoView {\n            view.addSubview(videoView)\n        }\n\n        // Monitor state changes\n        videoView?.stateDidChange = { state in\n            switch state {\n            case .loading:\n                print(\"Loading video...\")\n            case .playing:\n                print(\"Video playing\")\n            case .paused:\n                print(\"Video paused\")\n            case .failed(let error):\n                print(\"Error: \\(error.localizedDescription)\")\n            default:\n                break\n            }\n        }\n\n        // Add content on top\n        let label = UILabel()\n        label.text = \"Hello, World!\"\n        label.textColor = .white\n        label.font = .preferredFont(forTextStyle: .largeTitle)\n        label.textAlignment = .center\n        label.backgroundColor = UIColor.black.withAlphaComponent(0.5)\n        label.layer.cornerRadius = 10\n        label.clipsToBounds = true\n        label.translatesAutoresizingMaskIntoConstraints = false\n\n        view.addSubview(label)\n        NSLayoutConstraint.activate([\n            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),\n            label.centerYAnchor.constraint(equalTo: view.centerYAnchor),\n            label.widthAnchor.constraint(lessThanOrEqualTo: view.widthAnchor, constant: -40)\n        ])\n    }\n}\n```\n\n## API Reference\n\n### BackgroundVideoView (SwiftUI)\n\n| Parameter        | Type                            | Description                                    |\n| ---------------- | ------------------------------- | ---------------------------------------------- |\n| `resourceName`   | `String`                        | Video filename without extension               |\n| `resourceType`   | `String`                        | File extension (e.g., \"mp4\", \"mov\")            |\n| `bundle`         | `Bundle`                        | Bundle containing the video (default: `.main`) |\n| `onStateChanged` | `((VideoPlayerState) -\u003e Void)?` | Optional state change callback                 |\n\n### BackgroundVideoUIView (UIKit)\n\n| Property/Method                            | Type                            | Description                         |\n| ------------------------------------------ | ------------------------------- | ----------------------------------- |\n| `stateDidChange`                           | `((VideoPlayerState) -\u003e Void)?` | State change callback               |\n| `playerState`                              | `VideoPlayerState`              | Current playback state (read-only)  |\n| `prepareAndPlayVideo(with:ofType:bundle:)` | Method                          | Load and play a different video     |\n| `cleanupPlayer()`                          | Method                          | Stop playback and release resources |\n\n### VideoPlayerState\n\n| Case             | Description                               |\n| ---------------- | ----------------------------------------- |\n| `.idle`          | Player initialized, no video loaded       |\n| `.loading`       | Video asset loading asynchronously        |\n| `.playing`       | Video actively playing                    |\n| `.paused`        | Playback paused (background/interruption) |\n| `.failed(Error)` | Loading or playback failed                |\n\n### VideoPlayerError\n\n| Case                | Description                     |\n| ------------------- | ------------------------------- |\n| `.resourceNotFound` | Video file not in app bundle    |\n| `.invalidResource`  | File exists but can't be played |\n| `.playbackFailed`   | Runtime playback error          |\n\n## How It Works\n\nUnder the hood, SwiftUIBackgroundVideo uses Apple's recommended approach for seamless video looping:\n\n1. **AVQueuePlayer + AVPlayerLooper** - The \"treadmill pattern\" from WWDC 2016 that cycles player items without gaps\n2. **AVPlayerLayer** - Hardware-accelerated video rendering with aspect-fill scaling\n3. **NSCache** - Lightweight asset caching (max 3 videos) with automatic memory warning cleanup\n4. **NotificationCenter** - Observes `willEnterForeground`, `didEnterBackground`, and `interruptionNotification` for proper lifecycle handling\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Author\n\n**Ivan Magda** - [@ivan-magda](https://github.com/ivan-magda)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivan-magda%2Fswiftui-background-video","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivan-magda%2Fswiftui-background-video","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivan-magda%2Fswiftui-background-video/lists"}