flutter_soloud icon indicating copy to clipboard operation
flutter_soloud copied to clipboard

fix: audio output on IOS not working

Open johnareid54 opened this issue 1 month ago • 1 comments

When I ran the code on a real IOS device (iPhone) there was no sound begin generated. Looking at the issues posted I could see that this is an issue other has experienced, and you don't have a real mac and iPhone to test on. I was about to give up and revert back to another audio package when as a last try I asked Gemini to look at it - it came up with a simple adjustment which worked, the file "AppDelegate.swift" just needed a few adjustments. It could be helpful to others or even perhaps it could done within your package

Here are the steps to do that:

  1. Open your Xcode project Navigate to the ios folder of your Flutter project and open the Runner.xcworkspace file in Xcode.
  2. Modify AppDelegate.swift In the Xcode project navigator, find the AppDelegate.swift file located inside the Runner/Runner folder. You'll need to import AVFoundation and set the audio session category. Here is the code change you need to make. I'll provide it as a diff for your /Users/john/Desktop/Parrot/ios/Runner/AppDelegate.swift file (though you haven't provided it, this is the standard location and content for a new Flutter project). AppDelegate.swift -0 +8

The code change produced by Gemini cannot be applied because ios/Runner/AppDelegate.swift was not included in the context. import UIKit import Flutter import AVFoundation

@UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { do { try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default) try AVAudioSession.sharedInstance().setActive(true) } catch { print("Failed to set audio session category.") }

GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)

} }

If you don't have an AppDelegate.swift file, you might have an AppDelegate.m (Objective-C) file instead. The change would be similar but in Objective-C. What this change does: • import AVFoundation: This imports Apple's framework for working with time-based audiovisual media. • try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default): This line is the key. It sets the audio session category to .playback. This category indicates that audio playback is a central feature of your app. It allows your app to play audio even when the screen is locked and the Ring/Silent switch is set to silent. • try AVAudioSession.sharedInstance().setActive(true): This activates your app's audio session, making it the active session on the device. After making this change, clean your build folder (flutter clean) and rebuild your app for iOS. The sound should now work correctly on your iPhone. Background Audio (Optional) If you need audio to continue playing when your app is in the background, you have one more step: 1 In Xcode, select the Runner project in the navigator. 2 Select the Runner target. 3 Go to the Signing & Capabilities tab. 4 Click + Capability. 5 Select Background Modes. 6 In the new section that appears, check the box for Audio, AirPlay, and Picture in Picture. This will ensure your app's audio isn't cut off when the user switches to another app or locks their screen.

johnareid54 avatar Nov 15 '25 16:11 johnareid54

Hi @johnareid54, thanks for your informations.

So, it seems that an audio session is mandatory to play something on iOS! I wasn't aware of that.

To manage the audio session, you could also follow this doc which tells you how to use the audio_session package.

For the background music, when you switch your application to another or when you close it, you can try audio_service.

Anyway, I have now a Mac Mini M4! But still missing an iPhone.

alnitak avatar Nov 16 '25 15:11 alnitak