I am trying to implement an alarm functionality in my Flutter app.I wanna schedule the alarm using flutter workmanager package so that the alarm can be played in background even the app is closed. I was trying to play audio using the audioplayers package.
Normally,the audio is playing while not using it inside the workmanager executeTask function.The workmanager is working though.I've tested with print statement.But the audio is not playing.Here is the code of my main.dart file
main.dart file
import 'package:early_bird/functions/audio.dart';
import 'package:early_bird/screens/home_screen.dart';
import 'package:flutter/material.dart';
import 'package:workmanager/workmanager.dart';
@pragma('vm:entry-point')
Future<void> callBack() async {
Workmanager().executeTask((taskName, inputData) async {
try {
Audio audio = Audio('audio/sound.mp3');
print('I am running');
await audio.play();
return Future.value(true);
} catch (err) {
return Future.value(false);
}
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(callBack, isInDebugMode: true);
runApp(const App());
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
HomeScreen.routeName: (_) => HomeScreen(),
},
);
}
}
I've configure the audio inside the audio.dart file and call the Workmanager().registerPeriodicTask() function on a button click.
How can I solve it now?