NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Play a video

Forums > SwiftUI

I am having a problem playing a video. Here is my example code

import AVKit
import SwiftUI

struct ContentView: View {
    var body: some View {
        VideoPlayer(player: AVPlayer(url: URL(string: "https://www.youtube.com/embed/ld-bG1CjkZY?rel=0")!))
    }
}

When the program runs, I just get the video controls shown, but a blank video. Some other videos play OK. Any ideas? My iPhone is running 14.0

   

@twostraws  Site AdminHWS+

I'm not sure you're able to embed YouTube videos like that – IIRC they serve them up in a format iOS doesn't support without special permissions from Apple. Perhaps try a direct MP4 link first and see if that's better.

   

the problem is that I get the URL from this API call https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=2020-08-31

   

could i perhaps embed a web browser control and play a video inside that?

   

Yes I tried with local m4v files, its showing black screen. Tried mp4 file it shows the video thumbnail but no controls, does not play. A remote url works but not youtube video. I need to play local m4v files. Also how to programmatically play the video in swiftUI and also add flag overlays as this is a test.

   

could i perhaps embed a web browser control and play a video inside that?

   

Sure, the WKWebView will work great. I dont think you could play that YT link natively without trying to get link to the underling files...

   

Sorry for the late reply, but Giles Van Gruisen has created a WKWebView YouTube player (https://github.com/gilesvangruisen/Swift-YouTube-Player), which you can utilize in your SwiftUI project following this guide https://medium.com/@mikolukasik/implementing-a-youtube-player-in-swiftui-ff386fdfd1fb by Miko Lukasik

   

Would it be possible to implement something like this in macOS? If so, how hard would it be to convert it?

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.