{"id":23020797,"url":"https://github.com/tiagomaial/piplayground","last_synced_at":"2025-04-23T07:14:37.903Z","repository":{"id":224269332,"uuid":"750981313","full_name":"TiagoMaiaL/PiPlayground","owner":"TiagoMaiaL","description":"A simple iOS app exploring how Picture in Picture works","archived":false,"fork":false,"pushed_at":"2024-02-27T02:55:48.000Z","size":10147,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-23T07:14:31.532Z","etag":null,"topics":["avfoundation","avkit","ios","picture-in-picture","pip","swift","swiftui"],"latest_commit_sha":null,"homepage":"","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/TiagoMaiaL.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}},"created_at":"2024-01-31T17:47:10.000Z","updated_at":"2024-04-03T19:04:38.000Z","dependencies_parsed_at":"2024-02-24T23:38:42.309Z","dependency_job_id":null,"html_url":"https://github.com/TiagoMaiaL/PiPlayground","commit_stats":null,"previous_names":["tiagomaial/piplayground"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiagoMaiaL%2FPiPlayground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiagoMaiaL%2FPiPlayground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiagoMaiaL%2FPiPlayground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TiagoMaiaL%2FPiPlayground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TiagoMaiaL","download_url":"https://codeload.github.com/TiagoMaiaL/PiPlayground/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250386838,"owners_count":21422040,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","avkit","ios","picture-in-picture","pip","swift","swiftui"],"created_at":"2024-12-15T12:15:19.567Z","updated_at":"2025-04-23T07:14:37.857Z","avatar_url":"https://github.com/TiagoMaiaL.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PiPlayground\n\nA simple iOS app exploring how to implement Picture in Picture in a custom player.\n\n## TODOS:\n - [x] Add the project UI\n - [x] Add video playback components\n - [x] Add control for starting pip\n - [x] Add support for pip restoration\n - [x] Document how PiP works in this README file\n\n## Screenshot\n\n\u003cimg src=\"./screenshots/PiPlayground.gif\" width=500px /\u003e\n\n## PiP: Pragmatic Information\n\n### What it is\n- Allows users to watch a video in a little floating window, controlled by iOS\n- Users then are free to navigate in the app or OS itself (open different apps, for example)\n- Users are able to restore playback, allowing them to return to the original playback context\n- It's part of the `AVKit` framework\n- `AVKit` contains UI facilities for visualization of media playback \n\n### How to make it work\n- You'll be interfacing with the `AVPictureInPictureController` class (check `PictureInPicture.swift` in this project)\n- Use an `AVPlayerLayer` to instantiate it:\n```swift\nAVPictureInPictureController(playerLayer: playerLayer)\n```\n- Initialization follows this order:\n  - Create an `AVPlayer` ready for playback\n  - Create an `AVPlayerLayer`, which will have AVPlayer as its player\n  - Create the `AVPictureInPictureController`\n- You'll then need to check if pip is possible:\n```swift\npublisher(for: \\.isPictureInPicturePossible)\n    .filter { $0 == false }\n    .receive(on: RunLoop.main)\n    .timeout(.seconds(1), scheduler: RunLoop.main)\n    .removeDuplicates()\n    .sink(receiveCompletion: { completion in\n        switch completion {\n        case .failure:\n            continuation.resume(returning: false)\n        default:\n            break\n        }\n        \n        cancellable?.cancel()\n    }, receiveValue: { _ in\n        debugPrint(\"Pip is possible.\")\n        continuation.resume(returning: true)\n    })\n```\n- Once pip is possible, use `startPictureInPicture()` and `stopPictureInPicture()` methods to control it\n- Use delegation (`AVPictureInPictureControllerDelegate`) to receive its state:\n```swift\nfunc pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {\n    debugPrint(\"Pip is active.\")\n}\n    \nfunc pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {\n    debugPrint(\"Pip is inactive.\")\n}\n    \nfunc pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {\n    debugPrint(\"Pip error: \\(error)\")\n}\n```\n- PiP provides a way for you to restore the app UI, so users can navigate back to the original playback context:\n```swift\nfunc pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController) async -\u003e Bool {\n    guard let playbackRestorer else { return false }\n    return await playbackRestorer.restore()\n}\n// Check:\n// 1. PictureInPicture.swift (for pip encapsulation and restoration definitions)\n// 2. MovieSession.swift (for playback and pip creation)\n// 3. MoviesCatalogView.swift (for playback context restoration)\n```\n- Automatically start pip when application enters background:\n```swift\npipController.canStartPictureInPictureAutomaticallyFromInline = true\n```\n\n### Limitations\n- Lack of controls customization. The only documented method for customizing some of the controls is when you set `requiresLinearPlayback`\n- Other customization options are described in [this repository](https://github.com/CaiWanFeng/PiP) (although they are not documented by Apple) \n- PiP doesn't work on iPhone simulators\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagomaial%2Fpiplayground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiagomaial%2Fpiplayground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagomaial%2Fpiplayground/lists"}