244 lines
13 KiB
Swift
244 lines
13 KiB
Swift
//
|
|
// JWPlayerManager.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 24/06/24.
|
|
//
|
|
|
|
import UIKit
|
|
import JWPlayerKit
|
|
|
|
enum VideoContentType{
|
|
case liveStream
|
|
case webSeries
|
|
case trailer
|
|
case masilaSongs
|
|
case continueWatching
|
|
case audioBooks
|
|
case games
|
|
case songs
|
|
}
|
|
|
|
struct JwPlayerItemCreate{
|
|
var url : String
|
|
var poster : String?
|
|
var titles : String?
|
|
}
|
|
|
|
class JWPlayerManager {
|
|
static let shared = JWPlayerManager()
|
|
|
|
private init() {}
|
|
|
|
func presentPlayer(from viewController: UIViewController,playerItems : [JwPlayerItemCreate], startIndex: Int = 0, contentType : VideoContentType , videoIDs : [Int],catID : Int? = nil, completion: (() -> Void)? = nil) {
|
|
|
|
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
|
|
let playerVC = sb.instantiateViewController(identifier: K.StoryBoardID.Theme.playerVC) as! PlayerVC
|
|
|
|
do {
|
|
// Create an array to hold the JWPlayerItems
|
|
var items: [JWPlayerItem] = []
|
|
|
|
// Ensure the liveStreamURLs and titles arrays have the same count, if titles are provided
|
|
// if let titles = titles, titles.count != liveStreamURLs.count {
|
|
// print("Titles count does not match URLs count")
|
|
// Utilities.dismissProgressHUD()
|
|
// return
|
|
// }
|
|
|
|
switch contentType{
|
|
case .webSeries,.songs, .masilaSongs:
|
|
// Iterate over the liveStreamURLs to create JWPlayerItems
|
|
for (index, singleItem) in playerItems.enumerated() {
|
|
guard let url = URL(string: singleItem.url) else {
|
|
print("Invalid live stream URL at index \(index)")
|
|
continue
|
|
}
|
|
|
|
// let source = try JWVideoSourceBuilder()
|
|
// .defaultVideo(true)
|
|
// .file(url)
|
|
// .label("0")
|
|
// .build()
|
|
|
|
|
|
let item = try JWPlayerItemBuilder()
|
|
.file(url)
|
|
// .videoSources([source])
|
|
.title(singleItem.titles ?? "")
|
|
.posterImage(URL(string: singleItem.poster ?? "")!)
|
|
.build()
|
|
|
|
items.append(item)
|
|
}
|
|
case .liveStream:
|
|
guard let liveStreamItem = playerItems.first else {
|
|
print("Invalid live stream URL")
|
|
return
|
|
}
|
|
let item = try JWPlayerItemBuilder()
|
|
.file(URL(string: liveStreamItem.url)!)
|
|
.title(liveStreamItem.titles ?? "Trailer")
|
|
.build()
|
|
items.append(item)
|
|
case .trailer:
|
|
guard let liveStreamItem = playerItems.first else {
|
|
print("Invalid live stream URL")
|
|
return
|
|
}
|
|
let item = try JWPlayerItemBuilder()
|
|
.file(URL(string: liveStreamItem.url)!)
|
|
.title(liveStreamItem.titles ?? "Trailer")
|
|
.build()
|
|
items.append(item)
|
|
case .continueWatching, .audioBooks, .games:
|
|
guard let liveStreamItem = playerItems.first else {
|
|
print("Invalid live stream URL")
|
|
return
|
|
}
|
|
let item = try JWPlayerItemBuilder()
|
|
.file(URL(string: liveStreamItem.url)!)
|
|
.title(liveStreamItem.titles ?? "Trailer")
|
|
.build()
|
|
items.append(item)
|
|
}
|
|
|
|
|
|
// Ensure there is at least one valid item
|
|
guard !items.isEmpty else {
|
|
print("No valid items to play")
|
|
Utilities.dismissProgressHUD()
|
|
return
|
|
}
|
|
|
|
// // Create an ad break for 30% into the video
|
|
// let adBreakBuilder2 = JWAdBreakBuilder()
|
|
// .offset(.from(string: "pre")!)
|
|
//
|
|
// .tags([URL(string: generateAdTagUrl())!])
|
|
//
|
|
// guard let adBreak2 = try? adBreakBuilder2.build() else {
|
|
// print("Ad error 1")
|
|
// // Handle build error
|
|
// return
|
|
// }
|
|
//
|
|
// // Create an ad break for 30% into the video
|
|
// let adBreakBuilder1 = JWAdBreakBuilder()
|
|
// .offset(.midroll(percent: 20))
|
|
//// .offset(.from(string: "20%")!)
|
|
//// .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250,728x90&gdfp_req=1&output=vast4&unviewed_position_start=1&env=vp&impl=s&correlator=7898879")!])
|
|
// .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&sz=480x320%7C640x480&ciu_szs=480x320&min_ad_duration=5000&max_ad_duration=20000&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=")!])
|
|
//
|
|
// guard let adBreak1 = try? adBreakBuilder1.build() else {
|
|
// print("Ad error 2")
|
|
// // Handle build error
|
|
// return
|
|
// }
|
|
//
|
|
// let adBreakBuilder3 = JWAdBreakBuilder()
|
|
// .offset(.midroll(percent: 50))
|
|
//// .offset(.from(string: "50%")!)
|
|
// .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/live/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=&nofb=1")!])
|
|
//
|
|
// guard let adBreak3 = try? adBreakBuilder3.build() else {
|
|
// print("Ad error 2")
|
|
// // Handle build error
|
|
// return
|
|
// }
|
|
//
|
|
// let adBreakBuilder4 = JWAdBreakBuilder()
|
|
// .offset(.midroll(percent: 80))
|
|
//// .offset(.from(string: "50%")!)
|
|
// .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=90")!])
|
|
//
|
|
// guard let adBreak4 = try? adBreakBuilder4.build() else {
|
|
// print("Ad error 2")
|
|
// // Handle build error
|
|
// return
|
|
// }
|
|
//
|
|
// let adConfigBuilder = JWImaAdvertisingConfigBuilder()
|
|
// .schedule([adBreak1,adBreak3, adBreak4])
|
|
// guard let adConfig = try? adConfigBuilder.build() else {
|
|
//
|
|
// // Handle build error
|
|
// return
|
|
// }
|
|
|
|
|
|
let finalConfig : JWPlayerConfiguration?
|
|
|
|
switch contentType{
|
|
case .audioBooks:
|
|
let adConfigs = JWAdsBuilder.shareInstace.createAds(adsData: [AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=")!, offset: .midroll(percent: 40)),
|
|
AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/live/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=&nofb=1")!, offset: .midroll(percent: 80)),
|
|
AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=90")!, offset: .midroll(percent: 90))
|
|
])
|
|
|
|
finalConfig = try JWPlayerConfigurationBuilder()
|
|
.playlist(items: items)
|
|
.autostart(true)
|
|
.advertising(adConfigs!)
|
|
.build()
|
|
case .webSeries:
|
|
//https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&sz=480x320%7C640x480&ciu_szs=480x320&min_ad_duration=5000&max_ad_duration=20000&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=
|
|
let adConfigs = JWAdsBuilder.shareInstace.createAds(adsData: [AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=")!, offset: .midroll(percent: 40)),
|
|
AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/live/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=&nofb=1")!, offset: .midroll(percent: 80)),
|
|
AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=90")!, offset: .midroll(percent: 90))
|
|
])
|
|
|
|
finalConfig = try JWPlayerConfigurationBuilder()
|
|
.playlist(items: items)
|
|
.autostart(false)
|
|
.advertising(adConfigs!)
|
|
.build()
|
|
case .masilaSongs:
|
|
let adConfigs = JWAdsBuilder.shareInstace.createAds(adsData: [AdBreakItemStruct(adUrl: URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&ad_type=audio_video&sz=640x480%7C1280x720&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=")!, offset: .preroll())])
|
|
|
|
finalConfig = try JWPlayerConfigurationBuilder()
|
|
.playlist(items: items)
|
|
.autostart(true)
|
|
.advertising(adConfigs!)
|
|
.build()
|
|
case .trailer:
|
|
finalConfig = try JWPlayerConfigurationBuilder()
|
|
.playlist(items: items)
|
|
.autostart(true)
|
|
.build()
|
|
default:
|
|
// Create a JWPlayerConfiguration with the playlist
|
|
finalConfig = try JWPlayerConfigurationBuilder()
|
|
.playlist(items: items)
|
|
.autostart(false)
|
|
.build()
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
playerVC.videoIndex = startIndex
|
|
playerVC.contentType = contentType
|
|
playerVC.config = finalConfig
|
|
playerVC.vm.videoIDs = videoIDs
|
|
playerVC.vm.catID = catID
|
|
playerVC.modalPresentationStyle = .fullScreen
|
|
playerVC.modalTransitionStyle = .crossDissolve
|
|
// Present the PlayerVC
|
|
Utilities.dismissProgressHUD()
|
|
viewController.present(playerVC, animated: true) {
|
|
if contentType == .webSeries{
|
|
playerVC.player.loadPlayerItemAt(index: startIndex)
|
|
}
|
|
completion?()
|
|
// playerVC.transitionToFullScreen(animated: true) {
|
|
// print("FullScreen")
|
|
// }
|
|
}
|
|
}
|
|
} catch {
|
|
print("Error creating JWPlayer configuration: \(error)")
|
|
Utilities.dismissProgressHUD()
|
|
}
|
|
}
|
|
}
|
|
|