Recording a Live Stream

Once the LiveContribution has been created, you are finally ready to stream.

Preparing the Live Recorder

The audio and video live streaming is performed by a LiveRecorder object.

let liveRecorder = LiveRecorder(liveContribution: liveContribution)
liveRecorder.delegate = self
liveRecorder.recordingOrientation = .landscapeRight
liveRecorder.cameraPosition = .back
liveRecorder.prepareToRecord()

The prepareToRecord() method establishes the network connection to the Newzulu Live streaming server, gains access to the device's camera and microphone and starts displaying the video input in the recorder's cameraPreviewView.

If you don't invoke prepareToRecord(), it will be automatically performed the first time you start recording. However, invoking it beforehand allows the recording to start more quickly.

When the prepareToRecord() method is invoked for the first time, the user will be prompted to grant your application access to the device's camera and microphone. Make sure your application's Info.plist contains the NSCameraUsageDescription and NSMicrophoneUsageDescription keys.

Displaying a preview

The live recorder displays a video preview in its cameraPreviewView. You can add this preview view in your application's view hierarchy.

let previewView = liveRecorder.cameraPreviewView
previewView.frame = view.bounds
previewView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
previewView.videoGravity = .aspectFill
previewView.videoOrientation = .landscapeRight
view.addSubview(previewView)

Recording

When the user taps the record button, you invoke the startRecording(onSuccess:onError:) method.

liveRecorder.startRecording(onSuccess: {
    // We are now recording
}, onError: { error in
    // An error occurred.
})

This method generally completes quickly as it starts capturing audio and video immediately without waiting for the connection to be established to the Newzulu Live streaming server.

When the user taps the record button again, you invoke the stopRecording(onSuccess:onError:) method.

liveRecorder.stopRecording(onSuccess: {
    // We are no longer recording
}, onError: { error in
    // We are no longer recording but an error occurred.
})

Stopping the recording could takes some time as the recorder needs to send all pending audio and video to the Newzulu Live streaming server before completing. You should wait until one of the completion block is invoked before releasing the recorder, otherwise pending audio and video will be discarded.

You may stop and restart the recording as many times as you want.

Responding to events

An error may arise and unexpectedly interrupt the recording. If you want to be informed when such an error occurs, you need to provide a delegate to the recorder. That delegate must implement the liveRecorder(_:didStopRecordingWithError:) method.

func liveRecorder(_ liveRecorder: LiveRecorder, didStopRecordingWithError error: NewzuluError) {
    // An error occurred and interrupted the recording.
}

The delegate must also implement the liveRecorderDidChange(_:) method.

func liveRecorderDidChange(_ liveRecorder: LiveRecorder) {
    // The state of the recorder change.
}

That method is invoked when one of the recorder's property changes.

Adapting the orientation

If your view controller supports more than one orientation, you need to update the recorder's recordingOrientation and the camera preview view's orientation when the interface orientation changes.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { context in
        self.updateVideoOrientation()
    })
}

func updateVideoOrientation() -> AVCaptureVideoOrientation {
    let orientation = UIApplication.shared.statusBarOrientation
    // Adapt the preview view and recorder's orientation
}

Currently, LiveRecorder does not support changing its recordingOrientation while it is recording. If you want to prevent the interface orientation to change while recording, you can override the supportedOrientations property in your view controller and return the current orientation when isRecording is true.

Moreover, depending of your target audience, you may consider disabling the .portrait and .portraitUpsideDown orientations.

Finish your contribution

Once you're done recording, you need to finish the Live Contribution.

Next step: Finish your contribution.