Flutter集成Record 4.4.4包时执行pod install报错求助
问题场景
在MacOS Monterey 12.6.3系统上,使用Flutter 3.7.3稳定版运行录音功能代码时,出现CocoaPods依赖安装失败的错误。
运行的Flutter代码
void main() { runApp(const MainApp()); } class MainApp extends StatelessWidget { const MainApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: VoiceApp(key: Key('key'), title: 'MyVoice app'), ); } } class VoiceApp extends StatefulWidget { const VoiceApp({super.key, required this.title}); final String title; @override State<VoiceApp> createState() => VoiceAppState(); } class VoiceAppState extends State<VoiceApp> { String? name; final recorder = Record(); startRecording() async { final record = Record(); // 检查并请求权限 if (await record.hasPermission()) { // 开始录音 await record.start( path: 'aFullPath/myFile.m4a', encoder: AudioEncoder.aacLc, // 默认值 bitRate: 128000, // 默认值 samplingRate: 44100, // 默认值 ); } } @override Widget build(BuildContext context) { return Scaffold( body: Column(children: [ ElevatedButton( onPressed: startRecording(), // 存在问题:组件构建时直接执行函数,而非点击触发 child: const Text('Start'), ) ])); } }
报错信息
Launching lib/main.dart on macOS in debug mode... CocoaPods' output: Preparing Analyzing dependencies Inspecting targets to integrate Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``) Finding Podfile changes A record_macos R path_provider_foundation - FlutterMacOS Fetching external sources -> Fetching podspec for `FlutterMacOS` from `Flutter/ephemeral` -> Fetching podspec for `record_macos` from `Flutter/ephemeral/.symlinks/plugins/record_macos/macos` Resolving dependencies of `Podfile` CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is only performed in repo update [!] CocoaPods could not find compatible versions for pod "record_macos": In Podfile: record_macos (from `Flutter/ephemeral/.symlinks/plugins/record_macos/macos`) Specs satisfying the `record_macos (from `Flutter/ephemeral/.symlinks/plugins/record_macos/macos`)` dependency were found, but they required a higher minimum deployment target. /Library/Ruby/Gems/2.6.0/gems/molinillo- 0.8.0/lib/molinillo/resolution.rb:317:in `raise_error_unless_state' /Library/Ruby/Gems/2.6.0/gems/molinillo- 0.8.0/lib/molinillo/resolution.rb:299:in `block in unwind_for_conflict' /Library/Ruby/Gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:297:in `tap' /Library/Ruby/Gems/2.6.0/gems/molinillo- 0.8.0/lib/molinillo/resolution.rb:297:in `unwind_for_conflict' /Library/Ruby/Gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:682:in `attempt_to_activate' /Library/Ruby/Gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:254:in `process_topmost_state' /Library/Ruby/Gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:182:in `resolve' /Library/Ruby/Gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolver.rb:43:in `resolve' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:94:in `resolve' /Library/Ruby/Gems/2.6.0/gems/cocoapods- 1.11.3/lib/cocoapods/installer/analyzer.rb:1078:in `block in resolve_dependencies' /Library/Ruby/Gems/2.6.0/gems/cocoapods- 1.11.3/lib/cocoapods/user_interface.rb:64:in `section' /Library/Ruby/Gems/2.6.0/gems/cocoapods- 1.11.3/lib/cocoapods/installer/analyzer.rb:1076:in `resolve_dependencies' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/installer/analyzer.rb:124:in `analyze' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:416:in `analyze' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:241:in `block in resolve_dependencies' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/user_interface.rb:64:in `section' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:240:in `resolve_dependencies' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:161:in `install!' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command/install.rb:52:in `run' /Library/Ruby/Gems/2.6.0/gems/claide-1.0.3/lib/claide/command.rb:334:in `run' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command.rb:52:in `run' /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/bin/pod:55:in `<top (required)>' /usr/local/bin/pod:23:in `load' /usr/local/bin/pod:23:in `<main>' Exception: Error running pod install Exited
解决方案
1. 解决CocoaPods依赖兼容问题
错误核心是record_macos组件需要更高的macOS最低部署版本,操作步骤:
- 打开项目内的
macos/Podfile文件 - 将
platform :osx, '10.15'(或当前版本)修改为更高版本,比如platform :osx, '11.0'(可查看record_macos的podspec文件确认具体要求) - 执行以下命令重新安装依赖:
cd macos pod deintegrate pod install cd .. flutter clean flutter run
2. 修复按钮点击逻辑
原代码中ElevatedButton的onPressed直接调用startRecording(),会导致组件构建时就执行录音函数,而非点击按钮触发。需修改为传递函数引用:
ElevatedButton( onPressed: startRecording, // 移除括号 child: const Text('Start'), )
3. 优化录音路径
原代码使用固定路径'aFullPath/myFile.m4a'会导致路径不存在问题,建议用path_provider获取合法存储路径:
- 在
pubspec.yaml添加依赖:
dependencies: path_provider: ^2.0.15
- 修改
startRecording函数:
import 'package:path_provider/path_provider.dart'; // ... startRecording() async { final record = Record(); if (await record.hasPermission()) { // 获取应用文档目录 final directory = await getApplicationDocumentsDirectory(); final path = '${directory.path}/myFile.m4a'; await record.start( path: path, encoder: AudioEncoder.aacLc, bitRate: 128000, samplingRate: 44100, ); } }
内容的提问来源于stack exchange,提问作者Tusshu
相关产品推荐
相关产品推荐

