You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.73.6集成react-native-video iOS构建失败:找不到RCTEventDispatcher

解决React Native 0.73.6 + react-native-video 6.5.0 iOS构建报错:Cannot find type 'RCTEventDispatcher' in scope

问题原因

React Native 0.70+版本中,RCTEventDispatcher已被废弃并移除,而react-native-video ^6.5.0是适配旧版RN的版本,未兼容RN 0.73的API变更,导致构建时找不到该类型。

解决方案

方案一:升级react-native-video到兼容版本

这是最稳妥的解决方式,直接升级到支持RN 0.73的版本(推荐7.x及以上):

  1. 卸载旧版本:
npm uninstall react-native-video
  1. 安装最新兼容版本:
npm install react-native-video@latest
  1. 重新安装Pods:
cd ios && pod install && cd ..
  1. 清理缓存后重新编译:
# 清理Xcode缓存
rm -rf ~/Library/Developer/Xcode/DerivedData
# 清理Metro缓存
npx react-native start --reset-cache

方案二:手动修改react-native-video原生代码(不推荐,适配成本高)

如果暂时无法升级版本,可手动修改react-native-video的iOS原生代码替换废弃API:

  1. 打开node_modules/react-native-video/ios/Classes/RCTVideo.swift文件
  2. 替换所有RCTEventDispatcher相关代码:
    • 将属性声明private var _eventDispatcher: RCTEventDispatcher?替换为:
      private var _eventEmitter: RCTEventEmitter?
      
    • 修改初始化方法:
      init(eventEmitter: RCTEventEmitter!) {
          super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
          ReactNativeVideoManager.shared.registerView(newInstance: self)
          #if USE_GOOGLE_IMA
              _imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled)
          #endif
      
          _eventEmitter = eventEmitter
      
          #if os(iOS)
              if _pictureInPictureEnabled {
                  initPictureinPicture()
                  _playerViewController?.allowsPictureInPicturePlayback = true
              } else {
                  _playerViewController?.allowsPictureInPicturePlayback = false
              }
          #endif
      }
      
    • 找到原代码中_eventDispatcher?.sendEvent(withName: ...)的调用,替换为:
      _eventEmitter?.sendEvent(withName: <事件名>, body: <事件参数>)
      
  3. 修改RCTVideoManager.swift中对应的初始化调用,将传入的eventDispatcher改为eventEmitter
  4. 重新安装Pods并编译

注:手动修改node_modules代码存在后续npm install被覆盖的风险,建议配合patch-package工具保存修改:

# 安装patch-package
npm install patch-package --save-dev
# 修改代码后生成补丁
npx patch-package react-native-video

内容的提问来源于stack exchange,提问作者Andrii Spivak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:02:18