0

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
Tomas Baran
  • 1,570
  • 2
  • 20
  • 37
  • can you try it without "automatically mage signing" and manually adding the bundle id and provisioning profile? clean your app and also delete it on the device plz test it on device. – Maziar Saadatfar Mar 10 '22 at 22:03

1 Answers1

-1

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

Swinny89
  • 7,273
  • 3
  • 32
  • 52
  • Audio background capability is set up correctly as you can see in the screenshots as well as in the [repo's Info.plist](https://github.com/tomasbaran/background_test2/blob/master/ios/Runner/Info.plist): `UIBackgroundModesaudio` – Tomas Baran Mar 09 '22 at 14:06
  • Adding bounty to this answer since it was the only submission, not because it necessarily answered the question. – Suragch Mar 14 '22 at 06:18