Skip to content

Commit 31c1c59

Browse files
juliusmarmingeJulius Marminge
andauthored
feat(mobile): add video playback with native iOS controls (#8919)
Co-authored-by: Julius Marminge <[email protected]>
1 parent ef84bc9 commit 31c1c59

37 files changed

Lines changed: 2494 additions & 213 deletions

‎apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,43 @@ import Security
33
import UIKit
44

55
public final class T3NativeControlsModule: Module {
6+
private let presentationSources = T3PresentationSources()
7+
private var videoPresentation: T3NativeVideoPresentation?
8+
69
public func definition() -> ModuleDefinition {
710
Name("T3NativeControls")
811

12+
AsyncFunction("presentVideo") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
13+
try self.presentVideo(
14+
url: url,
15+
title: title,
16+
sourceIdentifier: sourceIdentifier,
17+
identifier: identifier,
18+
promise: promise
19+
)
20+
}.runOnQueue(.main)
21+
22+
AsyncFunction("dismissVideo") { (identifier: String) in
23+
self.dismissVideo(identifier: identifier)
24+
}.runOnQueue(.main)
25+
26+
OnDestroy {
27+
let presentation = self.videoPresentation
28+
DispatchQueue.main.async { presentation?.dismiss() }
29+
}
30+
31+
View(T3PresentationSourceView.self) {
32+
ViewName("PresentationSource")
33+
Prop("identifier") { (view: T3PresentationSourceView, identifier: String) in
34+
view.sources = self.presentationSources
35+
view.identifier = identifier
36+
}
37+
}
38+
39+
AsyncFunction("shareFileFromSource") { (url: URL, title: String, identifier: String, promise: Promise) in
40+
try self.shareFile(url: url, title: title, sourceIdentifier: identifier, promise: promise)
41+
}.runOnQueue(.main)
42+
943
Function("getShowcasePairingUrl") {
1044
let arguments = ProcessInfo.processInfo.arguments
1145
guard
@@ -101,4 +135,47 @@ public final class T3NativeControlsModule: Module {
101135
try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
102136
}
103137
}
138+
139+
private func presentVideo(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws {
140+
let isPlayableURL = url.isFileURL
141+
? FileManager.default.isReadableFile(atPath: url.path)
142+
: (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
143+
guard videoPresentation == nil,
144+
let presenter = appContext?.utilities?.currentViewController(),
145+
isPlayableURL
146+
else {
147+
throw NSError(
148+
domain: "T3NativeVideo",
149+
code: 2,
150+
userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
151+
)
152+
}
153+
let presentation = T3NativeVideoPresentation(identifier: identifier, url: url, title: title) { [weak self] error in
154+
self?.videoPresentation = nil
155+
if let error { promise.reject(error) } else { promise.resolve(nil) }
156+
}
157+
videoPresentation = presentation
158+
presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
159+
}
160+
161+
private func dismissVideo(identifier: String) {
162+
if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
163+
}
164+
165+
private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
166+
guard let presenter = appContext?.utilities?.currentViewController() else {
167+
throw NSError(
168+
domain: "T3NativePresentation",
169+
code: 2,
170+
userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
171+
)
172+
}
173+
try presentFileShare(
174+
url: url,
175+
title: title,
176+
source: presentationSources.view(for: sourceIdentifier),
177+
presenter: presenter,
178+
promise: promise
179+
)
180+
}
104181
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import ExpoModulesCore
2+
import UIKit
3+
4+
final class T3PresentationSources {
5+
private class Entry {
6+
weak var view: UIView?
7+
init(_ view: UIView) { self.view = view }
8+
}
9+
10+
private var entries: [String: Entry] = [:]
11+
12+
func register(_ view: UIView, identifier: String) {
13+
entries[identifier] = Entry(view)
14+
}
15+
16+
func remove(_ view: UIView, identifier: String) {
17+
if entries[identifier]?.view == nil || entries[identifier]?.view === view {
18+
entries.removeValue(forKey: identifier)
19+
}
20+
}
21+
22+
func view(for identifier: String) -> UIView? {
23+
// Use the child bounds, not the wrapper's potentially stretched layout bounds.
24+
entries[identifier]?.view?.subviews.first
25+
}
26+
}
27+
28+
final class T3PresentationSourceView: ExpoView {
29+
weak var sources: T3PresentationSources?
30+
var identifier = "" {
31+
didSet {
32+
sources?.remove(self, identifier: oldValue)
33+
if !identifier.isEmpty { sources?.register(self, identifier: identifier) }
34+
}
35+
}
36+
37+
deinit {
38+
sources?.remove(self, identifier: identifier)
39+
}
40+
}
41+
42+
func presentFileShare(
43+
url: URL,
44+
title: String,
45+
source: UIView?,
46+
presenter: UIViewController,
47+
promise: Promise
48+
) throws {
49+
guard url.isFileURL, FileManager.default.isReadableFile(atPath: url.path) else {
50+
throw NSError(
51+
domain: "T3NativePresentation",
52+
code: 1,
53+
userInfo: [NSLocalizedDescriptionKey: "The file is no longer available."]
54+
)
55+
}
56+
57+
guard let origin = source ?? presenter.view else {
58+
throw NSError(
59+
domain: "T3NativePresentation",
60+
code: 2,
61+
userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
62+
)
63+
}
64+
65+
let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil)
66+
activity.title = title
67+
activity.overrideUserInterfaceStyle = source?.traitCollection.userInterfaceStyle
68+
?? presenter.traitCollection.userInterfaceStyle
69+
activity.completionWithItemsHandler = { _, _, _, _ in promise.resolve(nil) }
70+
activity.modalPresentationStyle = .popover
71+
activity.popoverPresentationController?.sourceView = origin
72+
activity.popoverPresentationController?.sourceRect = source?.bounds
73+
?? CGRect(x: origin.bounds.midX, y: origin.bounds.maxY, width: 0, height: 0)
74+
presenter.present(activity, animated: true)
75+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import AVKit
2+
import UIKit
3+
4+
final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
5+
UIAdaptivePresentationControllerDelegate {
6+
let identifier: String
7+
private let controller = AVPlayerViewController()
8+
private let completion: (Error?) -> Void
9+
private var itemObservation: NSKeyValueObservation?
10+
private var backgroundObserver: NSObjectProtocol?
11+
private var playbackError: Error?
12+
private var presented = false
13+
private var dismissRequested = false
14+
private var finished = false
15+
private struct AudioSessionConfiguration {
16+
let category: AVAudioSession.Category
17+
let mode: AVAudioSession.Mode
18+
let options: AVAudioSession.CategoryOptions
19+
20+
init(_ session: AVAudioSession) {
21+
category = session.category
22+
mode = session.mode
23+
options = session.categoryOptions
24+
}
25+
}
26+
private var previousAudioSession: AudioSessionConfiguration?
27+
private weak var fullScreenController: UIViewController?
28+
private var embedded = false
29+
30+
init(identifier: String, url: URL, title: String, completion: @escaping (Error?) -> Void) {
31+
self.identifier = identifier
32+
self.completion = completion
33+
super.init()
34+
35+
let item = AVPlayerItem(url: url)
36+
let metadata = AVMutableMetadataItem()
37+
metadata.identifier = .commonIdentifierTitle
38+
metadata.value = title as NSString
39+
item.externalMetadata = [metadata]
40+
controller.player = AVPlayer(playerItem: item)
41+
controller.delegate = self
42+
controller.overrideUserInterfaceStyle = .dark
43+
controller.allowsPictureInPicturePlayback = false
44+
45+
itemObservation = item.observe(\.status, options: [.initial, .new]) { [weak self] item, _ in
46+
guard item.status == .failed else { return }
47+
DispatchQueue.main.async {
48+
guard let self else { return }
49+
self.playbackError = item.error ?? NSError(
50+
domain: "T3NativeVideo",
51+
code: 1,
52+
userInfo: [NSLocalizedDescriptionKey: "This video couldn't be played on this device."]
53+
)
54+
self.dismiss()
55+
}
56+
}
57+
backgroundObserver = NotificationCenter.default.addObserver(
58+
forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: .main
59+
) { [weak self] _ in self?.controller.player?.pause() }
60+
}
61+
62+
func present(from presenter: UIViewController, sources: T3PresentationSources, sourceIdentifier: String) {
63+
let audioSession = AVAudioSession.sharedInstance()
64+
previousAudioSession = AudioSessionConfiguration(audioSession)
65+
do {
66+
try audioSession.setCategory(.playback, mode: .moviePlayback)
67+
} catch {
68+
NSLog("T3 video audio session: %@", error.localizedDescription)
69+
}
70+
// AVKit exposes programmatic inline-to-full-screen entry through this selector.
71+
// This is the same guarded entry point used by expo-video's enterFullscreen().
72+
let enterFullScreen = NSSelectorFromString("enterFullScreenAnimated:completionHandler:")
73+
if let source = sources.view(for: sourceIdentifier), source.window != nil,
74+
controller.responds(to: enterFullScreen) {
75+
// AVKit owns the transition from its inline view to full screen. Using a
76+
// separate UIKit zoom transition prevents its native Close action from exiting.
77+
var responder: UIResponder? = source
78+
while let current = responder, !(current is UIViewController) { responder = current.next }
79+
let parent = responder as? UIViewController ?? presenter
80+
embedded = true
81+
parent.addChild(controller)
82+
controller.view.frame = source.bounds
83+
controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
84+
source.addSubview(controller.view)
85+
controller.didMove(toParent: parent)
86+
controller.view.layoutIfNeeded()
87+
controller.perform(enterFullScreen, with: true, with: nil)
88+
controller.player?.play()
89+
} else {
90+
presenter.present(controller, animated: true) { [self] in
91+
presented = true
92+
if dismissRequested {
93+
dismiss()
94+
} else if UIApplication.shared.applicationState == .active {
95+
controller.player?.play()
96+
}
97+
}
98+
controller.presentationController?.delegate = self
99+
}
100+
}
101+
102+
func dismiss() {
103+
dismissRequested = true
104+
guard !finished else { return }
105+
guard presented else {
106+
if embedded && fullScreenController == nil { finish() }
107+
return
108+
}
109+
(fullScreenController ?? controller).dismiss(animated: true) { [self] in finish() }
110+
}
111+
112+
func playerViewController(
113+
_ playerViewController: AVPlayerViewController,
114+
willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator
115+
) {
116+
fullScreenController = coordinator.viewController(forKey: .to)
117+
coordinator.animate(alongsideTransition: nil) { [weak self] context in
118+
guard let self else { return }
119+
if context.isCancelled {
120+
finish()
121+
} else {
122+
presented = true
123+
if dismissRequested { dismiss() }
124+
}
125+
}
126+
}
127+
128+
func playerViewController(
129+
_ playerViewController: AVPlayerViewController,
130+
willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator
131+
) {
132+
coordinator.animate(alongsideTransition: nil) { [weak self] context in
133+
if !context.isCancelled { self?.finish() }
134+
}
135+
}
136+
137+
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
138+
finish()
139+
}
140+
141+
private func finish() {
142+
guard !finished else { return }
143+
finished = true
144+
controller.player?.pause()
145+
if embedded {
146+
controller.willMove(toParent: nil)
147+
controller.view.removeFromSuperview()
148+
controller.removeFromParent()
149+
}
150+
itemObservation = nil
151+
controller.player = nil
152+
if let backgroundObserver { NotificationCenter.default.removeObserver(backgroundObserver) }
153+
backgroundObserver = nil
154+
let audioSession = AVAudioSession.sharedInstance()
155+
if let previousAudioSession, audioSession.category == .playback,
156+
audioSession.mode == .moviePlayback, audioSession.categoryOptions.isEmpty {
157+
// AVPlayer owns activation. Deactivating the shared session here could
158+
// stop another player or recorder that was active before this preview.
159+
try? audioSession.setCategory(
160+
previousAudioSession.category,
161+
mode: previousAudioSession.mode,
162+
options: previousAudioSession.options
163+
)
164+
}
165+
completion(playbackError)
166+
}
167+
}

‎apps/mobile/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"expo-crypto": "~57.0.2",
8686
"expo-dev-client": "~57.0.16",
8787
"expo-device": "~57.0.1",
88+
"expo-document-picker": "~57.0.1",
8889
"expo-file-system": "~57.0.6",
8990
"expo-font": "~57.0.2",
9091
"expo-glass-effect": "~57.0.1",
@@ -102,6 +103,7 @@
102103
"expo-sqlite": "~57.0.2",
103104
"expo-symbols": "~57.0.2",
104105
"expo-updates": "~57.0.19",
106+
"expo-video": "~57.0.3",
105107
"expo-web-browser": "~57.0.2",
106108
"expo-widgets": "~57.0.15",
107109
"punycode": "^2.3.1",

0 commit comments

Comments
 (0)