Skip to content

no effect when try switch speaker to earpiece during playing(setAudioContext) #1951

@yoer

Description

@yoer

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

During player playback, I want to dynamically switch from speaker to earphone to play, but there is no effect. After switching, the music currently playing will be completely sounded (there was a sound before switching)

Expected behaviour

Dynamically switch from speaker to earphone to playback, and you can hear the current music from earphone normally

Steps to reproduce

No response

Code sample

AudioContext get speakerOnAudioContext => AudioContext(
        iOS: AudioContextIOS(
          /// not silenced
          category: AVAudioSessionCategory.playback,
          options: const {
            AVAudioSessionOptions.mixWithOthers,
          },
        ),
        android: const AudioContextAndroid(
          isSpeakerphoneOn: true,
          stayAwake: true,
          contentType: AndroidContentType.unknown,
          usageType: AndroidUsageType.notificationRingtone,
          audioFocus: AndroidAudioFocus.gain,
        ),
      );

  AudioContext get speakerOffAudioContext => AudioContext(
        iOS: AudioContextIOS(
          /// not silenced
          category: AVAudioSessionCategory.playAndRecord,
          options: const {
            AVAudioSessionOptions.mixWithOthers,
          },
        ),
        android: const AudioContextAndroid(
          isSpeakerphoneOn: false,
          stayAwake: true,
          contentType: AndroidContentType.unknown,
          usageType: AndroidUsageType.notificationRingtone,
          audioFocus: AndroidAudioFocus.gain,
        ),
      );
// init
AudioPlayer.global.setAudioContext(speakerOnAudioContext);
// play
audioPlayer.play(AssetSource(sourcePath));
// switch:then i switch by button:
  final targetContext = currentAudioRoute == speaker
      ? speakerOnAudioContext
      : speakerOffAudioContext;
audioPlayer.setAudioContext(targetContext)

Affected platforms

Android, iOS

Platform details

No response

AudioPlayers Version

6.5.1

Build mode

debug

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