Andrew Quinn
Andrew Quinn

Reputation: 11

Embedded AVplayer does not play videos from URL, Swift

I'm trying to code a basic app that presents videos in a list and then plays them using AVplayer from URLS in a database. The video just displays a blank screen with the player controls and does not play the video or show any info. I have tried multiple different links from youtube, vimeo and filesharing sites, all with the same problem, all links are https/

Here is the code I'm using

import UIKit
import AVKit
import SwiftUI

struct VideoPlayerView: UIViewControllerRepresentable {
    var videoURL: URL
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let controller = AVPlayerViewController()
        controller.player = AVPlayer(url: videoURL)
        return controller
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
        // Do nothing
    }
}

struct VideoDetailView: View {
    var video: Video
    
    var body: some View{
        VStack (spacing: 20){
            Spacer()
            
            Image(video.imageName)
                .resizable()
                .scaledToFit()
                .frame(height: 150)
                .cornerRadius(12)
            
            Text(video.title)
                .font(.title2)
                .fontWeight(.semibold)
                .lineLimit(2)
                .multilineTextAlignment(.center)
                .padding(.horizontal)
            
            HStack(spacing: 40) {
                Label("\(video.viewCount)", systemImage: "eye.fill")
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                
                Text (video.uploadDate)
                    .font(.subheadline)
                    .foregroundColor(.secondary)
            }
            
            Text(video.description)
                .font(.body)
                .padding()
            
            Spacer()
            
            NavigationLink(destination: VideoPlayerView(videoURL: video.url)) {
                Label("Play Video", systemImage: "play.circle.fill")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            
        }
    }
}

Tried different links, different file formats all https.

Video doesnt play just shows player controls or play button with a slash through in simulator.

Upvotes: 1

Views: 1400

Answers (1)

Sam Spencer
Sam Spencer

Reputation: 8609

Based on the conversation in the comments, and the URL you said you're trying to access: those URLs won't work. YouTube, Vimeo, and (most) File Sharing sites do not provide direct access to files without a bit of extra work.

The AVPlayer initializer you're using expects a valid URL pointing to a properly encoded video file. won't do the work of getting the actual video file for you from those URLs.

You can better understand the problem here by looking at what the network returns from a URL. For example, the URL you posted, https://vimeo.com/445519785?share=copy, returns HTML content to load a webpage. AVPlayer doesn't understand that, so will fail to load anything.

So, instead of passing in something like this to AVPlayer:

AVPlayer(url: URL(string: "https://vimeo.com/someVideo")!)

You'll need to pass in a properly formatted URL pointing to a video file:

AVPlayer(url: URL(string: "https://vimeo.com/someVideo/downloadPath/file.mov")!)

How you get those URLs is beyond the scope of this particular question.

But, I will say that if this is your video (you own it / uploaded it), you should be able to get the direct download URL from the video management page on Vimeo.

Upvotes: 0

Related Questions