Flutter 播放声音并记录噪音级别

Don*_*ger 5 noise avaudioplayer decibel flutter

有人可以帮忙吗?

我遇到这个问题已经有一段时间了。该应用程序在 ios 模拟器以及 Android 设备上运行良好,但不能在 ios 设备上运行。总体思路是,我想要播放一个音调,一旦该音调开始播放,我需要记录设备拾取的分贝级别。据我了解,ios设备一次只允许使用一个通道,无论是输入还是输出。我正在使用音频播放器和噪音计插件。所有权限均已授予,如果我单独运行一种方法,它可以工作,反之亦然,但当运行两种方法时,它会抛出此错误。

https://pub.dev/packages/noise_meter

https://pub.dev/packages/audioplayers

我收到的错误如下:

"iOS => call startHeadlessService, playerId fa642ed8-2266-48b6-ac9f-1f474f225864"
"calling start headless service (\n    4430477584143042153\n)"
"iOS => call setReleaseMode, playerId fa642ed8-2266-48b6-ac9f-1f474f225864"
"iOS => call play, playerId fa642ed8-2266-48b6-ac9f-1f474f225864"
"setUrl /var/mobile/Containers/Data/Application/0E0D63C7-B1DD-4BD3-BC84-2AFB5C80B8D9/Library/Caches/2100.mp3"
[VERBOSE-2:FlutterObservatoryPublisher.mm(101)] Failed to register observatory port with mDNS with error -65555.
[VERBOSE-2:FlutterObservatoryPublisher.mm(103)] On iOS 14+, local network broadcast in apps need to be declared in the app's Info.plist. Debug and profile Flutter apps and modules host VM services on the local network to support debugging features such as hot reload and DevTools. To make your Flutter app or module attachable and debuggable, add a '_dartobservatory._tcp' value to the 'NSBonjourServices' key in your Info.plist for the Debug/Profile configurations. For more information, see https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions
""
"iOS -> updateDuration...1.541224"
"iOS -> invokechannel"
"iOS -> onSoundComplete..."
[avas]     AVAudioSession_iOS.mm:1149  Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
"Error inactivating audio session Error Domain=NSOSStatusErrorDomain Code=560030580 \"(null)\""
Run Code Online (Sandbox Code Playgroud)

我整理了一个简单的应用程序来仅仅演示我的问题:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:flutter/services.dart';
import 'package:noise_meter/noise_meter.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);



  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static AudioCache player = AudioCache();

  bool _isRecording = false;
  StreamSubscription<NoiseReading> _noiseSubscription;
  NoiseMeter _noiseMeter;

  playSound() async{
    player.play("2100.mp3");
  }


  void start() async {
    //_noiseSubscription = _noiseMeter.noiseStream.listen(onData);

    try {
      _noiseSubscription = _noiseMeter.noiseStream.listen(onData);
    } catch (exception) {
      print(exception);
    }
  }

  void onData(NoiseReading noiseReading) {
    this.setState(() {
      if (!this._isRecording) {
        this._isRecording = true;
      }
    });
    /// Do someting with the noiseReading object
    print(noiseReading.toString());
  }

  void onError(PlatformException e) {
    print(e.toString());
    _isRecording = false;
  }

  void stopRecorder() async {
    try {
      if (_noiseSubscription != null) {
        _noiseSubscription.cancel();
        _noiseSubscription = null;
      }
      this.setState(() {
        this._isRecording = false;
      });
    } catch (err) {
      print('stopRecorder error: $err');
    }
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //start();
    _noiseMeter = new NoiseMeter(onError);
  }



  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),

          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
        // player.play("2100.mp3");
        //
        // player.clearCache();
          playSound();
          start();
        },
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽。

Mαπ*_*π.0 0

这似乎是 iOS 14+ 设备的一个已知错误。

在 iOS 14 及更高版本上,在应用程序的调试版本中启用 Dart 多播 DNS 服务,以添加调试功能,例如热重载和通过 flutter Attach 的 DevTools。

查看有关“本地网络隐私权限”的文档。从那里,我建议按照有关在应用程序的调试版本中启用 Dart 多播 DNS 服务的说明进行操作。