How to load the list of videos available on the ios device in swiftui

50 Views Asked by At

I have started to develop an iOS application where i have to load all the videos that is available on the user's device as a list on my application as a label if i click on that it should navigate to an another view to play it using avplayer.

I have developed it to some extend where i can able to select the videos available on the photos and store it in a array as PhotosPickerItem.

How i can get playable video out of it and how i can list those videos as a list/grid.

import SwiftUI
import AVKit
import PhotosUI

struct ContentView : View {
    
    @State private var selectedAssets: [PhotosPickerItem] = []
    @State private var authorizationStatus: PHAuthorizationStatus = .notDetermined
    @State private var errorMessage: String?
    
    var body: some View {
        VStack{
            if authorizationStatus == .authorized || authorizationStatus == .limited{
                PhotosPicker(selection: $selectedAssets, matching: .videos) {
                    Text("Select Multiple Videos \(selectedAssets.count)")
                }
                if !selectedAssets.isEmpty {

                } else {
                    Text("No videos selected")
                }
            }
            else if authorizationStatus == .denied {
                HStack{
                    Image(systemName: "info.circle")
                    Text("Access Denied")
                }
            }
            else if authorizationStatus == .notDetermined {
                Button("Request Access") {
                    PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
                        self.authorizationStatus = status
                    }
                }
            }
            else {
                Text("An error occurred while accessing the Photos library.")
            }
            if let errorMessage = errorMessage {
                Text(errorMessage)
            }
            
        }
        .onAppear {
            PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
                self.authorizationStatus = status
            }
        }
    }
    
}

0

There are 0 best solutions below