Dakata Dakata - 4 days ago 9
Swift Question

How to open URL link in Swift 3?

Hello I am developing a Swift application and I need to start an AudioFile from URL link.

let metadateObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

if metadateObj.type == AVMetadataObjectTypeQRCode {

let BarcodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadateObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
qrCodeframeView?.frame = BarcodeObject.bounds

if metadateObj.stringValue != nil {

Label.text = metadateObj.stringValue


Basically I have a QR code camera which should scan the QR code (link of the mp4 file) and save it as Label.text

Then I use this code :

let movieURL: NSURL? = NSURL ( string: "\(Label.text)")
if let url = movieURL {
self.avPlayer = AVPlayer(url: url as URL)
self.avPlayerViewController.player = self.avPlayer
}


to start the player but it doesn`t play the mp4 file.

From the other hand if I replace the Label.text with the actual link:

let movieURL: NSURL? = NSURL ( string: "http://techslides.com/demos/sample-videos/small.mp4")
if let url = movieURL {
self.avPlayer = AVPlayer(url: url as URL)
self.avPlayerViewController.player = self.avPlayer
}


Here it works perfect: I am so confused I am sure that I am making everything correct because it is not the first time I am doing that. But in this case it is not working. Would you please be able to find my stupid mistake, I am frustrated!!!

Answer

maybe try this:

  OpenLabelButton.isHidden = false
            Label.isHidden = true
            captureSession?.stopRunning()

            label2 = Label.text!

And then

 let movieURL: NSURL? = NSURL ( string: "\(label2)")
            if let url = movieURL {

Hope it works :)

Comments