play sound with audioplayers in silent mode flutter

58 Views Asked by At

Is there a way to play sound with audioplayers when the device is in silent mode? My phone is always is silent mode because I don't want it to vibrate or play any sound when I get notifications. But I have some app on my phone which still get the play sounds, and that is OK and fine.

I was wondering if I can't do the same with audioplayers or any other package.

final AudioPlayer player;

void playNotificationSound() async {
 await player.setPlayerMode(PlayerMode.lowLatency);
 await player.play(AssetSource(audioFilePlath));
}
1

There are 1 best solutions below

2
WebDesk Solution On

Here's how you can change your code to use the audioplayers package for playing notification sounds:

void playNotificationSound() async {
  await player.setUrl(audioFilePath); // Use setUrl instead of setPlayerMode
  await player.play();
}

Make sure you've added the necessary permissions in your app's AndroidManifest.xml and Info.plist files to allow audio playback. Doing this should help ensure that your app can play audio files without any issues.