我正在使用XCode 9.3和Swift 4开发iOS应用程序。
我有一个设置,我有一个屏幕,剧集视图,有关于特定视频的信息。
对于未登录的用户,该屏幕包含:
“登录”按钮可打开模式。每当一个 用户点击此按钮,用户应该登录,然后返回到剧集视图,其中图像应该由视频替换 玩家(我们使用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")
}
即使代码一直运行到最后,并且隐藏了初始图像,也永远不会加载播放器。然而,如果我然后导航到不同的部分,然后返回到剧集视图,视频加载没有问题。此外,如果用户在之前登录时运行应用程序,则视频加载正常。只需从视频播放器无法加载的模态登录屏幕返回即可。
我检查了输出屏幕,得到了以下内容:
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
答案 0 :(得分:1)
正如NSGangster在评论中所回答的那样:
第一次进入VC(未登录)时,设置containerView.isHidden = true。你可能应该在某个地方将它设置为false,如果你想让它显示回来的话。这就是为什么我认为如果你重新加载VC它会起作用的原因。