在flutter中找不到'audioplayers / AudioplayersPlugin.h'文件

时间:2019-01-28 07:09:43

标签: ios swift audio dart flutter

无法在flutter中使用音频进行ios开发

这是我的代码: pubspec.yaml(使用音频播放器:用于播放音频的^ 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文件以播放音频。我已将音频文件导入到项目的资产文件夹中。 相同的代码可以在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代码用作我的IDE

1 个答案:

答案 0 :(得分:1)

我也遇到了这个问题,只需要安装CocoaPods。

brew install cocoapods
pod setup

有一个错误指示您需要执行此操作,但该错误已隐藏在构建输出中。