简体   繁体   English

无法快速使用 GCDWebServer 在网络浏览器上获取视频

[英]unable to get the video on web browser using GCDWebServer in swift

I created a small demo app where app access the image and videos from iPhone using UIImagePickerController.我创建了一个小型演示应用程序,其中应用程序使用 UIImagePickerController 从 iPhone 访问图像和视频。 When I select any image or video, app creates a copy of it( image or video) in document directory.当我选择任何图像或视频时,应用程序会在文档目录中创建它的副本(图像或视频)。 And create a web server on iPhone using GCDWebserver and need to expose the selected image or video.并使用 GCDWebserver 在 iPhone 上创建一个 Web 服务器,并需要公开选定的图像或视频。 But it does not work.但它不起作用。

Here is the sample code, not sure where I could be wrong.这是示例代码,不知道我哪里错了。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var btnselect: UIButton!
let videoPicker = UIImagePickerController()

@IBAction func btnSelect(_ sender: Any) {

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = .photoLibrary
            myPickerController.allowsEditing = false
            self.present(myPickerController, animated: true, completion: nil)
        }

    }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    // 1. saving video to documents directory

      let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL
            let videoData = NSData(contentsOf: videoURL as URL)
            let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)

            let newPath = path.appendingPathComponent("/myTestVideo.mp4")
            do {
                try videoData?.write(to: newPath)
            } catch {
                print(error)
            }


 // 2. Create web server on iPhone using GCDWebServer

        let webServer = GCDWebServer()
        webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
        return GCDWebServerDataResponse(newPath)

       })
       webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

}
}

I need this selected (image or video) to be hosted/accessible by this GCD Web Server.我需要此 GCD Web 服务器托管/访问此选定的(图像或视频)。

You can use GCDWebServerDataResponse to pass the file as the response like this:您可以使用 GCDWebServerDataResponse 将文件作为响应传递,如下所示:

webServer!.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
    let path = Bundle.main.path(forResource: "sample", ofType: "mp4")
    let handler = FileHandle.init(forReadingAtPath: path!)
    return GCDWebServerDataResponse(data: (handler?.readDataToEndOfFile())!, contentType: "video/mp4")  
})

Please notice that when you need to provide the content type and the data of the file.请注意,当您需要提供文件的内容类型和数据时。

Assuming it could be according wrong path假设它可能是根据错误的路径

let newPath = path.appendingPathComponent("/myTestVideo.mp4")

producing ..../Documents//myTestVideo.mp4制作..../Documents//myTestVideo.mp4

Try to remove backslash from appending path component.尝试从附加路径组件中删除反斜杠。

let newPath = path.appendingPathComponent("myTestVideo.mp4")

Also there is small chance that the problem could be related to "file://" prefix.此外,问题可能与“file://”前缀有关的可能性很小。 If previous suggestion will not help try to remove prefix.如果以前的建议无济于事,请尝试删除前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM