WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

1      

@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.

1      

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

1      

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

1      

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.

1      

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

1      

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

1      

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

1      

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

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.