Skip to content

Commit

Permalink
Fix: iOS - audio does not work with headphones (#3284)
Browse files Browse the repository at this point in the history
* Fix: Make AVAudioSession.Category.playAndRecord opt-in
* Fix: Call configureAudio when audioOutput is changed
* Update CHANGELOG.md
  • Loading branch information
mysport12 authored Oct 9, 2023
1 parent 1f01376 commit 0ad2e52
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog

## Next
- iOS: Fix audio session category when not using the audioOutput prop
- All: add built-in typescript support [#3266](https://github.com/react-native-video/react-native-video/pull/3266)
- iOS, Android: expose playback functions to ref [#3245](https://github.com/react-native-video/react-native-video/pull/3245)
- Windows: fix build error from over-specified SDK version [#3246](https://github.com/react-native-video/react-native-video/pull/3246)
Expand Down
6 changes: 3 additions & 3 deletions ios/Video/Features/RCTPlayerOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ enum RCTPlayerOperations {
}
}

static func configureAudio(ignoreSilentSwitch:String, mixWithOthers:String) {
static func configureAudio(ignoreSilentSwitch:String, mixWithOthers:String, audioOutput:String) {
let audioSession:AVAudioSession! = AVAudioSession.sharedInstance()
var category:AVAudioSession.Category? = nil
var options:AVAudioSession.CategoryOptions? = nil

if (ignoreSilentSwitch == "ignore") {
category = AVAudioSession.Category.playAndRecord
category = audioOutput == "earpiece" ? AVAudioSession.Category.playAndRecord : AVAudioSession.Category.playback
} else if (ignoreSilentSwitch == "obey") {
category = AVAudioSession.Category.ambient
}
Expand All @@ -221,7 +221,7 @@ enum RCTPlayerOperations {
if #available(iOS 16.0, *) {
do {
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category to playAndRecord with defaultToSpeaker options.")
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
try audioSession.setCategory(audioOutput == "earpiece" ? AVAudioSession.Category.playAndRecord : AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
} catch {
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category and options problem. Error: \(error).")
}
Expand Down
5 changes: 3 additions & 2 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc
func setIgnoreSilentSwitch(_ ignoreSilentSwitch:String?) {
_ignoreSilentSwitch = ignoreSilentSwitch
RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers)
RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers, audioOutput:_audioOutput)
applyModifiers()
}

Expand All @@ -510,7 +510,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
_player?.rate = 0.0
}
} else {
RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers)
RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers, audioOutput:_audioOutput)

if _adPlaying {
#if USE_GOOGLE_IMA
Expand Down Expand Up @@ -583,6 +583,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc
func setAudioOutput(_ audioOutput:String) {
_audioOutput = audioOutput
RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers, audioOutput:_audioOutput)
do {
if audioOutput == "speaker" {
#if os(iOS)
Expand Down

0 comments on commit 0ad2e52

Please sign in to comment.