P9MediaManager
基于 AVFoundation 的媒体管理库,用于轻松处理和管理资源。
安装
您可以从我们的发布页面下载最新框架文件。P9MediaManager 也可通过 CocoaPods 获得。安装它只需将以下行添加到 Podfile 中即可。pod ‘P9MediaManager’
设置
无需设置即可使用 P9MediaManager。 :) 如果您使用 Objective-C 的话,则像通常那样导入生成的头文件 -Swift.h。
播放
您可以通过 P9MediaView 和 UIImageView 使用两种方式通过 P9MediaManager 播放媒体。
使用 P9MediaView,
在您想要的位置声明 P9MediaView(也可以通过设置自定义类在 XIB 上使用)。
let playView = P9MediaView(frame: CGRect(x: 0, y: 0, width: 640, height: 480))
@IBOutlet weak var playView: P9MediaView!
通过资源 URL 和密钥设置播放器,并将其放置在播放视图上。
let myResourceUrl = URL(string: "https://sample-videos.com/video123/mp4/480/big_buck_bunny_480p_10mb.mp4")!
let myResourceKey = "MyUniqueResourceKeyToHandlingGivenResourceUrl"
if P9MediaManager.shared.setPlayer(resourceUrl: myResourceUrl, force: false, forKey: myResourceKey) == true {
P9MediaManager.shared.placePlayer(forKey: myResourceKey, toView: playView, mute: false, loop: false, autoPlayWhenReadyFlag: true)
}
如果您不需要显示视频,可以直接调用播放而无需放置播放器。
if P9MediaManager.shared.setPlayer(resourceUrl: myResourceUrl, force: false, forKey: myResourceKey) == true {
P9MediaManager.shared.playPlayerWhenReady(loop:false, forKey: myResourceKey)
}
P9MediaManager 支持 UIImageView 扩展。因此,您只需通过设置资源 URL 即可播放媒体资源,而无需关心其资源,就像使用 UIImage 在 UIImageView 上一样。
imageView.p9MediaPlay(resourceUrl: myResourceUrl, mute: true, loop: true, autoPlayWhenReady: true, rewindIfPlaying: false)
您还可以观察 P9MediaManager 事件来处理业务逻辑。
NotificationCenter.default.addObserver(self, selector: #selector(self.p9MediaManagerNotificationHandler(notification:)), name: .P9MediaManager, object: nil)
@objc func p9MediaManagerNotificationHandler(notification:Notification) {
guard let userInfo = notification.userInfo, let key = userInfo[P9MediaManagerNotificationKey] as? String, let event = userInfo[P9MediaManagerNotificationEvent] as? P9MediaManagerEvent else {
return
}
switch event {
case standby, standbyAgain, standbyAlready:
print("standby \(key).")
let resourceUrl = userInfo[P9MediaManagerNotificationResourceUrlString] as? String ?? ""
case .place, .placeAgain, .placeAlready:
print("place \(key).")
case .displace:
print("displace \(key).")
case .fail:
print("fail \(key).")
case .readyToPlay:
print("ready to play \(key).")
let haveVideoTrack = userInfo[P9MediaManagerNotificationHaveVideoTrack] as? Bool ?? false
let haveAudioTrack = userInfo[P9MediaManagerNotificationHaveAudioTrack] as? Bool ?? false
let currentSeconds = userInfo[P9MediaManagerNotificationCurrentSeconds] as? Int64 ?? 0
let amountSeconds = userInfo[P9MediaManagerNotificationAmountSeconds] as? Int64 ?? 0
case .play:
print("play \(key).")
case .playing, .seek:
print("playing \(key).")
let currentSeconds = userInfo[P9MediaManagerNotificationCurrentSeconds] as? Int64 ?? 0
let amountSeconds = userInfo[P9MediaManagerNotificationAmountSeconds] as? Int64 ?? 0
case .pause:
print("pause \(key).")
case .mute:
print("mute \(key).")
let mute = userInfo[P9MediaManagerNotificationMute] as? Bool ?? false
case .volumn:
print("volumn \(key).")
let volumn = userInfo[P9MediaManagerNotificationVolumn] as? Float ?? 0
case .buffering:
print("buffering \(key).")
let availableSeconds = userInfo[P9MediaManagerNotificationAvailableSeconds] as? Int64 ?? 0
case .pending:
print("pending \(key).")
case .playToEnd, .playToEndAndWillRewind:
print("play to end \(key).")
default:
break
}
}
控制
以下是您可以使用的控制函数。
// play player for given key.
func playPlayer(forKey key:String) -> Bool
// pause player for given key.
func pausePlayer(forKey key:String) -> Bool
// pause all players.
func pauseAllPlayers()
// pause all players except player for given key.
func pauseAllPlayersExceptOne(forKey remainKey:String)
// set display or not closed caption of player for given key.
func setClosedCaptionDisplay(enabled:Bool, forKey key:String) -> Bool
// get subtitle names of player for given key.
func subtitleDisplayNamesOfPlayer(forKey key:String) -> [String]?
// select subtitle of player for given key by display name.
func selectSubtitle(byDisplayName displayName:String?, forKey key:String) -> Bool
// get current seek time of player for given key.
func seekTimeOfPlayer(forKey key:String) -> Int64
// set seek time of player for given key by time value and scale.
func setSeekTimeOfPlayer(seekTimeValue:Int64, seekTimeScale:Int32=1, forKey key:String) -> Bool
// set seek time of player for given key by time rate.
func setSeekTimeOfPlayer(rate:Float, forKey key:String) -> Bool
// get mute status of player for given key.
func isMutedPlayer(forKey key:String) -> Bool
// set mute sttuse of player for given key.
func setMutePlayer(mute:Bool, forKey key:String) -> Bool
// get volume value of player for given key.
func volumeOfPlayer(forKey key:String) -> Float
// set volume value of player for given key.
func setVolumeOfPlayer(volume:Float, forKey key:String) -> Bool
// get playing rate of player for given key.
func playRateOfPlayer(forKey key:String) -> Float
// set playing rate of player for given key.
func setPlayRateOfPlayer(_ rate:Float, forKey key:String) -> Bool
// check the player ready for given key.
func havePlayer(forKey key:String) -> Bool
// get URL of player resource for given key.
func resourceUrlOfPlayer(forKey key:String) -> URL?
// get tracking infomation data from player for given key.
func trackInfoOfPlayer(forKey key:String) -> [String:Any]?
// check placing status of player for given key.
func isPlacedPlayer(forKey key:String) -> Bool
// check playing status of player for given key.
func isPlayingPlayer(forKey key:String) -> Bool
// get amount of seconds of player resource for given key.
func amountSecondsOfPlayer(forKey key:String) -> Int64
// get current second of player for given key.
func currentSecondsOfPlayer(forKey key:String) -> Int64
// get custom value of key you've setted to player for given key.
func customValue(forKey customKey:String, ofPlayerKey playerKey:String) -> Any?
// set custom value for key to player for given key.
func setCustom(value:Any, forKey customKey:String, ofPlayerKey playerKey:String) -> Bool
// remove custom value for key from player for given key.
func removeCustomValue(forKey customKey:String, ofPlayerKey playerKey:String) -> Bool
快照
您可以从媒体资源URL获取快照图像,无需创建播放器。
P9MediaManager.shared.snapshotImage(ofResourceUrl: url, pickSecond: 2, useMemoryCache: true) { (resourceUrl, pickedSecond, snapshotImage) in
self.imageView.image = snapshotImage
}
您还可以从正在播放的媒体当前帧获取快照图像。
if let snapshotImage = P9MediaManager.shared.snapshotPlayer(forKey: myResourceKey) {
self.imageView.image = snapshotImage
}
许可证
适用于本文件的MIT许可证。请参阅http://en.wikipedia.org/wiki/MIT_License。