Fix video player

This commit is contained in:
Thomas Ricouard 2022-12-22 07:00:35 +01:00
parent cff6f3a843
commit 6144e2ba6a
2 changed files with 29 additions and 14 deletions

View File

@ -1,15 +1,6 @@
import Foundation
public struct MediaAttachement: Codable, Identifiable, Hashable {
public struct Meta: Codable, Equatable {
public let width: Int?
public let height: Int?
public let size: String?
public let aspect: Float?
public let x: Float?
public let y: Float?
}
public enum SupportedType: String {
case image, gifv
}
@ -26,6 +17,5 @@ public struct MediaAttachement: Codable, Identifiable, Hashable {
public let url: URL
public let previewUrl: URL?
public let description: String?
public let meta: [String: Meta]?
}

View File

@ -2,8 +2,33 @@ import SwiftUI
import Models
import AVKit
private class VideoPlayerViewModel: ObservableObject {
@Published var player: AVPlayer?
private let url: URL
init(url: URL) {
self.url = url
}
func preparePlayer() {
player = .init(url: url)
player?.play()
}
}
private struct VideoPlayerView: View {
@StateObject var viewModel: VideoPlayerViewModel
var body: some View {
VStack {
VideoPlayer(player: viewModel.player)
}.onAppear {
viewModel.preparePlayer()
}
}
}
// Could have just been a state, but SwiftUI .sheet is buggy ATM without @StateObject
class SelectedMediaSheetManager: ObservableObject {
private class SelectedMediaSheetManager: ObservableObject {
@Published var selectedAttachement: MediaAttachement?
}
@ -58,7 +83,7 @@ public struct StatusMediaPreviewView: View {
}
)
case .gifv:
VideoPlayer(player: AVPlayer(url: attachement.url))
VideoPlayerView(viewModel: .init(url: attachement.url))
.frame(width: proxy.frame(in: .local).width)
.frame(height: attachements.count > 2 ? 100 : 200)
}
@ -98,7 +123,7 @@ public struct StatusMediaPreviewView: View {
}
)
case .gifv:
VideoPlayer(player: AVPlayer(url: attachement.url))
VideoPlayerView(viewModel: .init(url: attachement.url))
}
Spacer()
}