Swifty360Player

repository·master·Indexed 41 days ago

https://github.com/abdullahselek/swifty360player

An iOS library for streaming and playing 360-degree videos using AVPlayer. It supports motion-based orientation and provides integration options via Swifty360ViewController and Swifty360View. Version 0.2.7 requires iOS 11.0 or higher and Swift 5.x.

Tokens
1.3K
Snippets
6
Records
7
Agent score
2%

What's inside Swifty360Player

  1. Install Swifty360Player via Swift Package Manager

    master

    Add the following dependency to your Package.swift file:

    .package(url: "https://github.com/abdullahselek/Swifty360Player.git", from: "0.2.7")

    Then, run swift package resolve to download and resolve the package.

    .package(url: "https://github.com/abdullahselek/Swifty360Player.git", from: "0.2.7")
  2. Install Swifty360Player via CocoaPods

    master

    To integrate Swifty360Player using CocoaPods, add the following to your Podfile. Ensure your platform is set to iOS 11.0 or higher to match version 0.2.7 requirements.

    1. Add the pod to your target:
    target '<Your Target Name>' do
        pod 'Swifty360Player', '0.2.7'
    end
    1. Run pod install in your terminal.
    target '<Your Target Name>' do
        pod 'Swifty360Player', '0.2.7'
    end
  3. Integrate Swifty360Player using Swifty360ViewController

    master

    You can embed the 360-degree player by initializing a Swifty360ViewController with an AVPlayer instance and a Swifty360MotionManager.shared instance.

    Programmatic Integration

    Use addChildViewController to embed the player view controller into your existing view controller hierarchy.

    let videoURL = URL(fileURLWithPath: Bundle.main.path(forResource: "google-help-vr", ofType: "mp4")!)
    let player = AVPlayer(url: videoURL)
    
    let motionManager = Swifty360MotionManager.shared
    let swifty360ViewController = Swifty360ViewController(withAVPlayer: player, motionManager: motionManager)
    
    addChildViewController(swifty360ViewController)
    view.addSubview(swifty360ViewController.view)
    swifty360ViewController.didMove(toParentViewController: self)
    
    player.play()

    Storyboard Integration

    1. Create a subclass of Swifty360ViewController.
    2. In your parent view controller, instantiate the subclass via storyboard.
    3. Assign the player and motionManager properties.
    // In your parent VC
    guard let swifty360ViewController = self.storyboard?.instantiateViewController(withIdentifier: "TestViewController") as? TestViewController else { return }
    
    let videoURL = URL(fileURLWithPath: Bundle.main.path(forResource: "google-help-vr", ofType: "mp4")!)
    let player = AVPlayer(url: videoURL)
    let motionManager = Swifty360MotionManager.shared
    
    swifty360ViewController.player = player
    swifty360ViewController.motionManager = motionManager
    self.present(swifty360ViewController, animated: true, completion: nil)
    
    // The subclass implementation
    import UIKit
    import Swifty360Player
    
    class TestViewController: Swifty360ViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            player.play()
        }
    }
    let motionManager = Swifty360MotionManager.shared
    let swifty360ViewController = Swifty360ViewController(withAVPlayer: player, motionManager: motionManager)
    
    addChildViewController(swifty360ViewController)
    view.addSubview(swifty360ViewController.view)
    swifty360ViewController.didMove(toParentViewController: self)
  4. Integrate Swifty360Player using Swifty360View

    master

    For a more lightweight integration, use Swifty360View directly.

    Programmatic Integration

    Initialize the view with a frame, player, and motion manager, then call .setup(player:motionManager:).

    let videoURL = URL(fileURLWithPath: Bundle.main.path(forResource: "google-help-vr", ofType: "mp4")!)
    let player = AVPlayer(url: videoURL)
    let motionManager = Swifty360MotionManager.shared
    
    let swifty360View = Swifty360View(withFrame: view.bounds, player: player, motionManager: motionManager)
    swifty360View.setup(player: player, motionManager: motionManager)
    view.addSubview(swifty360View)
    
    player.play()

    Storyboard Integration

    1. Add a UIView to your view controller.
    2. Change its class to Swifty360View.
    3. Connect via IBOutlets.
    4. Call .setup(player:motionManager:) in your code.
    let swifty360View = Swifty360View(withFrame: view.bounds, player: player, motionManager: motionManager)
    swifty360View.setup(player: player, motionManager: motionManager)
    view.addSubview(swifty360View)
  5. Reorient camera angle to horizon

    master

    You can reorient the vertical camera angle to the horizon using the reorientVerticalCameraAngle(animated:) method available on both Swifty360ViewController and Swifty360View. This is typically triggered via a gesture recognizer.

    // Add a tap gesture to your view
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(reorientVerticalCameraAngle))
    view.addGestureRecognizer(tapGestureRecognizer)
    
    // Implementation
    @objc func reorientVerticalCameraAngle() {
        // If using Swifty360ViewController:
        swifty360ViewController.reorientVerticalCameraAngleToHorizon(animated: true)
        
        // OR if using Swifty360View:
        swifty360View.reorientVerticalCameraAngleToHorizon(animated: true)
    }
    @objc func reorientVerticalCameraAngle() {
        swifty360ViewController.reorientVerticalCameraAngleToHorizon(animated: true)
    }