If you’re developing a Flutter app that plays audio, you might notice that the audio doesn’t play when an iPhone is in silent mode, even though the volume is up. This can be a problem for apps that rely on sound, such as podcasts, music players, or voice recorders. In this guide, I’ll walk you through how to ensure that your Flutter app’s audio plays even when the device is on silent mode, using iOS-specific configurations.
Prerequisites
- Basic knowledge of Flutter development.
- Your Flutter project set up with audio playback (e.g., using plugins like audioplayers or just_audio).
- Understanding of how to modify platform-specific iOS code.
Problem
By default, iOS respects the system-wide mute/silent switch for media playback. When your iPhone is in silent mode, any audio your Flutter app tries to play won’t be heard unless you configure the iOS audio session properly.
Solution Overview
To allow audio playback in silent mode, you need to configure the AVAudioSession in your iOS native code. Specifically, you set the audio session category to playback, which allows audio to be played even when the device is muted.
Step-by-Step Guide
Step 1: Add an Audio Playback Plugin
First, make sure you have an audio playback plugin installed in your Flutter app. Two popular plugins are:
- audioplayers
- just_audio
Add one of these to your pubspec.yaml file. For example, using audioplayers:
dependencies:
audioplayers: ^0.20.1
Run flutter pub get to install the plugin.
Step 2: Modify AppDelegate.swift
Now, you need to modify the iOS-specific code in AppDelegate.swift to configure the audio session.
- Locate AppDelegate.swift:
- Open your Flutter project and navigate to the iOS folder.
The file path should look like this:
your_flutter_project/ios/Runner/AppDelegate.swift
Modify the AppDelegate.swift file:
Add the following code inside the didFinishLaunchingWithOptions method of your AppDelegate.swift to configure the AVAudioSession.
- import UIKit
- import Flutter
- import AVFoundation
- @UIApplicationMain
- @objc class AppDelegate: FlutterAppDelegate {
- override func application(
- _ application: UIApplication,
- didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
- ) -> Bool {
- // Set up audio session to allow playback in silent mode
- do {
- try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
- try AVAudioSession.sharedInstance().setActive(true)
- } catch {
- print(“Failed to set up AVAudioSession: \(error)”)
- }
- GeneratedPluginRegistrant.register(with: self)
- return super.application(application, didFinishLaunchingWithOptions: launchOptions)
- }}
Step 3: Explanation of the Code
- AVAudioSession.sharedInstance().setCategory(.playback): This sets the audio session category to playback, which ensures that your app can play audio even when the device is in silent mode.
- setActive(true): This activates the audio session immediately, preparing it to play audio.
- The try-catch block handles any potential errors that might occur when configuring the audio session.
Step 4: Rebuild Your Flutter App
After making changes to the iOS native code, clean and rebuild your Flutter app to apply the changes:
- flutter clean
- flutter run
Step 5: Test on iOS
- Put your iPhone in silent mode by flipping the silent switch.
- Run your app and attempt to play audio.
- The audio should now play even when the device is in silent mode.
Troubleshooting
- Audio still not playing in silent mode: Make sure you’ve correctly modified AppDelegate.swift and that the app rebuilds successfully.
- Swift version compatibility: If you’re using a custom Swift version, make sure the code syntax matches the correct Swift version.
- Testing: Test thoroughly on real devices as iOS simulators might not accurately simulate silent mode behavior.
Conclusion
By modifying the iOS audio session in your Flutter app, you can ensure that audio plays even when the device is in silent mode. This is especially important for media-focused apps that rely on continuous playback regardless of the system-wide silent setting.
With this small tweak, you improve the user experience for iOS users, ensuring they can always hear the audio your app generates, whether it’s music, podcasts, or voice recordings.
Developer: Sheeraz Ali
Designation: Flutter Developer
LinkedIn: Sheeraz Ali
GitHub: thesheerazali
Find for More Code Error related news Click here.
Subscribe our WhatsApp Channel to received Personalised updates.
Hi
Hello Mr. Rameez