1

I am using JSQMessage and pare to send and save video files

if (mediaType == kUTTypeVideo as String) || (mediaType ==  kUTTypeMovie as String) {

        let movieURL = info[UIImagePickerControllerMediaURL] as? NSURL
        let videoMediaItem = JSQVideoMediaItem.init(fileURL: movieURL, isReadyToPlay: true)
        let message = JSQMessage(senderId: self.senderId, senderDisplayName: senderDisplayName, date: NSDate(), media: videoMediaItem)
        self.saveMediaToParse(message, data: NSData(contentsOfURL: movieURL!)!, fileName:"videoFile")
        self.messages += [message]
        self.publishMessageToChannel(message)
        self.finishSendingMessage()
    }  


func saveMediaToParse(message:JSQMessage, data:NSData, fileName:String) {

    let imageFile = PFFile.init(data: data)
    let msg = PFObject(className: "test")
    msg["senderId"] = message.senderId
    msg[fileName] = imageFile
    msg["senderDisplayName"] = message.senderDisplayName
    msg["user"] = PFUser.currentUser()

    msg.saveInBackgroundWithBlock { (success, error) -> Void in
        if(error != nil) {
            NSLog("error: %@",(error?.localizedDescription)!)
        }
    }
}

when I am trying to get the video URL from the data as String I get back nil value. What am I doing wrong?

message["videoFile"].getDataInBackgroundWithBlock({ (data, error) -> Void in
                        if let data = data where error == nil {
                            **let dataStr = String(data: data, encoding: NSUTF8StringEncoding)**

                            let movieUrl = NSURL(string: dataStr!)
                            let videoMediaItem = JSQVideoMediaItem.init(fileURL: movieUrl, isReadyToPlay:true)
                            jsqMessage = JSQMessage(senderId:message["senderId"] as! String, displayName: message["senderDisplayName"] as! String, media: videoMediaItem)
                            self.messages.append(jsqMessage)
                            self.collectionView?.reloadData()
                            self.scrollToBottomAnimated(true)
                        }
                    })
                }
                self.messages.append(jsqMessage)
Ros
  • 144
  • 8
  • Not that I read your question, but the title. Did you actually try using `NSASCIIEncoding` instead of UTF-8? It happened to me before (on Objective-C) that this particular method returned `nil` when the text had weird characters or was in another encoding. – Alejandro Iván Dec 22 '15 at 13:49
  • Are u sure you are doing it right ? you are trying to get URl from video data ?? – Wolverine Dec 22 '15 at 13:54
  • If you want a URL, then you have to store the data @ someplace and then you have to try to get the local url of that. – Wolverine Dec 22 '15 at 13:55
  • When you create a string using an particular encoding, if the creation fails it is due to the data not matching the encoding. The data likely has non UTF-8 bytes in it or may not even be a string. – ahwulf Dec 22 '15 at 14:01
  • 1.Tried NSASCIIEncoding, returns weird characters 2. No I am not sure, do you know how I can play the video from data? What I have is the result of - NSData(contentsOfURL: movieURL!)! – Ros Dec 22 '15 at 14:51

1 Answers1

1

Ended up doing let dataStr = NSTemporaryDirectory() + "temp.mov" data.writeToFile(dataStr, atomically: true) let movieUrl = NSURL(fileURLWithPath: dataStr)

Instead of let dataStr = String(data: data, encoding: NSUTF8StringEncoding)**

Ros
  • 144
  • 8