Skip to content

[iOS] Not call default AudioContext.apply() in SwiftAudioplayersDarwinPlugin when init #1937

@ZhuJHua

Description

@ZhuJHua

Checklist

  • I read the troubleshooting guide before raising this issue
  • I made sure that the issue I am raising doesn't already exist

Current bug behaviour

Now, when the plugin is initialized in the engine, AVAudioSession is set by default.

init(
registrar: FlutterPluginRegistrar,
binaryMessenger: FlutterBinaryMessenger,
methodChannel: FlutterMethodChannel,
globalMethodChannel: FlutterMethodChannel,
globalEventChannel: FlutterEventChannel
) {
self.registrar = registrar
self.binaryMessenger = binaryMessenger
self.methods = methodChannel
self.globalMethods = globalMethodChannel
self.globalEvents = GlobalAudioPlayersStreamHandler()
do {
try globalContext.apply()
} catch {
// ignore error on initialization
}
super.init()
self.globalMethods.setMethodCallHandler(self.handleGlobalMethodCall)
globalEventChannel.setStreamHandler(self.globalEvents)
}

In a pure flutter application, this is of course not a problem, as it makes the application to play without setting AVAudioSession. However, in a hybrid application, since AVAudioSession is a global singleton, In the native page, the AVAudioSession might have been modified. For instance, if a video is playing in the native context and the value of AVAudioSession is modified, when redirected to the flutter page, due to the engine's initialization, the original value may be overwritten, resulting in unexpected behavior. Especially, the default value of the current plugin is .playback, which will interrupt other audio.

Expected behaviour

The plugin does not call apply during initialization but initializes it through manual invocation.

Steps to reproduce

  1. Write a hybrid application
  2. Open a native page and modified AVAudioSession
  3. Open a flutter page
  4. Then the AVAudioSession will be change

Code sample

Code sample
void main() {
}

Affected platforms

iOS

Platform details

No response

AudioPlayers Version

6.5.0

Build mode

release

Audio Files/URLs/Sources

No response

Screenshots

No response

Logs

my relevant logs
Full Logs
my full logs or a link to a gist

Flutter doctor:

Output of: flutter doctor -v

Related issues / more information

No response

Working on PR

no way

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions