如何解决“AddInstanceForFactory:没有为 id 注册工厂”错误?

2024-04-02

我正在学习什么https://github.com/LaiFengiOS/LFLiveKit/blob/master/samples/LFLiveKitSwiftDemo/LFLiveKitSwiftDemo/ViewController.swift https://github.com/LaiFengiOS/LFLiveKit/blob/master/samples/LFLiveKitSwiftDemo/LFLiveKitSwiftDemo/ViewController.swift does.

import UIKit
import LFLiveKit

class ViewController: UIViewController, LFLiveSessionDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()

    session.delegate = self
    session.preView = self.view

    self.requestAccessForAudio()
    self.requestAccessForVideo()
    self.view.backgroundColor = UIColor.clear
    self.view.addSubview(containerView)
    containerView.addSubview(stateLabel)
    containerView.addSubview(closeButton)
    containerView.addSubview(beautyButton)
    containerView.addSubview(cameraButton)
    containerView.addSubview(startLiveButton)

    cameraButton.addTarget(self, action:  #selector(didTappedCameraButton(_:)), for: .touchUpInside)
    beautyButton.addTarget(self, action: #selector(didTappedBeautyButton(_:)), for: .touchUpInside)
    startLiveButton.addTarget(self, action: #selector(didTappedCloseButton(_:)), for: .touchUpInside)
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  func requestAccessForAudio() -> Void {
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
    switch status {
    case AVAuthorizationStatus.notDetermined:
      AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: { ( granted ) in

      })
      break;
    case AVAuthorizationStatus.authorized: break;
    case AVAuthorizationStatus.denied: break;
    case AVAuthorizationStatus.restricted: break;
    default:
      break;
    }
  }

  func requestAccessForVideo() -> Void {
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    switch status {
    case AVAuthorizationStatus.notDetermined:
      AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { ( granted ) in
        if (granted) {
          DispatchQueue.main.async {
            self.session.running = true
          }
        }
      })
      break;
    case AVAuthorizationStatus.authorized:
      self.session.running = true
      break;

    case AVAuthorizationStatus.denied: break;
    case AVAuthorizationStatus.restricted: break;
    default:
      break;
    }
  }

  @objc func didTappedStartLiveButton(_ button: UIButton) -> Void {
    startLiveButton.isSelected = !startLiveButton.isSelected;
    if (startLiveButton.isSelected) {
      startLiveButton.setTitle("End Live", for: UIControl.State())
      let stream = LFLiveStreamInfo()
      stream.url = "rtmp://0.0.0.0:1935"
      session.startLive(stream)
    } else {
      startLiveButton.setTitle("Start Live", for: UIControl.State())
      session.stopLive()
    }
  }

  @objc func didTappedCameraButton(_ button: UIButton) -> Void {
    let devicePosition = session.captureDevicePosition;
    session.captureDevicePosition = (devicePosition == AVCaptureDevice.Position.back) ? AVCaptureDevice.Position.front : AVCaptureDevice.Position.back
  }

  @objc func didTappedBeautyButton(_ button: UIButton) -> Void {
    session.beautyFace = !session.beautyFace;
    beautyButton.isSelected = !session.beautyFace
  }

  @objc func didTappedCloseButton(_ button: UIButton) -> Void {
    // shut down
  }

  // Callback Functions
  func liveSession(_ session: LFLiveSession?, debugInfo: LFLiveDebug?) {
    print("debugInfo: \(String(describing: debugInfo?.currentBandwidth))")
  }

  func liveSession(_ session: LFLiveSession?, errorCode: LFLiveSocketErrorCode) {
    print("errorCode: \(errorCode.rawValue)")
  }

  func liveSession(_ session: LFLiveSession?, liveStateDidChange state: LFLiveState) {
      print("liveStateDidChange: \(state.rawValue)")
      switch state {
      case LFLiveState.ready:
          stateLabel.text = "Not Connected"
          break;
      case LFLiveState.pending:
          stateLabel.text = "Connecting..."
          break;
      case LFLiveState.start:
          stateLabel.text = "Connected"
          break;
      case LFLiveState.error:
          stateLabel.text = "Connection Error"
          break;
      case LFLiveState.stop:
          stateLabel.text = "Not Connected"
          break;
      default:
          break;
      }
  }

  var session: LFLiveSession = {
    let audioConfiguration = LFLiveAudioConfiguration.defaultConfiguration(for: LFLiveAudioQuality.high)
    let videoConfiguration = LFLiveVideoConfiguration.defaultConfiguration(for: LFLiveVideoQuality.low3)
    let session = LFLiveSession(audioConfiguration: audioConfiguration, videoConfiguration: videoConfiguration)
    return session!
  }()

  var containerView: UIView = {
    let containerView = UIView(
      frame: CGRect(
        origin: CGPoint(
          x: 0,
          y: 0
        ), size: CGSize(
          width: UIScreen.main.bounds.width,
          height: UIScreen.main.bounds.height
        )
      )
    )
    containerView.backgroundColor = UIColor.clear
    containerView.autoresizingMask = [
      .flexibleHeight, .flexibleHeight
    ]
    return containerView
  }()

  var stateLabel: UILabel = {
    let stateLabel = UILabel(
      frame: CGRect(
        origin: CGPoint(
          x: 20,
          y: 20
        ),
        size: CGSize(
          width: 80,
          height: 40
        )
      )
    )
    stateLabel.text = "Not Connected"
    stateLabel.textColor = UIColor.white
    stateLabel.font = UIFont.systemFont(ofSize: 14)
    return stateLabel
  }()

  var closeButton: UIButton = {
    let closeButton = UIButton(
      frame: CGRect(
        origin: CGPoint(
          x: UIScreen.main.bounds.width - 10 - 44,
          y: 20
        ),
        size: CGSize(
          width: 44,
          height: 44
        )
      )
    )
    closeButton.backgroundColor = UIColor.red
    return closeButton
  }()

  var cameraButton: UIButton = {
    let cameraButton = UIButton(
      frame: CGRect(
        origin: CGPoint(
          x: UIScreen.main.bounds.width - 54 * 2,
          y: 20
        ),
        size: CGSize(
          width: 44,
          height: 44
        )
      )
    )
    cameraButton.backgroundColor = UIColor.blue
    return cameraButton
  }()

  var beautyButton: UIButton = {
    let beautyButton = UIButton(
      frame: CGRect(
        origin: CGPoint(
          x: UIScreen.main.bounds.width - 54 * 3,
          y: 20
        ),
        size: CGSize(
          width: 44,
          height: 44
        )
      )
    )
    beautyButton.backgroundColor = UIColor.green
    return beautyButton
  }()

  var startLiveButton: UIButton = {
    let startLiveButton = UIButton(
      frame: CGRect(
        origin: CGPoint(
          x: 30,
          y: UIScreen.main.bounds.height - 50
        ),
        size: CGSize(
          width: UIScreen.main.bounds.width - 10 - 44,
          height: 44
        )
      )
    )
    startLiveButton.layer.cornerRadius = 22
    startLiveButton.setTitleColor(UIColor.black, for: UIControl.State())
    startLiveButton.setTitle("Start Streaming", for: UIControl.State())
    return startLiveButton
  }()
}

这是我写的,没有用。

AddInstanceForFactory: No factory registered for id <CFUUID 0x600001bef9c0> F8BB1C28-BAE8-11D6-9C31-00039315CD46

启动应用程序后,很快就会出现错误。

我查过Swift 5.1 错误:[插件] AddInstanceForFactory:没有为 id ,但我没有使用AVAudioPlayer https://stackoverflow.com/questions/58360765/swift-5-1-error-plugin-addinstanceforfactory-no-factory-registered-for-id-c

我该如何修复它?


“AddInstanceForFactory:没有为 id 注册工厂”

根据我的经验,该消息仅表明正在使用模拟器。据推测,模拟器没有工厂注册的 ID。当在真实设备上运行时,该消息就会消失。该消息在使用 AVFoundation 框架时出现,并且似乎与任何崩溃或其他问题没有直接关系。

尝试在真实设备上运行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解决“AddInstanceForFactory:没有为 id 注册工厂”错误? 的相关文章

随机推荐