UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

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

3      

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

3      

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

3      

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.

3      

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

3      

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

3      

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

3      

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

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.