检测到iBeacon后如何播放视频? [英] How to play a video after iBeacon detection?

查看:93
本文介绍了检测到iBeacon后如何播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iBeacon和Swift开发的新手,我的应用程序存在一些问题.在下面的代码中,我尝试检测iBeacon,当我检测到Beacon的次要值时,将其附加到我要播放的链接上.当我启动程序时出现问题.视频开始播放一秒钟,然后整个应用程序停止,并出现以下错误:

I am new in the iBeacon and Swift development and I have some problems with my application. In the following code bellow, I am trying to detect iBeacon and when I detect the minor value of the beacon I attach it to the link I want to play. The problem occurs when I start the program. The video starts playing for a second and then the whole application stops with the following error:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序试图以模态形式呈现活动控制器.libc ++ abi.dylib:以NSException类型的未捕获异常终止.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .libc++abi.dylib: terminating with uncaught exception of type NSException.

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?

var avPlayerViewController = AVPlayerViewController()
var avPlayer:AVPlayer?

let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "8492E75F-4FD6-469D-B132-043FE94921D8")!, identifier: "Estimotes")


   let videos = [


    19987: NSURL ( string: "http://techslides.com/demos/sample-videos/small.mp4"),


                ]



   func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

    let knownBeacons = beacons.filter{ $0.proximity != CLProximity.unknown }
    if (knownBeacons.count > 0) {
        let closestBeacon = knownBeacons[0] as CLBeacon

        if let url = self.videos[closestBeacon.minor.intValue] {


            self.avPlayer = AVPlayer(url: url as! URL)
            self.avPlayerViewController.player = self.avPlayer

        }
        self.present(self.avPlayerViewController,animated: true) { () -> Void in
            self.avPlayerViewController.player?.play()
        }
    }
}

请问我如何解决该错误?

Would you please advice me how to fix that error?

推荐答案

问题在于,每秒调用一次func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) 范围回调.因此,用于演示视频播放器的代码将在每一秒执行一次.第二.这可能第一次起作用,但是第二次出现错误,因为对self.present(...)的调用试图呈现与您已经呈现的视图控制器相同的视图. 您不能一遍又一遍地显示相同的视图控制器.

The problem is that the func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) ranging callback gets called every second. So your code to present the video player will execute every second. This might work the first time, but the second time, you'll get an error because the call to self.present(...) is trying to present the same view controller that you already presented. You can't present the same view controller over and over again.

解决方案取决于您希望它如何工作.您是否只希望此演示文稿发生一次?如果是这样,您可以这样做:

The solution depends on how you want this to work. Do you just want this presentation to happen once? If so, you could do:

if (knownBeacons.count > 0) {
    let closestBeacon = knownBeacons[0] as CLBeacon

    if let url = self.videos[closestBeacon.minor.intValue] {

        // only execute this code once, if avPlayer not created yet
        if self.avPlayer == nil { 
            self.avPlayer = AVPlayer(url: url as! URL)
            self.avPlayerViewController.player = self.avPlayer
            self.present(self.avPlayerViewController,animated: true) { () -> Void in
                self.avPlayerViewController.player?.play()
            }
        }
    }
}

这篇关于检测到iBeacon后如何播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆