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