English 中文(简体)
• 视听服务——cacheManager = = 无效:在重新启用之后不真实
原标题:Audio_service _cacheManager == null : is not true after app restart

我需要重新开始使用音像服务。 当用户签字时,应当清除海滩并重新启用。 在试图重新启用音响服务之后,我发现了一个错误:

flutter:  package:audio_service/audio_service.dart : Failed assertion: line 993 pos 12:  _cacheManager == null : is not true.

因此,无法启动音像服务。

After splash screen on the player screen we can start/pause audio and restart application. To restart app from player screen on button tap I am doing:

 FilledButton(
                onPressed: () {
                  BlocProvider.of<PlayerCubit>(context).clearCache();
                  RestartWidget.restartApp(context);
                },
                child: const Text( Restart App ),
              ),

其他法典:

Future<void> runAudioApp() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  await _initServices();

  runApp(
    const RestartWidget(
      onRestart: _initServices,
      child: AudioApp(),
    ),
  );
}

Future<void> _initServices() async {
  print( RestartWidget _initServices() );
  await di.init();
}

地点

class RestartWidget extends StatefulWidget {
  const RestartWidget({
    required this.onRestart,
    required this.child,
    super.key,
  });

  final Widget child;
  final void Function() onRestart;

  static void restartApp(BuildContext context) {
    context.findAncestorStateOfType<_RestartWidgetState>()?.restartApp();
  }

  @override
  State<RestartWidget> createState() => _RestartWidgetState();
}

class _RestartWidgetState extends State<RestartWidget> {
  Key key = UniqueKey();

  void restartApp() {
    setState(() {
      widget.onRestart.call();
      key = UniqueKey();
    });
  }

  @override
  Widget build(BuildContext context) => KeyedSubtree(
        key: key,
        child: widget.child,
      );
}

injection_container contain:

final sl = GetIt.instance;

Future<void> init() async {
  await sl.reset();
  try {
    sl.registerSingleton(AppAudioCache());
    if (!sl.isRegistered<AppAudioHandler>()) {
      try {
        sl.registerSingleton<AppAudioHandler>(
          await initAudioService(audioCache: sl()),
        );
      } catch (e) {
        print(e);
      }
    }
  } catch (e) {
    print(e);
  }
  await sl.allReady();
}

Here is repo with a minimal reproduction project: two screens splash and player.

我看到了这个网站

and I have seen in the Audio Handler documentation:

"Register the app s AudioHandler with configuration options. This must be called once during the app s initialisation ... " so does it mean that it is impossible to initialise new instance of AudioService on application restart? maybe there is a way to completely destroy the audio service instance in order to create a new one? perhaps the restart itself was done incorrectly? thanks in advance for any help!

enter image description here

问题回答

The way to interpret the documentation is that app initialisation only happens once when the Flutter engine itself is created and it enters your main method for the first time. So merely creating a method called restartApp and calling it multiple times does not mean you can make app initialisation happen more than once. So you can t initialise audio_service more than once in the same launch of your app.

由于你每次重新尝试使用“/code>的参数,你实际上没有必要两次开标。 如果你再次尝试采用某种应用逻辑的人工方法restartApp,那么你就可以在你现有的音响笔录中采用这种习惯方法,而不是重新启动整个系统。





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签