├── LICENSE ├── README.md ├── custom-avplayer-swiftui.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── custom-avplayer-swiftui ├── Assets.xcassets ├── AccentColor.colorset │ └── Contents.json ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Info.plist ├── Media.swift ├── PlayerViewModel.swift ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── SwiftUIViews ├── CustomControlsView.swift ├── CustomPlayerView.swift └── CustomVideoPlayer.swift ├── UIKitViews └── PlayerView.swift └── custom_avplayer_swiftui.swift /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Create with Swift 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Custom AVPlayer with SwiftUI 2 | A sample project to create a Custom video player with AVKit and SwiftUI supporting Picture-In-Picture, which is used in the Create with Swift article at https://createwithswift.com/custom-video-player-with-avkit-and-swiftui-supporting-picture-in-picture. 3 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 42D544032747DDA7007BD580 /* custom_avplayer_swiftui.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544022747DDA7007BD580 /* custom_avplayer_swiftui.swift */; }; 11 | 42D544052747DDA7007BD580 /* CustomPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544042747DDA7007BD580 /* CustomPlayerView.swift */; }; 12 | 42D544072747DDA9007BD580 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 42D544062747DDA9007BD580 /* Assets.xcassets */; }; 13 | 42D5440A2747DDA9007BD580 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 42D544092747DDA9007BD580 /* Preview Assets.xcassets */; }; 14 | 42D544112747DDF2007BD580 /* PlayerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544102747DDF2007BD580 /* PlayerViewModel.swift */; }; 15 | 42D544132747DE28007BD580 /* Media.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544122747DE28007BD580 /* Media.swift */; }; 16 | 42D544162747DE6A007BD580 /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544152747DE6A007BD580 /* PlayerView.swift */; }; 17 | 42D544192747DE9D007BD580 /* CustomControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D544182747DE9D007BD580 /* CustomControlsView.swift */; }; 18 | 42D5441C2747DF32007BD580 /* CustomVideoPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D5441B2747DF32007BD580 /* CustomVideoPlayer.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 42D543FF2747DDA7007BD580 /* custom-avplayer-swiftui.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "custom-avplayer-swiftui.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | 42D544022747DDA7007BD580 /* custom_avplayer_swiftui.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = custom_avplayer_swiftui.swift; sourceTree = ""; }; 24 | 42D544042747DDA7007BD580 /* CustomPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomPlayerView.swift; sourceTree = ""; }; 25 | 42D544062747DDA9007BD580 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 42D544092747DDA9007BD580 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 27 | 42D544102747DDF2007BD580 /* PlayerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewModel.swift; sourceTree = ""; }; 28 | 42D544122747DE28007BD580 /* Media.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Media.swift; sourceTree = ""; }; 29 | 42D544152747DE6A007BD580 /* PlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerView.swift; sourceTree = ""; }; 30 | 42D544182747DE9D007BD580 /* CustomControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomControlsView.swift; sourceTree = ""; }; 31 | 42D5441A2747DEBE007BD580 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = ""; }; 32 | 42D5441B2747DF32007BD580 /* CustomVideoPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomVideoPlayer.swift; sourceTree = ""; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | 42D543FC2747DDA7007BD580 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | ); 41 | runOnlyForDeploymentPostprocessing = 0; 42 | }; 43 | /* End PBXFrameworksBuildPhase section */ 44 | 45 | /* Begin PBXGroup section */ 46 | 42D543F62747DDA7007BD580 = { 47 | isa = PBXGroup; 48 | children = ( 49 | 42D544012747DDA7007BD580 /* custom-avplayer-swiftui */, 50 | 42D544002747DDA7007BD580 /* Products */, 51 | ); 52 | sourceTree = ""; 53 | }; 54 | 42D544002747DDA7007BD580 /* Products */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 42D543FF2747DDA7007BD580 /* custom-avplayer-swiftui.app */, 58 | ); 59 | name = Products; 60 | sourceTree = ""; 61 | }; 62 | 42D544012747DDA7007BD580 /* custom-avplayer-swiftui */ = { 63 | isa = PBXGroup; 64 | children = ( 65 | 42D5441A2747DEBE007BD580 /* Info.plist */, 66 | 42D544022747DDA7007BD580 /* custom_avplayer_swiftui.swift */, 67 | 42D544142747DE5E007BD580 /* UIKitViews */, 68 | 42D544172747DE7F007BD580 /* SwiftUIViews */, 69 | 42D544102747DDF2007BD580 /* PlayerViewModel.swift */, 70 | 42D544122747DE28007BD580 /* Media.swift */, 71 | 42D544062747DDA9007BD580 /* Assets.xcassets */, 72 | 42D544082747DDA9007BD580 /* Preview Content */, 73 | ); 74 | path = "custom-avplayer-swiftui"; 75 | sourceTree = ""; 76 | }; 77 | 42D544082747DDA9007BD580 /* Preview Content */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 42D544092747DDA9007BD580 /* Preview Assets.xcassets */, 81 | ); 82 | path = "Preview Content"; 83 | sourceTree = ""; 84 | }; 85 | 42D544142747DE5E007BD580 /* UIKitViews */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 42D544152747DE6A007BD580 /* PlayerView.swift */, 89 | ); 90 | path = UIKitViews; 91 | sourceTree = ""; 92 | }; 93 | 42D544172747DE7F007BD580 /* SwiftUIViews */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 42D544042747DDA7007BD580 /* CustomPlayerView.swift */, 97 | 42D5441B2747DF32007BD580 /* CustomVideoPlayer.swift */, 98 | 42D544182747DE9D007BD580 /* CustomControlsView.swift */, 99 | ); 100 | path = SwiftUIViews; 101 | sourceTree = ""; 102 | }; 103 | /* End PBXGroup section */ 104 | 105 | /* Begin PBXNativeTarget section */ 106 | 42D543FE2747DDA7007BD580 /* custom-avplayer-swiftui */ = { 107 | isa = PBXNativeTarget; 108 | buildConfigurationList = 42D5440D2747DDA9007BD580 /* Build configuration list for PBXNativeTarget "custom-avplayer-swiftui" */; 109 | buildPhases = ( 110 | 42D543FB2747DDA7007BD580 /* Sources */, 111 | 42D543FC2747DDA7007BD580 /* Frameworks */, 112 | 42D543FD2747DDA7007BD580 /* Resources */, 113 | ); 114 | buildRules = ( 115 | ); 116 | dependencies = ( 117 | ); 118 | name = "custom-avplayer-swiftui"; 119 | productName = "custom-avplayer-swiftui"; 120 | productReference = 42D543FF2747DDA7007BD580 /* custom-avplayer-swiftui.app */; 121 | productType = "com.apple.product-type.application"; 122 | }; 123 | /* End PBXNativeTarget section */ 124 | 125 | /* Begin PBXProject section */ 126 | 42D543F72747DDA7007BD580 /* Project object */ = { 127 | isa = PBXProject; 128 | attributes = { 129 | BuildIndependentTargetsInParallel = 1; 130 | LastSwiftUpdateCheck = 1310; 131 | LastUpgradeCheck = 1310; 132 | TargetAttributes = { 133 | 42D543FE2747DDA7007BD580 = { 134 | CreatedOnToolsVersion = 13.1; 135 | }; 136 | }; 137 | }; 138 | buildConfigurationList = 42D543FA2747DDA7007BD580 /* Build configuration list for PBXProject "custom-avplayer-swiftui" */; 139 | compatibilityVersion = "Xcode 13.0"; 140 | developmentRegion = en; 141 | hasScannedForEncodings = 0; 142 | knownRegions = ( 143 | en, 144 | Base, 145 | ); 146 | mainGroup = 42D543F62747DDA7007BD580; 147 | productRefGroup = 42D544002747DDA7007BD580 /* Products */; 148 | projectDirPath = ""; 149 | projectRoot = ""; 150 | targets = ( 151 | 42D543FE2747DDA7007BD580 /* custom-avplayer-swiftui */, 152 | ); 153 | }; 154 | /* End PBXProject section */ 155 | 156 | /* Begin PBXResourcesBuildPhase section */ 157 | 42D543FD2747DDA7007BD580 /* Resources */ = { 158 | isa = PBXResourcesBuildPhase; 159 | buildActionMask = 2147483647; 160 | files = ( 161 | 42D5440A2747DDA9007BD580 /* Preview Assets.xcassets in Resources */, 162 | 42D544072747DDA9007BD580 /* Assets.xcassets in Resources */, 163 | ); 164 | runOnlyForDeploymentPostprocessing = 0; 165 | }; 166 | /* End PBXResourcesBuildPhase section */ 167 | 168 | /* Begin PBXSourcesBuildPhase section */ 169 | 42D543FB2747DDA7007BD580 /* Sources */ = { 170 | isa = PBXSourcesBuildPhase; 171 | buildActionMask = 2147483647; 172 | files = ( 173 | 42D5441C2747DF32007BD580 /* CustomVideoPlayer.swift in Sources */, 174 | 42D544112747DDF2007BD580 /* PlayerViewModel.swift in Sources */, 175 | 42D544052747DDA7007BD580 /* CustomPlayerView.swift in Sources */, 176 | 42D544132747DE28007BD580 /* Media.swift in Sources */, 177 | 42D544192747DE9D007BD580 /* CustomControlsView.swift in Sources */, 178 | 42D544162747DE6A007BD580 /* PlayerView.swift in Sources */, 179 | 42D544032747DDA7007BD580 /* custom_avplayer_swiftui.swift in Sources */, 180 | ); 181 | runOnlyForDeploymentPostprocessing = 0; 182 | }; 183 | /* End PBXSourcesBuildPhase section */ 184 | 185 | /* Begin XCBuildConfiguration section */ 186 | 42D5440B2747DDA9007BD580 /* Debug */ = { 187 | isa = XCBuildConfiguration; 188 | buildSettings = { 189 | ALWAYS_SEARCH_USER_PATHS = NO; 190 | CLANG_ANALYZER_NONNULL = YES; 191 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_ENABLE_OBJC_WEAK = YES; 197 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 198 | CLANG_WARN_BOOL_CONVERSION = YES; 199 | CLANG_WARN_COMMA = YES; 200 | CLANG_WARN_CONSTANT_CONVERSION = YES; 201 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 202 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 203 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 204 | CLANG_WARN_EMPTY_BODY = YES; 205 | CLANG_WARN_ENUM_CONVERSION = YES; 206 | CLANG_WARN_INFINITE_RECURSION = YES; 207 | CLANG_WARN_INT_CONVERSION = YES; 208 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 209 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 210 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 211 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 212 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 213 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 214 | CLANG_WARN_STRICT_PROTOTYPES = YES; 215 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 216 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 217 | CLANG_WARN_UNREACHABLE_CODE = YES; 218 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 219 | COPY_PHASE_STRIP = NO; 220 | DEBUG_INFORMATION_FORMAT = dwarf; 221 | ENABLE_STRICT_OBJC_MSGSEND = YES; 222 | ENABLE_TESTABILITY = YES; 223 | GCC_C_LANGUAGE_STANDARD = gnu11; 224 | GCC_DYNAMIC_NO_PIC = NO; 225 | GCC_NO_COMMON_BLOCKS = YES; 226 | GCC_OPTIMIZATION_LEVEL = 0; 227 | GCC_PREPROCESSOR_DEFINITIONS = ( 228 | "DEBUG=1", 229 | "$(inherited)", 230 | ); 231 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 232 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 233 | GCC_WARN_UNDECLARED_SELECTOR = YES; 234 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 235 | GCC_WARN_UNUSED_FUNCTION = YES; 236 | GCC_WARN_UNUSED_VARIABLE = YES; 237 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 238 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 239 | MTL_FAST_MATH = YES; 240 | ONLY_ACTIVE_ARCH = YES; 241 | SDKROOT = iphoneos; 242 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 243 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 244 | }; 245 | name = Debug; 246 | }; 247 | 42D5440C2747DDA9007BD580 /* Release */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | ALWAYS_SEARCH_USER_PATHS = NO; 251 | CLANG_ANALYZER_NONNULL = YES; 252 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 253 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 254 | CLANG_CXX_LIBRARY = "libc++"; 255 | CLANG_ENABLE_MODULES = YES; 256 | CLANG_ENABLE_OBJC_ARC = YES; 257 | CLANG_ENABLE_OBJC_WEAK = YES; 258 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 259 | CLANG_WARN_BOOL_CONVERSION = YES; 260 | CLANG_WARN_COMMA = YES; 261 | CLANG_WARN_CONSTANT_CONVERSION = YES; 262 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 263 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 264 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 265 | CLANG_WARN_EMPTY_BODY = YES; 266 | CLANG_WARN_ENUM_CONVERSION = YES; 267 | CLANG_WARN_INFINITE_RECURSION = YES; 268 | CLANG_WARN_INT_CONVERSION = YES; 269 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 270 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 271 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 272 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 273 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 274 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 275 | CLANG_WARN_STRICT_PROTOTYPES = YES; 276 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 277 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 278 | CLANG_WARN_UNREACHABLE_CODE = YES; 279 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 280 | COPY_PHASE_STRIP = NO; 281 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 282 | ENABLE_NS_ASSERTIONS = NO; 283 | ENABLE_STRICT_OBJC_MSGSEND = YES; 284 | GCC_C_LANGUAGE_STANDARD = gnu11; 285 | GCC_NO_COMMON_BLOCKS = YES; 286 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 287 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 288 | GCC_WARN_UNDECLARED_SELECTOR = YES; 289 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 290 | GCC_WARN_UNUSED_FUNCTION = YES; 291 | GCC_WARN_UNUSED_VARIABLE = YES; 292 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 293 | MTL_ENABLE_DEBUG_INFO = NO; 294 | MTL_FAST_MATH = YES; 295 | SDKROOT = iphoneos; 296 | SWIFT_COMPILATION_MODE = wholemodule; 297 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 298 | VALIDATE_PRODUCT = YES; 299 | }; 300 | name = Release; 301 | }; 302 | 42D5440E2747DDA9007BD580 /* Debug */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 306 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 307 | CODE_SIGN_STYLE = Automatic; 308 | CURRENT_PROJECT_VERSION = 1; 309 | DEVELOPMENT_ASSET_PATHS = "\"custom-avplayer-swiftui/Preview Content\""; 310 | DEVELOPMENT_TEAM = ""; 311 | ENABLE_PREVIEWS = YES; 312 | GENERATE_INFOPLIST_FILE = YES; 313 | INFOPLIST_FILE = "custom-avplayer-swiftui/Info.plist"; 314 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 315 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 316 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 317 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 318 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 319 | LD_RUNPATH_SEARCH_PATHS = ( 320 | "$(inherited)", 321 | "@executable_path/Frameworks", 322 | ); 323 | MARKETING_VERSION = 1.0; 324 | PRODUCT_BUNDLE_IDENTIFIER = "com.createwithswift.custom-avplayer-swiftui"; 325 | PRODUCT_NAME = "$(TARGET_NAME)"; 326 | SWIFT_EMIT_LOC_STRINGS = YES; 327 | SWIFT_VERSION = 5.0; 328 | TARGETED_DEVICE_FAMILY = "1,2"; 329 | }; 330 | name = Debug; 331 | }; 332 | 42D5440F2747DDA9007BD580 /* Release */ = { 333 | isa = XCBuildConfiguration; 334 | buildSettings = { 335 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 336 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 337 | CODE_SIGN_STYLE = Automatic; 338 | CURRENT_PROJECT_VERSION = 1; 339 | DEVELOPMENT_ASSET_PATHS = "\"custom-avplayer-swiftui/Preview Content\""; 340 | DEVELOPMENT_TEAM = ""; 341 | ENABLE_PREVIEWS = YES; 342 | GENERATE_INFOPLIST_FILE = YES; 343 | INFOPLIST_FILE = "custom-avplayer-swiftui/Info.plist"; 344 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 345 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 346 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 347 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 348 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 349 | LD_RUNPATH_SEARCH_PATHS = ( 350 | "$(inherited)", 351 | "@executable_path/Frameworks", 352 | ); 353 | MARKETING_VERSION = 1.0; 354 | PRODUCT_BUNDLE_IDENTIFIER = "com.createwithswift.custom-avplayer-swiftui"; 355 | PRODUCT_NAME = "$(TARGET_NAME)"; 356 | SWIFT_EMIT_LOC_STRINGS = YES; 357 | SWIFT_VERSION = 5.0; 358 | TARGETED_DEVICE_FAMILY = "1,2"; 359 | }; 360 | name = Release; 361 | }; 362 | /* End XCBuildConfiguration section */ 363 | 364 | /* Begin XCConfigurationList section */ 365 | 42D543FA2747DDA7007BD580 /* Build configuration list for PBXProject "custom-avplayer-swiftui" */ = { 366 | isa = XCConfigurationList; 367 | buildConfigurations = ( 368 | 42D5440B2747DDA9007BD580 /* Debug */, 369 | 42D5440C2747DDA9007BD580 /* Release */, 370 | ); 371 | defaultConfigurationIsVisible = 0; 372 | defaultConfigurationName = Release; 373 | }; 374 | 42D5440D2747DDA9007BD580 /* Build configuration list for PBXNativeTarget "custom-avplayer-swiftui" */ = { 375 | isa = XCConfigurationList; 376 | buildConfigurations = ( 377 | 42D5440E2747DDA9007BD580 /* Debug */, 378 | 42D5440F2747DDA9007BD580 /* Release */, 379 | ); 380 | defaultConfigurationIsVisible = 0; 381 | defaultConfigurationName = Release; 382 | }; 383 | /* End XCConfigurationList section */ 384 | }; 385 | rootObject = 42D543F72747DDA7007BD580 /* Project object */; 386 | } 387 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIBackgroundModes 6 | 7 | audio 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Media.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Media.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import Foundation 9 | import AVFoundation 10 | 11 | struct Media: Identifiable { 12 | let id = UUID() 13 | let title: String 14 | let url: String 15 | } 16 | 17 | extension Media { 18 | var asPlayerItem: AVPlayerItem { 19 | AVPlayerItem(url: URL(string: url)!) 20 | } 21 | 22 | static var playlist: [Media] = [ 23 | .init(title: "First video", url: "URL_TO_FIRST_VIDEO.m3u8"), 24 | .init(title: "Second video", url: "URL_TO_SECOND_VIDEO.mp4"), 25 | .init(title: "Third video", url: "URL_TO_THIRD_VIDEO.m3u8"), 26 | .init(title: "Fourth video", url: "URL_TO_FOURTH_VIDEO.m3u8"), 27 | .init(title: "Fifth video", url: "URL_TO_FIFTH_VIDEO.mp4") 28 | // ... 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/PlayerViewModel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PlayerViewModel.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import AVFoundation 9 | import Combine 10 | 11 | final class PlayerViewModel: ObservableObject { 12 | let player = AVPlayer() 13 | @Published var isInPipMode: Bool = false 14 | @Published var isPlaying = false 15 | 16 | @Published var isEditingCurrentTime = false 17 | @Published var currentTime: Double = .zero 18 | @Published var duration: Double? 19 | 20 | private var subscriptions: Set = [] 21 | private var timeObserver: Any? 22 | 23 | deinit { 24 | if let timeObserver = timeObserver { 25 | player.removeTimeObserver(timeObserver) 26 | } 27 | } 28 | 29 | init() { 30 | $isEditingCurrentTime 31 | .dropFirst() 32 | .filter({ $0 == false }) 33 | .sink(receiveValue: { [weak self] _ in 34 | guard let self = self else { return } 35 | self.player.seek(to: CMTime(seconds: self.currentTime, preferredTimescale: 1), toleranceBefore: .zero, toleranceAfter: .zero) 36 | if self.player.rate != 0 { 37 | self.player.play() 38 | } 39 | }) 40 | .store(in: &subscriptions) 41 | 42 | player.publisher(for: \.timeControlStatus) 43 | .sink { [weak self] status in 44 | switch status { 45 | case .playing: 46 | self?.isPlaying = true 47 | case .paused: 48 | self?.isPlaying = false 49 | case .waitingToPlayAtSpecifiedRate: 50 | break 51 | @unknown default: 52 | break 53 | } 54 | } 55 | .store(in: &subscriptions) 56 | 57 | timeObserver = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: 600), queue: .main) { [weak self] time in 58 | guard let self = self else { return } 59 | if self.isEditingCurrentTime == false { 60 | self.currentTime = time.seconds 61 | } 62 | } 63 | } 64 | 65 | func setCurrentItem(_ item: AVPlayerItem) { 66 | currentTime = .zero 67 | duration = nil 68 | player.replaceCurrentItem(with: item) 69 | 70 | item.publisher(for: \.status) 71 | .filter({ $0 == .readyToPlay }) 72 | .sink(receiveValue: { [weak self] _ in 73 | self?.duration = item.asset.duration.seconds 74 | }) 75 | .store(in: &subscriptions) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/SwiftUIViews/CustomControlsView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomControlsView.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct CustomControlsView: View { 11 | @ObservedObject var playerVM: PlayerViewModel 12 | 13 | var body: some View { 14 | HStack { 15 | if playerVM.isPlaying == false { 16 | Button(action: { 17 | playerVM.player.play() 18 | }, label: { 19 | Image(systemName: "play.circle") 20 | .imageScale(.large) 21 | }) 22 | } else { 23 | Button(action: { 24 | playerVM.player.pause() 25 | }, label: { 26 | Image(systemName: "pause.circle") 27 | .imageScale(.large) 28 | }) 29 | } 30 | 31 | if let duration = playerVM.duration { 32 | Slider(value: $playerVM.currentTime, in: 0...duration, onEditingChanged: { isEditing in 33 | playerVM.isEditingCurrentTime = isEditing 34 | }) 35 | } else { 36 | Spacer() 37 | } 38 | } 39 | .padding() 40 | .background(.thinMaterial) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/SwiftUIViews/CustomPlayerView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomPlayerView.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import SwiftUI 9 | import AVFoundation 10 | 11 | struct CustomPlayerView: View { 12 | @StateObject private var playerVM = PlayerViewModel() 13 | @State private var playlist: [Media] = Media.playlist 14 | 15 | init() { 16 | let audioSession = AVAudioSession.sharedInstance() 17 | do { 18 | try audioSession.setCategory(.playback) 19 | } catch { 20 | print("Setting category to AVAudioSessionCategoryPlayback failed.") 21 | } 22 | } 23 | 24 | var body: some View { 25 | VStack { 26 | VStack { 27 | CustomVideoPlayer(playerVM: playerVM) 28 | .overlay(CustomControlsView(playerVM: playerVM) 29 | , alignment: .bottom) 30 | .clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous)) 31 | } 32 | .padding() 33 | .overlay(playerVM.isInPipMode ? List(playlist) { media in 34 | Button(media.title) { 35 | playerVM.setCurrentItem(media.asPlayerItem) 36 | } 37 | } : nil) 38 | 39 | Button(action: { 40 | withAnimation { 41 | playerVM.isInPipMode.toggle() 42 | } 43 | }, label: { 44 | if playerVM.isInPipMode { 45 | Label("Stop PiP", systemImage: "pip.exit") 46 | } else { 47 | Label("Start PiP", systemImage: "pip.enter") 48 | } 49 | }) 50 | .padding() 51 | } 52 | .padding() 53 | .onAppear { 54 | playerVM.setCurrentItem(playlist.first!.asPlayerItem) 55 | playerVM.player.play() 56 | } 57 | .onDisappear { 58 | playerVM.player.pause() 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/SwiftUIViews/CustomVideoPlayer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomVideoPlayer.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import SwiftUI 9 | import Combine 10 | import AVKit 11 | 12 | struct CustomVideoPlayer: UIViewRepresentable { 13 | @ObservedObject var playerVM: PlayerViewModel 14 | 15 | func makeUIView(context: Context) -> PlayerView { 16 | let view = PlayerView() 17 | view.player = playerVM.player 18 | context.coordinator.setController(view.playerLayer) 19 | return view 20 | } 21 | 22 | func updateUIView(_ uiView: PlayerView, context: Context) { } 23 | 24 | func makeCoordinator() -> Coordinator { 25 | return Coordinator(self) 26 | } 27 | 28 | class Coordinator: NSObject, AVPictureInPictureControllerDelegate { 29 | private let parent: CustomVideoPlayer 30 | private var controller: AVPictureInPictureController? 31 | private var cancellable: AnyCancellable? 32 | 33 | init(_ parent: CustomVideoPlayer) { 34 | self.parent = parent 35 | super.init() 36 | 37 | cancellable = parent.playerVM.$isInPipMode 38 | .sink { [weak self] in 39 | guard let self = self, 40 | let controller = self.controller else { return } 41 | if $0 { 42 | if controller.isPictureInPictureActive == false { 43 | controller.startPictureInPicture() 44 | } 45 | } else if controller.isPictureInPictureActive { 46 | controller.stopPictureInPicture() 47 | } 48 | } 49 | } 50 | 51 | func setController(_ playerLayer: AVPlayerLayer) { 52 | controller = AVPictureInPictureController(playerLayer: playerLayer) 53 | controller?.canStartPictureInPictureAutomaticallyFromInline = true 54 | controller?.delegate = self 55 | } 56 | 57 | func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { 58 | parent.playerVM.isInPipMode = true 59 | } 60 | 61 | func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { 62 | parent.playerVM.isInPipMode = false 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/UIKitViews/PlayerView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PlayerView.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import AVFoundation 9 | import UIKit 10 | 11 | final class PlayerView: UIView { 12 | override static var layerClass: AnyClass { 13 | return AVPlayerLayer.self 14 | } 15 | 16 | var playerLayer: AVPlayerLayer { layer as! AVPlayerLayer } 17 | 18 | var player: AVPlayer? { 19 | get { 20 | playerLayer.player 21 | } 22 | set { 23 | playerLayer.videoGravity = .resizeAspectFill 24 | playerLayer.player = newValue 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /custom-avplayer-swiftui/custom_avplayer_swiftui.swift: -------------------------------------------------------------------------------- 1 | // 2 | // custom_avplayer_swiftui.swift 3 | // custom-avplayer-swiftui 4 | // 5 | // Created by Marco Falanga on 19/11/21. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct custom_avplayer_swiftui: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | NavigationView { 15 | CustomPlayerView() 16 | .navigationTitle("CustomVideoPlayer") 17 | } 18 | } 19 | } 20 | } 21 | --------------------------------------------------------------------------------