SynqSwift 0.2.2

SynqSwift 0.2.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Kjartan Vestvik.



 
Depends on:
Alamofire~> 4.4
SynqHttpLib~> 0.4
 

SynqSwift 0.2.2

SynqSwift

Build Status

This is the SYNQ mobile SDK for iOS, written in Swift 3. It lets you easily integrate your mobile app with the SYNQ platform and the SYNQ Video API.

Example

To run the example project, clone the repo, and run pod install from the Example directory first. The example project features an app that utilizes the features of the SDK, and this shows how parts of the SDK are to be used.

Installation

SynqSwift is available through CocoaPods. If you do not have CocoaPods installed, you can install it with the following command:

$ gem install cocoapods

To integrate SynqSwift into your Xcode project, specify it in your Podfile:

pod "SynqSwift"

Then run the following commang to install:

$ pod install

If you get an error saying [!] Unable to find a specification for <name of pod>, try running pod repo update, and then run pod install again. This should fix it.

SynqUploader

This part consists of classes for fetching videos from the Photos library, exporting and uploading them to SYNQ. The SDK uses Alamofire for communicating with the server. It utilizes a background configured session manager to manage video uploads. This makes the upload continue regardless of whether the app is in the foreground or background.

Import SynqSwift

import SynqSwift

Init an instance of SynqUploader and set SQVideoUploadDelegate to be able to handle upload results

let uploader = SynqUploader()
uploader.delegate = self

Create a SQVideoUpload object for each PHAsset to be uploaded

let aVideo = SQVideoUpload.init(asset: anAsset)

Set upload parameters for the SQVideoUpload object

To do this, you must do two things. 1: Create a video object in the SYNQ API, and 2: Fetch upload parameters from the API for the created video object.

In the example project, the SynqHttpLib pod and the example server (SYNQ-Nodejs-example-server) performs these two functions in one step via the function “createVideoAndGetParamsWithSuccess:”

video.uploadParameters = uploadParams

Add each SQVideoUpload object to an array and call the upload function

uploader.uploadVideoArray(videos: videoArray, exportProgress:
{ progress in
    // Report export progress to UI
    self.progressView.progress = Float(progress)

}, uploadProgress: { progress in
    // We need progress between 0 and 1, so must divide percent by 100
    let progressBelowOne = progress / 100.0;

    // Report upload progress to UI
    self.progressView.progress = Float(progressBelowOne)
})

SQVideoUploadDelegate

The outcome of each upload is reported through the SQVideoUploadDelegate methods. These are the methods that are available, and how they should be used:

func videoUploadComplete(video: SQVideoUpload)

A video is successfully uploaded.

func videoUploadFailed(video: SQVideoUpload)

There was an error uploading a video.

func allVideosUploadedSuccessfully()

All videos were successfully uploaded.

Play uploaded videos

The example project included in this repo contains functionality for playing back your uploaded videos. This consists of a table view that lists all uploaded videos. Selecting one of the videos will open a new view controller (an instance of AVPlayerViewController) with a video player configured to play the selected video. This example uses the HLS output format as source for the video playback. The various sources for video playback can be found under the “outputs” field of the video object:

"outputs": {
  "hls": {
    "url": "https://multicdn.synq.fm/projects/fb/ec/fbec62099ed94d7ba7692c7353d20435/derivatives/videos/0c/19/0c19b46991ae49be994cec9f3909329a/hls/0c19b46991ae49be994cec9f3909329a_hls.m3u8",
    "state": "complete"
  },
  "mp4_360": {
    "url": "https://multicdn.synq.fm/projects/fb/ec/fbec62099ed94d7ba7692c7353d20435/derivatives/videos/0c/19/0c19b46991ae49be994cec9f3909329a/mp4_360/0c19b46991ae49be994cec9f3909329a_mp4_360.mp4",
    "state": "complete"
  },
  "mp4_720": {
    "url": "https://multicdn.synq.fm/projects/fb/ec/fbec62099ed94d7ba7692c7353d20435/derivatives/videos/0c/19/0c19b46991ae49be994cec9f3909329a/mp4_720/0c19b46991ae49be994cec9f3909329a_mp4_720.mp4",
    "state": "complete"
  },
  "mp4_1080": {
    "url": "https://multicdn.synq.fm/projects/fb/ec/fbec62099ed94d7ba7692c7353d20435/derivatives/videos/0c/19/0c19b46991ae49be994cec9f3909329a/mp4_1080/0c19b46991ae49be994cec9f3909329a_mp4_1080.mp4",
    "state": "complete"
  },
  "webm_720": {
    "url": "https://multicdn.synq.fm/projects/fb/ec/fbec62099ed94d7ba7692c7353d20435/derivatives/videos/0c/19/0c19b46991ae49be994cec9f3909329a/webm_720/0c19b46991ae49be994cec9f3909329a_webm_720.webm",
    "state": "complete"
  }
}

Please note: the “url” field is only present when the state is “complete”, i.e. when the transcoding of the video file is finished. The state might also read “submitted” or “progressing”, meaning that the transcoding is not complete yet, hence no output url.

Having obtained the URL for the needed output format, presenting the video player view is accomplished by configuring an instance of the AVPlayerViewController:

// Convert url string to URL
let videoUrlString    // the url string fetched from the video object JSON
let videoUrl = URL(string: videoUrlString)

// Configure AVPlayerViewController with an AVPlayer
let avPlayerViewController = AVPlayerViewController()
avPlayerViewController.player = AVPlayer(url: videoUrl!)

// Present the avplayer view controller
self.present(avPlayerViewController, animated: true) {
    avPlayerViewController.player?.play()
}

Important note

To use our API, you need to have an API key. Each API key corresponds to a project, and a project is a collection of videos, and settings regarding those videos, such as webhooks. Register for a free API key. Your app should never communicate directly with our API, you should have your own server and use your API key as a secret key from there. Your server should then authenticate requests from your app before sending http calls to SYNQ. If you do not have any server configured to access the SYNQ API, you can use our NodeJS example server as a way to get started. Please note that this server should never be used in production!

Requirements

This SDK requires iOS 8 or above

Author

Kjartan Vestvik, [email protected]

License

SynqSwift is available under the MIT license. See the LICENSE file for more info.