无法在flutter中使用音频进行ios开发
这是我的代码: pubspec.yaml(使用audioplayers:^0.7.8插件来播放音频)
name: play_audio
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
# lib for adding sound
audioplayers: ^0.7.8
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/sound/correct_answer.mp3
- assets/sound/wrong_answer.mp3
play_audio.dart 文件 导入audio_cache.dart文件来播放音频。我已将音频文件导入到项目的 Assets 文件夹中。 相同的代码在 Android 设备中运行良好
import 'package:audioplayers/audio_cache.dart';
import "package:flutter/material.dart";
// audio file path
const correctSoundPath = "sound/correct_answer.mp3";
const inCorrectSoundPath = "sound/wrong_answer.mp3";
class PlayAudio extends StatefulWidget {
_PlayAudioState createState() => _PlayAudioState();
}
class _PlayAudioState extends State<PlayAudio> {
AudioCache player;
@override
void initState() {
// TODO: implement initState
super.initState();
player = AudioCache(); // initialising audio cache
debugPrint("inside initState");
}
Future _playCorrect() async {
player.play(correctSoundPath);
debugPrint("inside _playCorrect()");
}
Future _playIncorrect() async {
player.play(inCorrectSoundPath);
debugPrint("inside _playIncorrect()");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// correct button
MaterialButton(
child: Text("Correct"),
onPressed: () {
_playCorrect();
},
),
// incorrect button
MaterialButton(
child: Text("Incorrect"),
onPressed: () {
_playIncorrect();
},
)
],
),
);
}
}
使用 flutter run 在 iOS 设备中运行此项目时出现以下错误:
Error output from Xcode build:
** BUILD FAILED **
Xcode's output:
=== BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Debug ===
Debug.xcconfig line 1: Unable to find included file "Pods/Target Support
Files/Pods-Runner/Pods-Runner.debug.xcconfig"
Debug.xcconfig line 1: Unable to find included file "Pods/Target Support
Files/Pods-Runner/Pods-Runner.debug.xcconfig"
=== BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Debug ===
/Users/quiz/Documents/Project/Flutter_Project/TODO_Flutter_Projects/Quiz/multiple_choice_ios/ios/Runner/Genera
tedPluginRegistrant.m:6:9: fatal error: 'audioplayers/AudioplayersPlugin.h' file not found
#import <audioplayers/AudioplayersPlugin.h>
上面的代码在不使用音频文件的情况下运行正常。如果我有什么需要补充的,请告诉我,我正在使用 Microsoft Visual Studio Code 作为我的 IDE
最佳答案
我也遇到了这个问题,只需要安装 CocoaPods。
brew install cocoapods
pod setup
有一个错误表明您需要执行此操作,但它隐藏在构建输出中。
关于ios - 在 flutter 中找不到“audioplayers/AudioplayersPlugin.h”文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54397063/