Bundle ID breaks audio in the background

259 Views Asked by At

The audio works in the background as long as my bundle id is "dev.suragch.flutterAudioServiceDemo" (not mine)

enter image description here

but if I change bunde id to my "com.ambee.new": enter image description here — background stops working.

What's going on? How to make it work with my bundleId "com.ambee.new"?

This is my main.dart:

import 'package:audio_service/audio_service.dart';
import 'package:get_it/get_it.dart';
import 'package:audioplayers/audioplayers.dart' as audioplayers;
void main() async {
  await setupServiceLocator();
  final _audioHandler = getIt<AudioHandler>();
  _audioHandler.play();
}


GetIt getIt = GetIt.instance;

Future<void> setupServiceLocator() async {
  // services
  getIt.registerSingleton<AudioHandler>(await initAudioService());
}

Future<AudioHandler> initAudioService() async {
  return await AudioService.init(
    builder: () => MyAudioHandler(),
    config: AudioServiceConfig(
      androidNotificationChannelId: 'com.mycompany.myapp.audio',
      androidNotificationChannelName: 'Audio Service Demo',
      androidNotificationOngoing: true,
      androidStopForegroundOnPause: true,
    ),
  );
}

class MyAudioHandler extends BaseAudioHandler {
  final _player = audioplayers.AudioPlayer();

  MyAudioHandler() {
    _player.setReleaseMode(audioplayers.ReleaseMode.LOOP);
  }

  @override
  Future<void> play() => _player.play(
      'https://firebasestorage.googleapis.com/v0/b/ambee-cloud.appspot.com/o/Sound%2Fshort.wav?alt=media&token=bd7cc97f-d3d8-490b-9aff-b92df304e145');
}

You can reproduce the issue yourself by going to ios -> Runner.xcworkspace from this repo. Test the background audio loop with a physical iPhone, not simulator.

When you test it with this bundle ID in Xcode:

  • dev.suragch.flutterAudioServiceDemo: once you block the physical iPhone, it keeps looping the audio in background
  • com.ambee.new: once you block the physical iPhone, it STOPS looping the audio in background
1

There are 1 best solutions below

2
On

It's because you need to add the background audio capability to your new bundle ID. Without this being added, the app doesn't have permission to play audio in the background.

You can find the documentation here: https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_ios_and_tvos/enabling_background_audio