skip to Main Content

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


  1. 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:

     struct LinkView: View {
        var link: String
        var body: some View {
            WebView(url: URL(string: link.embed)!)
        }
    }
    
    struct WebView: UIViewRepresentable {
        var url: URL
        func makeUIView(context: Context) -> WKWebView  {
            let webView = WKWebView()
            let request = URLRequest(url: url)
            webView.load(request)
            return webView
        }
        func updateUIView(_ uiView: WKWebView, context: Context) {
        }
    }
    extension String {
        var embed: String {
            var strings = self.components(separatedBy: "/")
            let videoId = strings.last ?? ""
            strings.removeLast()
            let embedURL = strings.joined(separator: "/") + "embed/(videoId)"
            return embedURL
        }
    }
    

    Usage:

    LinkView(link: "https://youtube.com/videoid")
    

    Remember, this is a very basic WebView that does not handle errors and loading.

    Login or Signup to reply.
  2. A better Solution I made

    
    struct YoutubeVideoView: UIViewRepresentable {
        
        var youtubeVideoID: String
        
        func makeUIView(context: Context) -> WKWebView  {
            
            WKWebView()
        }
        
        func updateUIView(_ uiView: WKWebView, context: Context) {
            
            let path = "https://www.youtube.com/embed/(youtubeVideoID)"
            guard let url = URL(string: path) else { return }
            
            uiView.scrollView.isScrollEnabled = false
            uiView.load(.init(url: url))
        }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search