I am trying to make a video player in my app (with swiftUI) that playes videos from youtube when the user creates a URL string. To practise I have seperated this into a new project to see that it works. But the screen is just black with a play button. Nothing happends when I press the play button or the screen.
This is my code:
import SwiftUI
import AVKit
struct ContentView: View {
let url = URL(string: "https://youtu.be/Wlf1T5nrO50")!
var body: some View {
VStack{
VideoPlayer(player: AVPlayer(url: url))
.scaledToFit()
}
}
I found another video with how to make embedded youtube videos but then you need to just copy the video ID and my user is not that advanced. I want the user to be able to just copy the URL.
Thankful for any help.
2
Answers
A YouTube url is not a valid video URL. You have to either add youtube’s library or play it using a WKWebView.
Also check out this package, it’s awesome!
Using WKWebView:
Usage:
Remember, this is a very basic WebView that does not handle errors and loading.
A better Solution I made