gpt4 book ai didi

ios - 如何在spritekit中录制游戏场景

转载 作者:行者123 更新时间:2023-11-28 08:46:26 26 4
gpt4 key购买 nike

我有一个使用 SpriteKit 的项目。我看过 ReplayKit 的 WWDC15 视频。我将 ReplayKit 添加到我的项目中,我想记录我的 GameScene 并在 Facebook 上分享它,在相机胶卷中保存更多……等等。

现在没有任何功能,但我添加了功能并整理了所有内容。

谢谢!

开始录制

func startScreenRecording() {
// Do nothing if screen recording hasn't been enabled.

let sharedRecorder = RPScreenRecorder.sharedRecorder()

// Register as the recorder's delegate to handle errors.
sharedRecorder.delegate = self

sharedRecorder.startRecordingWithMicrophoneEnabled(true) { error in
if let error = error {
self.showScreenRecordingAlert(error.localizedDescription)
}
}
}

停止录制

func stopScreenRecordingWithHandler(handler:(() -> Void)) {
let sharedRecorder = RPScreenRecorder.sharedRecorder()

sharedRecorder.stopRecordingWithHandler { (previewViewController: RPPreviewViewController?, error: NSError?) in
if let error = error {
// If an error has occurred, display an alert to the user.
self.showScreenRecordingAlert(error.localizedDescription)
return
}

if let previewViewController = previewViewController {
// Set delegate to handle view controller dismissal.
previewViewController.previewControllerDelegate = self

/*
Keep a reference to the `previewViewController` to
present when the user presses on preview button.
*/
self.presentViewController(previewViewController, animated: true, completion: nil)
}

handler()
}
}

ReplayKit的其他功能

  func showScreenRecordingAlert(message: String) {
// Pause the scene and un-pause after the alert returns.

GameScene().paused = true

// Show an alert notifying the user that there was an issue with starting or stopping the recorder.
let alertController = UIAlertController(title: "ReplayKit Error", message: message, preferredStyle: .Alert)

let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in

GameScene().paused = false
}
alertController.addAction(alertAction)

/*
`ReplayKit` event handlers may be called on a background queue. Ensure
this alert is presented on the main queue.
*/
dispatch_async(dispatch_get_main_queue()) {
self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}

func discardRecording() {
// When we no longer need the `previewViewController`, tell `ReplayKit` to discard the recording and nil out our reference
RPScreenRecorder.sharedRecorder().discardRecordingWithHandler {
self.previewViewController = nil
}
}

// MARK: RPScreenRecorderDelegate

func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
// Display the error the user to alert them that the recording failed.
showScreenRecordingAlert(error.localizedDescription)

/// Hold onto a reference of the `previewViewController` if not nil.
if previewViewController != nil {
self.previewViewController = previewViewController
}
}

// MARK: RPPreviewViewControllerDelegate

func previewControllerDidFinish(previewController: RPPreviewViewController) {
previewViewController?.dismissViewControllerAnimated(true, completion: nil)
}

游戏场景

import SpriteKit
import ReplayKit

class GameScene: SKScene , SKPhysicsContactDelegate{

func addRecordButton() {

RecordButton = SKSpriteNode(imageNamed:"Record-Off.png")
RecordButton.name = "RecordButton"
RecordButton.position = CGPoint(x: self.size.width/2 + 185, y: self.size.height/2 + 285)
RecordButton.size = CGSizeMake(32,32)
addChild(RecordButton)

}

func Record() {
let recorder = RPScreenRecorder.sharedRecorder()
if recorder.available{
RecordButton.texture = SKTexture(imageNamed: "Record-On.png")
print("Record")
} else {
RecordButton.texture = SKTexture(imageNamed: "Record-Off.png")
print("Stop Record")
}
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {
let location = touch.locationInNode(self)

if {
if (RecordButton.containsPoint(location)){
Record()
}
}

}

最佳答案

class ViewController: UIViewController, RPScreenRecorderDelegate, RPPreviewViewControllerDelegate {

@IBOutlet weak var startRecordingButton: UIButton!
@IBOutlet weak var stopRecordingButton: UIButton!
@IBOutlet weak var activityView: UIActivityIndicatorView!

private let recorder = RPScreenRecorder.sharedRecorder()


override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if SIMULATOR {
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.showSimulatorWarning), userInfo: nil, repeats: false)
return
}
}

override func viewDidLoad() {
super.viewDidLoad()


recorder.delegate = self
activityView.hidden = true
buttonEnabledControl(recorder.recording)
}

@IBAction func startRecordingAction(sender: AnyObject) {
activityView.hidden = false

// start recording
recorder.startRecordingWithMicrophoneEnabled(true) { [unowned self] (error) in
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.activityView.hidden = true
}

if let error = error {
print("Failed start recording: \(error.localizedDescription)")
return
}

print("Start recording")
self.buttonEnabledControl(true)


}
}

@IBAction func stopRecordingAction(sender: AnyObject) {
activityView.hidden = false

//end recording
recorder.stopRecordingWithHandler({ [unowned self] (previewViewController, error) in
dispatch_async(dispatch_get_main_queue()) {
self.activityView.hidden = true
}

self.buttonEnabledControl(false)

if let error = error {
print("Failed stop recording: \(error.localizedDescription)")
return
}

print("Stop recording")
previewViewController?.previewControllerDelegate = self



dispatch_async(dispatch_get_main_queue()) { [unowned self] in
// show preview vindow
self.presentViewController(previewViewController!, animated: true, completion: nil)
}
})
}

//MARK: - Helper
//control the enabled each button
private func buttonEnabledControl(isRecording: Bool) {
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in

let enabledColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
let disabledColor = UIColor.lightGrayColor()

if !self.recorder.available {
self.startRecordingButton.enabled = false
self.startRecordingButton.backgroundColor = disabledColor
self.stopRecordingButton.enabled = false
self.stopRecordingButton.backgroundColor = disabledColor

return
}

self.startRecordingButton.enabled = !isRecording
self.startRecordingButton.backgroundColor = isRecording ? disabledColor : enabledColor
self.stopRecordingButton.enabled = isRecording
self.stopRecordingButton.backgroundColor = isRecording ? enabledColor : disabledColor
}
}


func showSimulatorWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
// let actionCancel = UIAlertAction(title: "cancel", style: .Cancel, handler: nil)

let alert = UIAlertController(title: "ReplayKit不支持模拟器", message: "请使用真机运行这个Demo工程", preferredStyle: .Alert)
alert.addAction(actionOK)
// alert.addAction(actionCancel)
self.presentViewController(alert, animated: true, completion: nil)
}

func showSystemVersionWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
let alert = UIAlertController(title: nil, message: "系统版本需要是iOS9.0及以上才支持ReplayKit", preferredStyle: .Alert)
alert.addAction(actionOK)
self.presentViewController(alert, animated: true, completion: nil)
}



// MARK: - RPScreenRecorderDelegate
// called after stopping the recording
func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
print("Stop Recording ...\n");
}

// called when the recorder availability has changed
func screenRecorderDidChangeAvailability(screenRecorder: RPScreenRecorder) {
let availability = screenRecorder.available
print("Availability: \(availability)\n");
}

// MARK: - RPPreviewViewControllerDelegate
// called when preview is finished
func previewControllerDidFinish(previewController: RPPreviewViewController) {
print("Preview finish");

dispatch_async(dispatch_get_main_queue()) {
[unowned previewController] in
// close preview window
previewController.dismissViewControllerAnimated(true, completion: nil)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

关于ios - 如何在spritekit中录制游戏场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com