return 时视频播放器未从模态加载

Video Player not loading upon return from modal

我正在使用 XCode 9.3 和 Swift 4 开发 iOS 应用程序。

我有一个设置,其中有一个屏幕,剧集视图,其中包含有关特定视频的信息。

对于未登录的用户,屏幕包含:

“登录”按钮会打开一个模式。每当一个 用户点击此按钮,用户应该登录,然后 return 到剧集视图,图像应该被视频替换 相应剧集的播放器(我们使用 JW)。

我创建了一个委托,它将在登录时调用 EpisodeViewController 中的一个方法:

func refreshTopView() {
    subscribed = 1
    setUpTopView()
    print("View has refreshed")
}

登录后,代表调用 setUpTopView()

func setUpTopView() {

    if subscribed == 1 {

        // Hide the image view
        episodeImage.isHidden = true

        // Set up URL to video
        let linktoJSON = "https://content.jwplatform.com/feeds/" + (episode?.interviewKey)! + ".json";

        // Set video screen flag to true
        videoScreen = true

        // Get the feed for the video
        getFeed(url: linktoJSON)

    } else {

        // Hide the container view for the video
        containerView.isHidden = true

        // Get the show path and image file and set up the url
        let showPath = episode?.showPath
        let imageFile = episode?.imageFile
        let url = "https://examplesite.com/ios/images/" + showPath! + "/slider/" + imageFile! + ".jpg"

        // Show the image
        episodeImage.sd_setImage(with: URL(string: url))

    }
}

由于用户已被验证为已订阅

,这又会调用 getFeed()
func getFeed(url: String) {
    Alamofire.SessionManager.default
        .requestWithoutCache(url, method: .get).validate().responseJSON { response in
        switch response.result {

        case .success(let value):
            let json = JSON(value)
            self.buildPlaylistSources(json: json)

        case .failure(let error):
            print(error)
        }
    }
}

这将调用为 JW Player 构建播放列表的函数,

func buildPlaylistSources(json: JSON) {
    playlistSources = [[String:String]]()

    if let playlistItems = json["playlist"].array {

        for item in playlistItems {
            if let sources = item["sources"].array {
                // For each source
                var tempSource = [String:String]()

                for source in sources {
                    if let sourceType = source["type"].string {
                        if sourceType.hasPrefix("video"){
                            let key = source["label"].stringValue
                            let value = source["file"].stringValue
                            tempSource[key] = value
                        }
                    }
                }
                playlistSources.append(tempSource)
            }
        }
        createPlayer()
    }
}

然后设置播放器并将其添加到屏幕

func createPlayer() {
    let config: JWConfig = JWConfig()

    let showPath = episode?.showPath
    let imageFile = episode?.imageFile
    let videoImage = "https://examplesite.com/ios/images/" + showPath! + "/slider/" + imageFile! + ".jpg"


    for playlistSource in playlistSources {
        let item = JWPlaylistItem()

        var sourceArray = [JWSource]()

        for (key, value) in playlistSource {
            sourceArray.append(JWSource(file: value, label: key))
        }

        item.sources = sourceArray
        item.image = videoImage

        playlist.append(item)
    }


    config.playlist = playlist
    config.controls = true



    player = JWPlayerController(config: config)
    player.delegate = self

    let frame: CGRect = containerView.bounds

    self.player.view.frame = frame
    self.player.view.autoresizingMask = [UIViewAutoresizing.flexibleBottomMargin, UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleLeftMargin, UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleWidth]

    self.player.forceFullScreenOnLandscape = true
    self.player.forceLandscapeOnFullScreen = true

    containerView.addSubview(player.view)

    print("jwplayer")
}

即使代码一直 运行 到最后,并且隐藏了初始图像,播放器也永远不会加载。但是,如果我然后导航到不同的部分,然后 return 到剧集视图,则视频加载没有问题。此外,如果用户在之前登录的情况下运行该应用程序,则视频加载正常。只是在 return 从模态登录屏幕进入时,视频播放器不会加载。

我检查了输出屏幕并得到以下信息:

2018-04-11 08:52:54.515047-0500 MyAppIOS[8506:1047732] [MediaRemote] [AVOutputContext] WARNING: AVF context unavailable for +[MRAVOutputContext sharedAudioPresentationContext]_block_invoke
2018-04-11 08:52:54.515273-0500 MyAppIOS[8506:1047732] [MediaRemote] [AVOutputContext] WARNING: AVF context unavailable for +[MRAVOutputContext createOutputContextWithUniqueIdentifier:]
2018-04-11 08:52:57.476625-0500 MyAppIOS[8506:1050590] [0x7f95a686f000] Decoding failed with error code -1
2018-04-11 08:52:57.476904-0500 MyAppIOS[8506:1050590] [0x7f95a686f000] Decoding: C0 0x02800168 0x0000354A 0x11111100 0x00000000 16384
2018-04-11 08:52:57.477102-0500 MyAppIOS[8506:1050590] [0x7f95a686f000] Options: 640x360 [FFFFFFFF,FFFFFFFF] 00024060

正如 NSGangster 在评论中的回答:

在第一次访问 VC 时(未登录),您设置 containerView.isHidden = true。如果您希望它显示备份,您可能应该在某处将其设置为 false。这就是为什么我认为如果您重新加载 VC 它会起作用。