ios - 录制视频并上传到 Firebase

标签 ios swift video firebase

有人知道如何将视频上传到 firebase 吗? 我使用了这个链接:record video in swift

我成功录制并在同一 View 中播放 Gif of my video on giphy

播放我刚刚录制的视频的代码是:

    @IBAction func playVideo(sender: AnyObject) {
    print("Play a video")

    // Find the video in the app's document directory
    let paths = NSSearchPathForDirectoriesInDomains(
        NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory: AnyObject = paths[0]
    let dataPath = documentsDirectory.stringByAppendingPathComponent(saveFileName)

    let videoAsset = (AVAsset(URL: NSURL(fileURLWithPath: dataPath)))
    let playerItem = AVPlayerItem(asset: videoAsset)

    print(playerItem)


    let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))


    let pathURL = NSURL.fileURLWithPath(dataPath)
    moviePlayer = MPMoviePlayerController(contentURL: pathURL)
    if let player = moviePlayer {
        player.view.frame = videoView.bounds
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        videoView.addSubview(player.view)

    }

    moviePlayer!.view.frame = videoPreview.bounds
    moviePlayer!.view.center = CGPointMake(CGRectGetMidX(videoPreview.bounds), CGRectGetMidY(videoPreview.bounds))
    videoPreview.addSubview((moviePlayer?.view)!)
}

我知道如何将图片上传到 Firebase,我需要使用 NSString,如下所示:

var base64String : NSString!

        //let pic = UIImagePNGRepresentation(UIImage(named: "3")!)
    let picture = UIImageJPEGRepresentation(self.profilePictureImageView.image!, 0.1)!


    self.base64String = picture.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

    let updatedProfileInfo = [
        "provider": USER_REF.authData.provider,
        "email": self.emailTextfield.text!,
        "Username": self.usernameTextfield.text!,
        "ProfilePicture" :  self.base64String,
        "ProfileDescription" : self.bioDescriptionTextfield.text
    ]
    USER_REF.updateChildValues(updatedProfileInfo)

但是你如何处理视频呢?

谢谢

最佳答案

这是我使用 UIImagePicker 对象类选择设备中的视频或图像并将其上传到 Fireabase 的解决方案:

@IBAction func uploadButton(_ sender: Any) {
    // Configuration
    let picker = UIImagePickerController()
    picker.allowsEditing = true
    picker.delegate = self
    picker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]

    // Present the UIImagePicker Controller
    present(picker, animated: true, completion: nil)
}

// The didFinishPickingMediaWithInfo let's you select an image/video and let's you decide what to do with it. In my example, I decided to convert the selected data into video and upload it to Firebase Storage
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
        // we selected a video
        print("Here's the file URL: ", videoURL)

        // Where we'll store the video:
        let storageReference = FIRStorage.storage().reference().child("video.mov")

        // Start the video storage process
        storageReference.putFile(videoURL as URL, metadata: nil, completion: { (metadata, error) in
            if error == nil {
                print("Successful video upload")
            } else {
                print(error?.localizedDescription)
            }
        })

      }
            //Dismiss the controller after picking some media
            dismiss(animated: true, completion: nil)
    }

关于ios - 录制视频并上传到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722251/

相关文章:

ios - 按下时向 SKSpriteNode 添加视觉效果

java - .wmv 文件的格式。 (或者任何视频文件都很棒)

php - 如何在 PHP 中计算视频长度和大小?

objective-c - iOS:接受多个对象类型作为参数(是)对象?

android - 有什么工具可以将 html5 游戏编译成本地移动设备

swift - 将 Google map 添加到 Swift 中的 View

video - 剪切多个视频并与 ffmpeg 合并

iphone - UITableView dataSource 必须从 tableView :cellForRowAtIndexPath: 返回一个单元格

ios - 来自另一个类的值

ios - 如何更改数字键盘的位置?