Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Some device an not play audio directly in the background #1388

Closed
ininmm opened this issue Jan 9, 2025 · 1 comment
Closed

[iOS] Some device an not play audio directly in the background #1388

ininmm opened this issue Jan 9, 2025 · 1 comment
Assignees
Labels
1 backlog bug Something isn't working

Comments

@ininmm
Copy link

ininmm commented Jan 9, 2025

On iPhone 8 (iOS 16), I found that audio cannot be played in the background directly. It has to be played in the foreground first before it can work in the background.

On iPhone XR and iPhone 12, audio can be played directly in the background.
Additionally, I tested background audio playback directly in Xcode using AVAudioSession with the same session configuration, and iPhone 8 worked fine in that setup.

Minimal reproduction project
Provide a link here using one of two options:

  1. Fork this repository and modify the example to reproduce the bug, then provide a link here.
  2. If the unmodified official example already reproduces the bug, just write "The example".

To Reproduce (i.e. user steps, not code)
Below is my code:

class AudioUtil {
  factory AudioUtil() => _instance;

  AudioUtil._();

  static final AudioUtil _instance = AudioUtil._();

  late AudioPlayer player;

  Future<void> init() async {
    player = AudioPlayer();
    final AudioSession session = await AudioSession.instance;
    await session.configure(const AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playback,
      avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.mixWithOthers,
      avAudioSessionMode: AVAudioSessionMode.defaultMode,
      androidAudioAttributes: AndroidAudioAttributes(
        contentType: AndroidAudioContentType.music,
        usage: AndroidAudioUsage.media,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
    ));
    await session.setActive(true);
  }

  Future<void> playNotification() async {
    String asset;
    if (Platform.isAndroid) {
      asset = 'assets/audio/mediumRinging.mp3';
    } else {
      asset = 'assets/audio/mediumRinging.caf';
    }
    await AudioUtil().player.setAsset(asset);
    await AudioUtil().player.setVolume(1.0);
    await AudioUtil().player.play();
  }
}

Error messages

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ PlatformException(560557684, Session activation failed, null, null)
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ #0   StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651)
│ #1   MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334)
│ #2   <asynchronous suspension>
│ #3   AVAudioSession.setActive (package:audio_session/src/darwin.dart:150)
│ #4   <asynchronous suspension>
│ #5   AudioSession.setActive (package:audio_session/src/core.dart:237)
│ #6   <asynchronous suspension>
│ #7   AudioPlayer.play (package:just_audio/just_audio.dart:948)
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Expected behavior
iPhone 8 should play audio in the background just like other iOS devices.

Screenshots
If applicable, add screenshots to help explain your problem.

  • iPhone 6
  • OS: [e.g. iOS8.1]

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel [user-branch], 3.19.6, on macOS 15.2 24C101 darwin-arm64, locale zh-Hant-TW)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.96.2)
[✓] Connected device (4 available)
[✓] Network resources

Additional context
Using package:

  • audio_session: 0.1.21
  • just_audio: 0.9.42
@ininmm ininmm added 1 backlog bug Something isn't working labels Jan 9, 2025
Copy link

github-actions bot commented Jan 9, 2025

Oops, it appears that your issue did not follow the template and is missing one or more required sections. Please open a new issue, and provide all required sections and information.

FAQ:

  1. Do I really need to submit a minimal reproduction project for a bug? A: Yes. I prioritise bugs secondarily on how many people are affected, and primarily on whether the bug report is complete, in the sense that it enables me to immediately reproduce it and start working on a fix. If a bug is important to you, the best thing you can do is to provide all requested information ASAP so that I can start looking into it ASAP.

  2. I think I supplied all required information, so did the bot make a mistake? A: The bot only checks the section headings, so when you post a new issue, make sure you leave the section headings intact. (Note that because of this, it is even possible to trick the bot by including only the section headings, and then not providing the requested information under each heading. This is frowned upon, and the issue will be closed manually.)

@github-actions github-actions bot closed this as completed Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 backlog bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants