There is a weird bug that occurs when I try to play an audio file(.mp3) from assets, in iOS. I'm using audioplayers plugin for this. My task is to play an audio file (either from local storage or assets), after some delay, while the app is not in foreground, that is while the screen is locked or app in in recent tray. The problem is, when I try to play the audio file after any timer function when iPhone is locked (ex. after timer.delay, Future.delay
, etc) the app doesn't plays it. The app plays it after unlocking the iPhone. Also a point is to be noted that EVERYTHING WORKS as intended in DEBUG version of the app, and NOT IN RELEASE version. One more thing, I have added background audio permission in info.plist, which is necessary for playing audio in background. In Android, everything is working fine. Also , I have tried the Audio_service plugin, but it didn't worked as well.
My dummy code for app is as follows:
class AudioTesting extends StatefulWidget {
@override
_AudioTestingState createState() => _AudioTestingState();
}
class _AudioTestingState extends State<AudioTesting> {
@override
void initState() {
AudioPlayer _audioPlayer = AudioPlayer();
AudioCache _audioCache = AudioCache();
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return AudioServiceWidget(
child: Scaffold(
appBar: AppBar(
title: Text('Testing Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(onPressed: (){
Future.delayed(Duration(seconds: 5),(){
_audioCache.load('$test.mp3');
_audioPlayer = await _audioCache.play('$test.mp3');
//This doesnt works in release mode, but works in debug mode, when played in Locked state
});
},
child: Text('Future.delay func'),
),
RaisedButton(onPressed: (){
sleep(Duration(seconds: 5));
_audioCache.load('$test.mp3');
_audioPlayer = await _audioCache.play('$test.mp3');
// This works completely fine in both version
},
child: Text('sleep func'),
),
],
),
),
),
);
}
}
You can reproduce this bug as follows:
- Copy paste the above code snippet.
- build a release version and deploy it on any physical iOS device.
- Click on the buttons and lock your iPhone.
In my case, I used iPhone 8 with iOS 14.4
Are there any possible alternative solutions that would let me play music in background, when triggered by some functions in iOS?