(以下內容均翻譯自英文版文件,最新資訊及內容敬請切換至英文語系參考原文)
初始化
播放控制
設定播放器參數
取得播放器參數
設置事件監聽器/回調
播放器界面
全螢幕 | 管理自動播放控制 | 顯示播放控制 | uiStyle配置
初始化
建立
初始化一個播放器實例。
程式碼範例
let playerConfig = PlayerConfig()
player = PlayerFactory.create(playerConfig: playerConfig)
類別定義
public class UniPlayerConfig {
public var key: String? // A BlendVision One license key that can be found in the BOP portal
public var styleConfig: UniStyleConfig // Configures visual presentation and behaviour of the Player UI. A default StyleConfig is set initially.
public var playbackConfig: UniPlaybackConfig // Configures playback behaviour. A default PlaybackConfig is set initially.
public var bufferConfig: UniBufferConfig // Configures buffer settings. A default BufferConfig is set initially.
public var adaptationConfig: UniAdaptationConfig // Configures adaptation logic. A default AdaptationConfig is set initially
public var tweaksConfig: UniTweaksConfig // Configures experimental features. A default TweaksConfig is set initially
}
public final class UniStyleConfig {
public var scalingMode: AVLayerVideoGravity
public var uiStyleConfig: UniUIStyleConfig // Set the property that will be used for the UI.
}
public class UniPlaybackConfig {
public var isAutoplayEnabled: Bool // Whether the player starts playing automatically after loading a source or not. Default value is false.
public var isMuted: Bool // Whether the sound is muted on startup or not. Default value is false.
}
public class UniBufferConfig {
public var audioAndVideo: UniBufferMediaTypeConfig
}
public class UniBufferMediaTypeConfig {
// The amount of data in seconds the player tries to buffer in advance.
// If set to 0, the player will choose an appropriate forward buffer duration suitable for most use-cases.
// Default value is 6.
public var forwardDuration: TimeInterval
public var audioTimePitchAlgorithm: AVAudioTimePitchAlgorithm
}
public final class UniAdaptationConfig {
// The upper bitrate boundary in bits per second for network bandwidth consumption of the played source.
// Can be set to 0 for no limitation.
// Default value is 0.
public var maxSelectableBitrate: UInt = 0
}
public final class UniTweaksConfig {
var timeChangedInterval: TimeInterval // Default is 1. Minimum is 0.025.
/// This is useful if the duration of the segments does not match the duration specified in the manifest.
/// In this case, if we try to seek to the end,
/// AVPlayer could get stuck and might stall forever Therefore increasing this value could help.
var seekToEndThreshold: TimeInterval
}
自: 2.1.0
消毀
銷毀播放器並釋放所有分配的資源。呼叫此方法後不得使用播放器實例。
func destroy()
自: 2.1.0
加載
設置一個新的影片源,並回傳一個解析為播放器的承諾。
程式碼範例
player.load(sourceConfig)
類別定義
public class UniSourceConfig { public let url: URL public let type: UniSourceType public var title: String? // The title of the video source. public var sourceDescription: String? // The description of the video source. public var posterSource: URL? // The URL to a preview image displayed until the video starts. public var drmConfig: UniDrmConfig? // The DRM config for the source. public var thumbnailTrack: UniThumbnailTrack? // The thumbnail track for this source config. public let isPSEEnabled: Bool public var sharedURL: URL? public var features: [UniFeature] = [] } public class UniDrmConfig { public var licenseUrl: URL? // The DRM license acquisition URL. } public class UniFairPlayConfig: UniDrmConfig { public var certificateUrl: URL? // The URL to the FairPlay Streaming certificate of the license server. public var licenseRequestHeaders: [String: String]? // A dictionary to specify custom HTTP headers for the license request. public var certificateRequestHeaders: [String: String]? // A dictionary to specify custom HTTP headers for the certificate request. } public class UniThumbnailTrack: UniTrack { // Refer to UniTrack } public enum UniFeature: String { case d3 = "D3" case pse = "PSE" }
自: 2.1.0
來源
播放控制
播放
開始播放或暫停後繼續播放。如果播放器已經在播放,則沒有任何效果。
func play()
自: 2.1.0
暫停
暫停正在播放的影片。如果播放器已經暫停,則沒有效果。
func pause()
自: 2.1.0
跳轉
跳轉至指定的播放時間。
跳轉到參數 time(秒)指定的播放時間。不得大於影片的總時長。在觀看直播串流時沒有影響,因為無法跳轉。
func seek(time: TimeInterval)
自: 2.1.0
快轉
快轉至參數 time(秒)指定的播放時間。不得大於影片的總時長。
func forward(_offset: TimeInterval)
自: 2.1.0
倒帶
倒帶至參數 time 指定的播放時間(以秒為單位)。不得大於影片的總時長。
func rewind(_offset: TimeInterval)
自: 2.1.0
正在播放
如果播放器正在播放,即已經開始且未暫停,則回傳 true
。
var isPlaying: Bool { get }
自: 2.1.0
播放器參數
音量
取得 / 設定播放器的音量,介於 0(無聲)和 100(最大音量)之間。該屬性用於控制播放器相對於系統音量的音訊音量。
var volume: Int { get set }
自: 2.1.0
静音
如果播放器處於靜音狀態,則回傳 true
。
var isMuted: Bool { get set }
自: 2.1.0
當前時間
以秒為單位返回當前播放時間。對於 VOD 串流,返回的時間範圍為 0 到資產的持續時間。對於直播串流,則返回表示當前播放位置的 Unix 時間戳記。
var currentTime: TimeInterval { get }
自: 2.1.0
時長
如果是直播至 VOD,影片源的持續時間(以秒為單位);如果是直播,則為 Double.infinity
。
如果沒有或不知道時長,默認值為 0。
var duration: TimeInterval { get }
自: 2.1.0
可用的影片畫質
返回一個數組,其中包含播放器可支援的所有可用影片畫質。
var availableVideoQualities: [UniVideoQuality] { get }
自: 2.1.0
影片畫質
回傳當前選擇的影片畫質。
var videoQuality: UniVideoQuality
? { get }
since: 2.1.0
最大可選位元率
位元率上限以每秒比特為單位,用於當前播放源的網路頻寬消耗。可設定為 0 以自動變更設定檔。在此設定的值只對目前播放的影片源有效,不會延續到隨後載入的影片源。
默認值為 0。
var maxSelectableBitrate: UInt { set }
註記:啟用 PSE 時,不允許設定畫質為 1080p 或更高畫質。
自: 2.1.0
可用音訊
var availableAudio: [UniAudioTrack] { get }
自: 2.1.0
音訊
回傳目前使用的音軌。
var audio: UniAudioTrack? { get }
自: 2.1.0
設定音訊
將音軌設置為 audioTrackID 指定的 ID。可通過呼叫 availableAudio 取得列表。
func setAudio(track identifier: String)
自: 2.1.0
可用字幕
回傳包含所有可用字幕軌的 UniSubtitleTrack 對象的數組。
var availableSubtitles: [UniSubtitleTrack] { get }
自: 2.1.0
字幕
回傳當前使用的 UniSubtitleTrack。
var subtitle: UniSubtitleTrack { get }
自: 2.1.0
設置字幕
將字幕軌設定為 trackID 所指定的 ID。可通過呼叫 availableSubtitles 取得列表。
使用 nil 作為 ID 會停用字幕。
func setSubtitle(track identifier: String)
自: 2.1.0
播放速度
取得/設定播放器的播放速度。支持快轉、慢轉和倒轉播放。
-
數值在 0 和 1 之間時使用慢動作,數值大於 2 時使用快進,數值在 0 和 -1 之間時使用慢倒退,數值小於 -1 時使用快倒退。
-
在 Casting 過程中,負值將被忽略。
-
在反向播放過程中,播放將繼續進行,直到到達活動信號源的開頭。到達信號源開頭時,播放將暫停,播放速度將重置為默認值 1。
var playbackSpeed: Float { get set }
自: 2.1.0
UniSource - 縮圖
表示包含類似 UniThumbnail 的縮圖數據的軌道。
func thumbnail(forTime time: TimeInterval) -> UniThumbnail?
UniThumbnail
let start: TimeInterval { get } let end: TimeInterval { get } let text: String { get } let url: URL { get } let px: UInt { get } let py: UInt { get } let width: UInt { get } let height: UInt { get }
自: 2.1.0
播放/錯誤事件回調監聽器
監聽播放過程中發生的播放回調事件。該方法提供了函數回調和監聽器,用於取得播放器的狀態。例如,在取得緩衝狀態後,可以取得其優先圖示。
播放事件監聽器
// Listen to playback events (includes error events)
player.add(UniPlayerListener)
// Implement delegate didReceiveOnEvent
public func player(_ player: UniPlayer, didReceiveOnEvent event: UniEvent) {
// do something
}
public protocol UniPlayerListener{
func player(_ player: UniPlayer, didReceiveOnEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnReadyEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnPlayingEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnPausedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnTimeChangedEvent event: UniTimeChangedEvent)
func player(_ player: UniPlayer, didReceiveOnSeekEvent event: UniSeekEvent)
func player(_ player: UniPlayer, didReceiveOnSeekedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnStallStartedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnStallEndedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnPlaybackFinishedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveOnAudioChangedEvent event: UniAudioChangedEvent)
func player(_ player: UniPlayer, didReceiveOnSubtitleChangedEvent event: UniSubtitleChangedEvent)
func player(_ player: UniPlayer, didReceiveOnSourceLoadEvent event: UniSourceLoadEvent)
func player(_ player: UniPlayer, didReceiveOnSourceLoadedEvent event: UniSourceLoadedEvent)
func player(_ player: UniPlayer, didReceiveOnVideoQualityChangedEvent event: UniVideoPlaybackQualityChangedEvent)
func player(_ player: UniPlayer, didReceivePlaybackSpeedChangedEvent event: UniPlaybackSpeedChangedEvent)
func player(_ player: UniPlayer, didReceiveSourceErrorEvent event: UniSourceErrorEvent)
func player(_ player: UniPlayer, didReceiveMutedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveUnmutedEvent event: UniEvent)
func player(_ player: UniPlayer, didReceiveAudioVolumeChangedEvent event: UniAudioVolumeChangedEvent)
func player(_ player: UniPlayer, didReceiveSourceUnloadEvent event: UniSourceUnloadEvent)
func player(_ player: UniPlayer, didReceiveSourceUnloadedEvent event: UniSourceUnloadedEvent)
func player(_ player: UniPlayer, didReceiveErrorEvent event: UniPlayerErrorEvent)
func player(_ player: UniPlayer, didReceiveTimeShiftEvent event: UniTimeShiftEvent)
func player(_ player: UniPlayer, didReceiveTimeShiftedEvent event: UniEvent)
}
播放紀錄事件回調
通過這種方法可以取得紀錄資訊,然後可以整合到紀錄服務中
UniLogger.shared.add(UniLoggerListener) extension AppDelegate: UniLoggerListener { func logEvent(_ event: UniLogEvent) { } } public class UniLogEvent: UniLogEventProtocol { public var name: EventName { return .none } public var properties: [UniLogEvent.PropertyName: Any] }
自: 2.1.0
播放器界面
全螢幕
將播放器介面設置為全螢幕。
var orientation: UIInterfaceOrientationMask = .portrait var fullscreen: Bool = false // Handling view transition override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { coordinator.animate { _ in if size.width > size.height { self.playerView?.enterFullscreen() // Will enter landscape } else { self.playerView?.exitFullscreen() // Will enter portrait } } super.viewWillTransition(to: size, with: coordinator) } // Conforming to UniFullscreenHandler protocol extension UniBasicPlaybackViewController: UniFullscreenHandler { var isFullscreen: Bool { return fullscreen } func onFullscreenRequested() { updateLayoutOnFullscreenChange(fullscreen: true) } func onFullscreenExitRequested() { updateLayoutOnFullscreenChange(fullscreen: false) } private func updateLayoutOnFullscreenChange(fullscreen: Bool) { self.fullscreen = fullscreen orientation = fullscreen ? .landscape : .portrait bottomConstraint.isActive = fullscreen ? true : false if #available(iOS 16.0, *) { setNeedsUpdateOfSupportedInterfaceOrientations() } else { UIViewController.attemptRotationToDeviceOrientation() } } }
自: 2.1.0
管理自動播放控制
自動設定使用者介面狀態。當使用者與之交互時顯示控制器,3 秒內無交互時顯示控制器。控制器包括進度條、跳轉功能、標題、後退按鈕、播放、暫停、倒帶、縮圖跳轉等。
- True: 默認值
- False: 如果不想讓播放器自動管理播放控制,請將此屬性設置為 false
var managesPlaybackControlsAutomatically: Bool
自: 2.1.0
顯示播放控制
如果不想在內容上看到系統提供的播放控制,請將此屬性設置為 false。在需要非交互式影片演示(如影片啟動畫面)的情況下,隱藏播放控件會很有用。默認值為 true。
var showsPlaybackControls: Bool
自: 2.1.0
UniStyle 配置
包含可用於改變播放器使用者介面的視覺效果和行為的配置值。
- 縮放模式(ScalingMode): 決定在父容器的範圍內如何縮放或拉伸影片內容。
- uiStyleConfig
var scalingMode: AVLayerVideoGravity { get set }
var uiStyleConfig: UniUIStyleConfig { get set }
- UniUIStyleConfig.trackTintColor: 更改控制器顏色
var trackTintColor: UIColor { get set }
自: 2.1.0
取得设置到播放器中的所有串流内容原始数据。
程式碼範例
自:
2.1.0
< 返回