# How to set up playback Source: https://www.nianticspatial.com/docs/nsdk/how-to/playback/setting_up_playback/?platform=swift ## Description This guide explains how to set up Playback in a Swift application so you can run NSDK using a prerecorded dataset instead of a live AR session. Playback uses recorded AR session data to simulate device input, allowing you to test and iterate without needing to move a physical device. When a dataset is provided, NSDK runs against the playback session. If no dataset is available, the app falls back to a live ARKit session. ## Prerequisites You will need an Xcode project with the NSDK Swift package integrated. For more information, see [Set up the NSDK in Swift](https://www.nianticspatial.com/docs/nsdk/setup/#set-up-the-nsdk-in-swift). ## Steps To set up Playback in your Swift app, complete the following steps: 1. [Add your dataset to the app bundle](#add-your-dataset-to-the-app-bundle) -- Include the dataset as a folder reference so its structure is preserved. 2. [Load the dataset from the app bundle](#load-the-dataset-from-the-app-bundle) -- Use `BundlePlaybackDatasetLoader` to load the dataset. 3. [Initialize NSDKView with the dataset](#initialize-nsdkview-with-the-dataset) -- Create an `NSDKView` using the dataset or fall back to a live ARKit session. 4. [Use PlaybackSessionDataSource for the NSDK session](#use-playbacksessiondatasource-for-the-nsdk-session) -- Connect the playback session to `NSDKSession`. 5. [Drive the NSDK session from frame callbacks](#drive-the-nsdk-session-from-frame-callbacks) -- Call `nsdkSession.update()` from `NSDKViewDelegate` to process each frame. ### Add your dataset to the app bundle Add your playback dataset folder to your app target as a folder reference so its directory structure is preserved in the app bundle. The dataset folder must contain a `capture.json` file along with the recorded frame images. In Xcode: 1. Select your app target. 2. Open the **Build Phases** tab. 3. Expand **Copy Bundle Resources**. 4. Select **Add Items (+)**. 5. Select **Add Other...** from the bottom of the pop-up window. 6. Choose your dataset folder. 7. Select **Open**. 8. When prompted, select the checkbox next to **Copy items if needed** and the radio button next to **Create folder references**. 9. Select **Finish**. Adding the dataset as a folder reference preserves the directory structure required by `BundlePlaybackDatasetLoader`. ### Load the dataset from the app bundle Use `BundlePlaybackDatasetLoader` to load the dataset by the folder name you added in step 1. This assumes the dataset was added as a folder reference and is preserved as a directory in the app bundle: ```swift let loader = BundlePlaybackDatasetLoader(directory: "YourDatasetFolderName") let dataset = loader.loadDataset() ``` `BundlePlaybackDatasetLoader` looks for the named folder inside the main bundle. It returns `nil` if the folder is not found or the `capture.json` is missing. > **Note:** > > If Xcode flattens the folder contents into the app bundle root, playback may still work if you pass an empty string to `BundlePlaybackDatasetLoader(directory: "")`. However, preserving the dataset as a folder reference is the recommended setup. ### Initialize NSDKView with the dataset Pass the loaded dataset to `NSDKView`. If the dataset is `nil`, `NSDKView` falls back to a live ARKit session: ```swift let arView: NSDKView if let dataset = dataset { arView = NSDKView(dataset: dataset) } else { arView = NSDKView() // live ARKit session } ``` ### Use PlaybackSessionDataSource for the NSDK session When running with a playback dataset, `arView.session` is a `PlaybackSession`. Create a `PlaybackSessionDataSource` and assign it to `NSDKSession.dataSource`. For a live session, use `DefaultSessionDataSource` instead: ```swift let nsdkSession = NSDKSession(accessToken: accessToken, refreshToken: refreshToken, useLidar: ARUtils.isLidarAvailable()) if let playbackSession = arView.session as? PlaybackSession { nsdkSession.dataSource = PlaybackSessionDataSource(session: playbackSession) } else { nsdkSession.dataSource = DefaultSessionDataSource(session: arView.session, orientationReporter: self) } ``` All NSDK features (Depth, Scene Segmentation, Object Detection) work the same as in a live session. ### Drive the NSDK session from frame callbacks Conform your view controller to `NSDKViewDelegate` and call `nsdkSession.update()` from both the live and playback frame callbacks. Implementing both allows the same view controller to handle either mode. ```swift extension MyARViewController: NSDKViewDelegate { // Called on each live ARKit frame func session(_ session: ARSession, didUpdate frame: ARFrame) { nsdkSession?.update() } // Called on each playback frame func playbackSession(_ session: PlaybackSession, didUpdate frame: PlaybackFrame) { nsdkSession?.update() } } ```