Skip to content

Commit 20f6a9a

Browse files
committed
fix: do not attempt to play video that is not yet scrolled into view (fixes #326)
Signed-off-by: Tommy van der Vorst <tommy@pixelspark.nl>
1 parent c3ce82f commit 20f6a9a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Sushitrain/FileViewerView.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct FileViewerView: View {
4343
LazyHStack(spacing: 1.0) {
4444
ForEach(self.scrollableFiles) { sibling in
4545
NavigationStack {
46-
FileViewerContentView(file: sibling, isShown: $isShown)
46+
FileViewerContentView(file: sibling, isShown: $isShown, playFile: .constant(sibling == self.file))
4747
.toolbar {
4848
self.toolbarContent(file: sibling)
4949
}
@@ -163,6 +163,7 @@ private struct FileViewerContentView: View {
163163
@Environment(AppState.self) private var appState
164164
var file: SushitrainEntry
165165
@Binding var isShown: Bool
166+
@Binding var playFile: Bool
166167
@State private var error: (any Error)? = nil
167168
@State private var loading: Bool = true
168169

@@ -177,6 +178,17 @@ private struct FileViewerContentView: View {
177178
self.error = nil
178179
}
179180
}
181+
else if !playFile {
182+
// Placeholder is shown while this view is not yet fully scrolled into view
183+
ZStack {
184+
if file.isVideo {
185+
Rectangle().frame(maxWidth: .infinity, maxHeight: .infinity)
186+
.foregroundStyle(.black)
187+
.ignoresSafeArea()
188+
}
189+
ThumbnailView(file: file, showFileName: false, showErrorMessages: false, scaleToFill: false)
190+
}
191+
}
180192
else {
181193
if file.isVideo || file.isAudio {
182194
FileMediaPlayer(file: file, visible: $isShown)
@@ -187,8 +199,12 @@ private struct FileViewerContentView: View {
187199
.id(url)
188200
#if os(iOS)
189201
.ignoresSafeArea()
202+
.opacity(loading ? 0.0 : 1.0)
190203
#endif
191204
if loading {
205+
ThumbnailView(file: file, showFileName: false, showErrorMessages: false, scaleToFill: false)
206+
.blur(radius: 10.0)
207+
.opacity(0.5)
192208
ProgressView()
193209
.progressViewStyle(.circular).controlSize(.extraLarge)
194210
}
@@ -225,11 +241,7 @@ private struct FileMediaPlayer: View {
225241
private func activateSession() {
226242
#if os(iOS)
227243
do {
228-
try session.setCategory(
229-
.playback,
230-
mode: .default,
231-
options: []
232-
)
244+
try session.setCategory(.playback, mode: .default, options: [])
233245
}
234246
catch _ {}
235247

@@ -299,6 +311,7 @@ private struct FileMediaPlayer: View {
299311
do {
300312
let url = file.localNativeFileURL ?? URL(string: self.file.onDemandURL())!
301313
let avAsset = AVURLAsset(url: url)
314+
Log.warn("StartPlayer file=\(file.fileName()) url=\(url)")
302315
if try await avAsset.load(.isPlayable) {
303316
let player = AVPlayer(playerItem: AVPlayerItem(asset: avAsset))
304317
// TODO: External playback requires us to use http://devicename.local:xxx/file/.. URLs rather than http://localhost.

Sushitrain/ThumbnailView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct ThumbnailView: View {
1111
var showFileName: Bool
1212
var showErrorMessages: Bool
1313
var onTap: (() -> Void)?
14+
var scaleToFill: Bool = true
1415

1516
@Environment(AppState.self) private var appState
1617
@State private var showPreview = false
@@ -32,7 +33,12 @@ struct ThumbnailView: View {
3233
})
3334

3435
case .success(let image):
35-
image.resizable().scaledToFill()
36+
if scaleToFill {
37+
image.resizable().scaledToFill()
38+
}
39+
else {
40+
image.resizable().scaledToFit()
41+
}
3642

3743
case .failure(_):
3844
if self.showErrorMessages {

0 commit comments

Comments
 (0)