SpecificMediaFileView is a view that shows up when the user presses a button, and it would show up either an image or a video. When a media type of video shows up, the view isn't obvious to the user that there are video playback controls, so the user has to manually tap it once to see the playback controls showing up to know that the media type shown up from SpecificMediaFileView is of type Video:
import SwiftUI
import AVKit
struct Media: Identifiable {
let id = UUID()
let creationTime: Date
var image: UIImage?
var videoURL: URL?
}
struct SpecificMediaFileView: View {
let media: Media
var body: some View {
Group {
if let image = media.image {
Image(uiImage: image)
} else if let videoURL = media.videoURL {
VideoPlayer(player: AVPlayer(url: videoURL))
}
}
}
}
I have tried implementing the solution suggested by Amir Kosandi in this thread, and the video does show up with playback controls on the appearance of SpecificMediaFileView, but now the playback controls can't be manually hidden or shown with a tap gesture after this implementation:
struct SpecificMediaFileView: View {
let media: Media
var body: some View {
Group {
if let image = media.image {
Image(uiImage: image)
} else if let videoURL = media.videoURL {
CustomVideoPlayer(url: videoURL)
}
}
}
}
struct CustomVideoPlayer: UIViewControllerRepresentable {
var url: URL
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = AVPlayer(url)
controller.showsPlaybackControls = true
controller.setValue(false, forKey: "canHidePlaybackControls")
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
// Update the controller if needed.
}
}
The former implementation doesn't have it show up on appear but is user show/hide-controllable via tap, but the latter does the opposite of the two. How can I modify my CustomVideoPlayer to show the playback controls on appear but allow them to be hidden/shown with a tap gesture?
I just used an
onTapGesture, to keep things simple, that updates theUIViewControllerRepresentableVideoPlayer using aBindingvariable passed by theSpecificMediaFileView:And updated the
CustomVideoPlayerlike this:This will surely work for hiding or showing the player controls but, If you want full control over this you should implement your own controls using an
overlayover theCustomVideoPlayerView. I could show you an example if you want.By the way, as I said, according to me the best way to do this is to have a custom design for your controls. I already had one working in a project, I took the chance to simplify it a bit for this use case:
Edit the
CustomVideoPlayerlike this:And you use it like this:
I know it can be a bit overwhelming at first sight, but it's pretty straightforward when you look at the code. It's just a bunch overlayed icons and the timer to make them appear/disapper. You can customise it for your needs, of course.
Let me know your thoughts on this!