Flutter播放声音和记录噪音级别。

5

请帮忙!

我遇到这个问题已经有一段时间了。该应用在iOS模拟器以及Android设备上运行良好,但在iOS设备上却不能正常运行。我的想法是要播放一个音调,一旦音调开始播放,我就需要记录设备所捕捉到的分贝级别。据我所知,iOS设备只允许同时使用一个通道,无论是输入还是输出。我正在使用audioplayers和noise_meter插件。所有权限都已被授予,如果我单独运行一个方法它能够工作,反之亦然,但当同时运行两种方法时,它就会抛出以下错误:

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)\""

我已经做了一个简单的应用程序来演示我的问题:

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.
    );
  }
}

非常感谢任何帮助。

1个回答

0

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

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

查看关于“本地网络隐私权限”的文档。从那里开始,建议按照说明启用您的应用程序Debug版本中的Dart多播DNS服务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接