The Photos app on iOS is capable of playing Live Photos and showing images on another screen. My SwiftUI app is able to AirPlay videos and I can make the button appear or not using AVPlayer's isExternalPlaybackActive boolean. However, that boolean is not available for images and Live Photos.
I've tried using an AVRoutePickerView(), which gives me a button for the user to start/stop AirPlay, but I have not found a way to associate it with SwiftUI's Image or my LivePhotoView:
struct LivePhotoView: UIViewRepresentable { var livephoto: PHLivePhoto @Binding var playback:Bool @AppStorage("playAudio") var playAudio = userDefaults.bool(forKey: "playAudio") func makeUIView(context: Context) -> PHLivePhotoView { let view = PHLivePhotoView() try! AVAudioSession.sharedInstance().setCategory({playAudio ? .playback : .soloAmbient}()) view.contentMode = .scaleAspectFit return view } func updateUIView(_ lpView: PHLivePhotoView, context: Context) { lpView.livePhoto = livephoto if playback { lpView.isMuted = false try! AVAudioSession.sharedInstance().setCategory({playAudio ? .playback : .soloAmbient}()) try! AVAudioSession.sharedInstance().setActive(true) lpView.isMuted = false lpView.startPlayback(with: .full) } else { lpView.stopPlayback() } } }
struct RouteButtonView: UIViewRepresentable { func makeUIView(context: Context) -> AVRoutePickerView { let routePickerView = AVRoutePickerView() routePickerView.tintColor = .gray routePickerView.prioritizesVideoDevices = true return routePickerView } func updateUIView(_ uiView: AVRoutePickerView, context: Context) { // No update needed } }
Am I missing something? If it's a system internal API only available to the Photos App, why?
Mike