您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 9 ReplayKit 螢幕錄影實作:Swift 版本完整程式碼

編輯:iOS 開發綜合

iOS 9 帶來的 ReplayKit 讓 App 內錄製螢幕畫面變得非常簡單,API 設計相當直覺。以下用 Swift 示範一個可重複使用的錄影代理類(Objective-C 的寫法大同小異,這裡不另外列出)。

ReplayKitProxy 完整實作

// ReplayKitProxy.swift
// ReplayKitDemo
//
// Created by LiuYanghui on 15/12/11.
// Copyright © 2015年 LiuYanghui. All rights reserved.
//

import UIKit
import ReplayKit

class ReplayKitProxy: NSObject, RPPreviewViewControllerDelegate {

    private var rootViewController: UIViewController?
    private var previewViewController: RPPreviewViewController?

    // 單例
    class var sharedInstance: ReplayKitProxy {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: ReplayKitProxy? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = ReplayKitProxy()
            Static.instance?.initData()
        }
        return Static.instance!
    }

    private func initData() {
    }

    /// 開始錄製
    func startRecording() {
        let recorder = RPScreenRecorder.sharedRecorder()

        if recorder.available == false {
            print("Replaykit is not available")
            return
        }
        if recorder.recording == true {
            print("Replaykit is recording")
            return
        }

        recorder.startRecordingWithMicrophoneEnabled(true) { (error) -> Void in
            if error != nil {
                print(error?.localizedDescription)
            }
        }
    }

    /// 停止錄製
    func stopRecording() {
        let recorder = RPScreenRecorder.sharedRecorder()
        if recorder.recording == false {
            return
        }

        recorder.stopRecordingWithHandler { (previewViewController, error) -> Void in
            if error != nil {
                print(error?.localizedDescription)
            } else if let viewController = previewViewController {
                viewController.previewControllerDelegate = self
                self.previewViewController = viewController
            }
        }
    }

    /// 刪除已錄製的影片,必須在 stopRecording 之後呼叫
    func discardRecording() {
        let recorder = RPScreenRecorder.sharedRecorder()
        if recorder.recording == true {
            return
        }
        recorder.discardRecordingWithHandler { () -> Void in
            print("discardRecording complete")
        }
    }

    /// 播放預覽
    func displayRecordingContent() {
        rootViewController?.presentViewController(self.previewViewController!,
                                                  animated: true,
                                                  completion: { () -> Void in
            print("display complete")
        })
    }

    // MARK: - RPPreviewViewControllerDelegate
    func previewControllerDidFinish(previewController: RPPreviewViewController) {
        previewController.dismissViewControllerAnimated(true) { () -> Void in }
    }
}

使用上的幾個重點

先檢查可用性。RPScreenRecorder.sharedRecorder().available 必須為 true 才能錄製,模擬器與部分不支援的情境會回傳 false。

麥克風的權限。startRecordingWithMicrophoneEnabled(true) 會開啟麥克風收音,首次使用時系統會跳出權限詢問;若不需要錄旁白,傳 false 即可。

刪除的時機。discardRecording() 一定要在停止錄製之後呼叫,否則不會生效。

預覽畫面的 delegate。記得實作 RPPreviewViewControllerDelegate,否則使用者按下分享或關閉按鈕時畫面不會正確消失。若要進一步分享成片,可以在這個 delegate 中搭配 UIActivityViewController 或 RPActivityViewController 輸出到相簿。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved