Swift. Pass the url from the marker into the controller

بدون دیدگاه


I’m doing the app for viewing IP cameras and want to add cameras to the map so you can click on the desired marker and go to see the desired camera. I have a map and PlayerViewController which reproduce a webcam.

Now each marker only transmits the first stream of the webcam. Help me. How to make different webcam worked?

ViewController

import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

var moscow: [(name: String, URLs:String, img:String, latitude:Double, longitude: Double)] =
    [("cam1", "http://example/1.m3u8", "1.jpg", 55.753989, 37.620235),
     ("cam2", "http://example/2.m3u8", "2.jpg", 55.741308, 37.653914),
     ("cam3","http://example/3.m3u8","3.jpg", 55.742468, 37.629292)]
override func viewDidLoad() {
    super.viewDidLoad()

    var latitudes = moscow.map({ $0.latitude })
    var longitudes = moscow.map({ $0.longitude })
    var annotations = moscow.map({ $0.name })

    for i in 0...2 {
        let coordinate = CLLocationCoordinate2DMake(latitudes[i], longitudes[i])
        let span = MKCoordinateSpanMake(0.003, 0.003)
        let region = MKCoordinateRegionMake(coordinate, span)
        mapView.setRegion(region, animated:true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = annotations[i]
        self.mapView.addAnnotation(annotation)

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    print(#function)
}

// Called when the annotation was added
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.animatesDrop = true
        pinView?.canShowCallout = true
        pinView?.isDraggable = true
        pinView?.pinColor = .purple

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        pinView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    print(#function)
    if control == view.rightCalloutAccessoryView {
        performSegue(withIdentifier: "toTheMoon", sender: self)
    }
}

override  func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toTheMoon" {
        let controller = segue.destination as! PlayerViewController

        var urlll = moscow.map({ $0.URLs })

        for i in 0...2 {
        controller.webcamURL = urlll[i] // only first cam play
        }

    }
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.ending {
        let droppedAt = view.annotation?.coordinate
        print(droppedAt)
    }

}

}

PlayerViewController

import UIKit
import AVFoundation
import AVKit

class PlayerViewController: AVPlayerViewController {

var webcamURL:  String!
var webcamTitle: String!
override func viewDidLoad() {
    super.viewDidLoad()
    self.title = webcamTitle
    let url = URL(string: webcamURL)
    player = AVPlayer(url: url!)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    player!.play()

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.player!.pause()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    player = nil
}

}



لینک منبع

برچسب‌ها: ,,,,
  • نویسنده
    A-brand
  • تعداد بازدید
    19 views
0دیدگاه فرستاده شده است.
شما هم دیدگاه خود را بنویسید