Search code examples
iosarraysswiftavplayeravplayerviewcontroller

How can i make an array of .mp4 or .mov files in Swift?


Ok So I'm trying to make an array of locally stored videos in Xcode. Can anyone help me out? I know how to program a tableView, I just need to use an array to pass that information (using the array of video files) to an AVPlayerKit Controller. in Swift.


Solution

  • So I had a similar issue before and what I ended up doing was creating a Model for my videos, which contained a property for storing the file data. This way I was able to make Sequence Types for my Model and use said sequence as a data source:

    import Foundation
    
    class MyVideo: Object{
    
    var fileName: String?
    var fileExtension: String?
    
    func setVideoFileData(filePath: String){
        self.fileExtension = filePath.pathExtension
        self.fileName      = filePath.stringByDeletingPathExtension
    }
    
    
    }
    

    And then in the my ViewController something like this:

    var VideoCollection: Array<MyVideo>()
    
    var newVideo = MyVideo()
    newVideo.setVideoFileData("file.mp4")
    VideoCollection.append(newVideo)
    
    for video in VideoCollection{
        let videoURL: NSURL = NSBundle.mainBundle().URLForResource(video.fileName!, withExtension: video.fileExtension!)!
    }
    

    Of course you would first check those optionals fileName and fileExtension for nil instead of force unwrapping.

    This would allow using your videos with Sequence Types.

    This solution worked for what I needed, not sure it will be suited for what you need, but just an idea. Best of luck!