Skip to content

[iOS][Audio] Playing sound in background Problem #5456

@onka13

Description

@onka13

🐛 Bug Report

Environment

Expo CLI 3.0.2 environment info:
System:
OS: macOS 10.14.6
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.16.0 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
IDEs:
Xcode: 10.3/10G8 - /usr/bin/xcodebuild
npmPackages:
expo: ^33.0.0 => 33.0.5
react: 16.8.3 => 16.8.3
react-native: https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz => 0.59.8
react-navigation: ^3.11.0 => 3.11.0
npmGlobalPackages:
expo-cli: 3.0.2

Steps to Reproduce

1- Open app, play sound
2- Lock Phone
3- Stop or pause sound with Music controls buttons
4- Try to play sound with Music controls buttons

Error:

  • code: E_AV_PLAY
  • domain: UMModulesErrorDomain
  • NSLocalizedDescription: This experience is currently in the background, so the audio session could not be activated

I found the related file and comment that line works for me. Now I can play and stop sounds on background on IOS.

File; https://github.com/expo/expo/blob/master/packages/expo-av/ios/EXAV/EXAV.m#L272

  if (_isBackgrounded && ![_kernelAudioSessionManagerDelegate isActiveForModule:self]) {
    //return UMErrorWithMessage(@"This experience is currently in the background, so the audio session could not be activated.");
  }

Sample

import { Audio } from "expo-av"
import MusicControl from "react-native-music-control"


Audio.setAudioModeAsync({
	allowsRecordingIOS: false,
	staysActiveInBackground: true,
	interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
	playsInSilentModeIOS: true,
	shouldDuckAndroid: false,
	interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
	playThroughEarpieceAndroid: true
})

MusicControl.resetNowPlaying()
MusicControl.enableControl("play", true)
MusicControl.enableControl("pause", true)
MusicControl.enableControl("stop", true)
MusicControl.enableControl("nextTrack", false)
MusicControl.enableControl("previousTrack", false)
MusicControl.enableControl("changePlaybackPosition", false)
MusicControl.enableControl("setRating", false)
MusicControl.enableControl("volume", false)
MusicControl.enableControl("remoteVolume", false)
MusicControl.enableControl("enableLanguageOption", false)
MusicControl.enableControl("disableLanguageOption", false)
MusicControl.enableControl("closeNotification", true, { when: "always" })

MusicControl.enableBackgroundMode(true)
MusicControl.handleAudioInterruptions(true)
MusicControl.on("play", () => {
	playSound()
})
MusicControl.on("pause", () => {
	stopSound()
})
MusicControl.on("stop", () => {
	stopSound()
})
MusicControl.on("closeNotification", () => {
	stopSound()
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions