From d1634844dbbd4e3b5cd1a6b0dbbe33b8133808f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Pantale=C3=A3o=20Gon=C3=A7alves?= Date: Mon, 27 May 2024 11:21:34 +0200 Subject: [PATCH] Fix Assist audio player audio session setup (#2795) ## Summary ## Screenshots ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant# ## Any other notes --- Sources/App/Assist/Audio/AudioPlayer.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/App/Assist/Audio/AudioPlayer.swift b/Sources/App/Assist/Audio/AudioPlayer.swift index 8c414f1c4..a56274ce6 100644 --- a/Sources/App/Assist/Audio/AudioPlayer.swift +++ b/Sources/App/Assist/Audio/AudioPlayer.swift @@ -1,5 +1,6 @@ import AVFoundation import Foundation +import Shared protocol AudioPlayerProtocol { func play(url: URL) @@ -10,6 +11,15 @@ final class AudioPlayer: NSObject, AudioPlayerProtocol { private let player = AVPlayer() func play(url: URL) { + do { + let audioSession = AVAudioSession.sharedInstance() + try audioSession.setActive(false) + try audioSession.setCategory(.playback) + try audioSession.setActive(true) + } catch { + Current.Log.error("Failed to setup audio session for audio player: \(error.localizedDescription)") + } + let playerItem = AVPlayerItem(url: url) player.replaceCurrentItem(with: playerItem) player.play()