Skip to content

Commit

Permalink
Fix Assist audio player audio session setup (#2795)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
  • Loading branch information
bgoncal authored May 27, 2024
1 parent 7216210 commit d163484
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/App/Assist/Audio/AudioPlayer.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AVFoundation
import Foundation
import Shared

protocol AudioPlayerProtocol {
func play(url: URL)
Expand All @@ -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)")

This comment has been minimized.

Copy link
@michalsrutek

michalsrutek May 27, 2024

Contributor

It might also be beneficial to log just error (which is basically String(describing: error)), as localizedDescription might be in Chinese, Hungarian, etc. - and might not provide enough context.

}

let playerItem = AVPlayerItem(url: url)
player.replaceCurrentItem(with: playerItem)
player.play()
Expand Down

0 comments on commit d163484

Please sign in to comment.