i have an application and i play music for user while using it.
so i handle when user is not in resuming mode the song should stop. it works very fine but i found a bug.
the bug is when i minimize the app and go to some app like Instagram and see a reels when tap the home screen and suddenly the music play. but my AppLifecycleState
doesn’t change.
this is my code :
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
LayoutBuilder(
builder: ((context, constraints) {
Responsive().init(constraints: constraints);
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: AppRouter(),
);
}),
),
);
}
class AppRouter extends StatefulWidget {
const AppRouter({super.key});
@override
State<AppRouter> createState() => _AppRouterState();
}
class _AppRouterState extends State<AppRouter> with WidgetsBindingObserver {
List<AppLifecycleState> appStates = [];
Future<void> _playAudio() async {
await QUser().audioPlayer.play(AssetSource(QUser().audioSource));
}
bool wasAudioPlaying = true;
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
log(state.toString());
if (appStates.length > 10) {
final last = appStates[appStates.length - 1];
appStates = [];
appStates.add(last);
}
appStates.add(state);
if (state == AppLifecycleState.resumed) {
if (wasAudioPlaying) {
await QUser().audioPlayer.resume();
}
} else {
if (appStates[appStates.length - 2] == AppLifecycleState.resumed) {
if (QUser().audioPlayer.state == PlayerState.playing) {
wasAudioPlaying = true;
} else if (QUser().audioPlayer.state == PlayerState.paused) {
wasAudioPlaying = false;
}
}
await QUser().audioPlayer.pause();
}
}
@override
initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocProvider<AppBloc>(
create: (context) => AppBloc(),
child: BlocConsumer<AppBloc, AppState>(
listener: (context, state) async {
if (state.isLoading) {
LoadingScreen().show(
context: context,
text: "منتظر بمانید",
);
} else {
LoadingScreen().hide();
}
if (state.exception is InternetConnectionException) {
await showErrorDialog(
context: context,
text: "ارتباط خود را با اینترنت بررسی نمیایید",
);
} else if (state.exception is GenericException) {
await showErrorDialog(
context: context,
text: "مشکلی پیش آمده، لطفا دوباره تلاش کنید",
);
}
},
builder: (context, state) {
if (state is OnboardingAppState) {
return const OnBoardingView();
} else if (state is RegisterAppState) {
return const RegisterView();
} else if (state is AppRulesAppState) {
return const AppRulesView();
} else if (state is LoginAppState) {
return const LoginView();
} else if (state is ChooseProfileIconAppState) {
return ChooseProfileIconView(
isRegistered: state.isRegistered,
);
} else if (state is ConfirmNumberAppState) {
return ConfirmNumberView(
isRegistered: state.isRegistered,
);
} else if (state is ProfileAppState) {
return const ProfileView();
} else if (state is HomeAppState) {
if (QUser().audioPlayer.state == PlayerState.paused) {
return HomeView(
runAnimation: state.runAnimation,
);
} else {
QUser().audioPlayer.setReleaseMode(ReleaseMode.loop);
_playAudio();
return HomeView(
runAnimation: state.runAnimation,
);
}
} else if (state is RulesAppState) {
return const RulesView();
} else if (state is CommentAppState) {
return const CommentView();
} else if (state is CategoryAppState) {
return const CategoryView();
} else if (state is PickPlayerAppState) {
return const PickPlayerView();
} else if (state is SelectTimeAppState) {
return const SelectTimeView();
} else if (state is GameAppState) {
return GameView(
question: state.question,
player: state.player,
seconds: state.seconds,
);
} else if (state is GameEndAppState) {
return const GameEndView();
} else if (state is InitialAppState) {
return const InitialApp();
} else {
context.read<AppBloc>().add(const InitialAppEvent());
return const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
},
),
);
}
}
when my app is closed or minimized the song shouldn’t play at all. i handle it very well but when i play a sound from somewhere else like Instagram the app doesn’t work properly