简单的问题我似乎无法找到答案的原因.
你如何在Swift中循环AVPlayer?
numberOfLoops = -1仅适用于AVAudioPlayer
我确实需要它循环没有任何延迟/黑色闪光等.这就是为什么我没有使用MPMoviePlayerViewController.
谢谢你的帮助.
码:
let url_1 = NSURL.fileURLWithPath(outputFilePath_1)
let asset_1 = AVAsset.assetWithURL(url_1) as? AVAsset
let playerItem_1 = AVPlayerItem(asset: asset_1)
let player_1 = AVPlayer(playerItem: self.playerItem_1)
let playerLayer_1 = AVPlayerLayer(player: self.player_1)
playerLayer_1!.frame = self.view.frame
self.view.layer.addSublayer(self.playerLayer_1)
player_1!.play()
Run Code Online (Sandbox Code Playgroud)
Ale*_*kov 149
斯威夫特4
var player: AVPlayer!
...
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: CMTime.zero)
self?.player?.play()
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: kCMTimeZero)
self?.player?.play()
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特2
配置AVPlayerItem并创建播放器后:
var player: AVPlayer!
...
// Invoke after player is created and AVPlayerItem is specified
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: self.player.currentItem)
...
func playerItemDidReachEnd(notification: NSNotification) {
self.player.seekToTime(kCMTimeZero)
self.player.play()
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*ahl 28
@Christopher Pickslay的答案更新为Swift 4:
func loopVideo(videoPlayer: AVPlayer) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
videoPlayer.seek(to: CMTime.zero)
videoPlayer.play()
}
}
Run Code Online (Sandbox Code Playgroud)
但是,正如我在下面提到的那样,如果你有多个玩家,请务必将对象指定为AVPlayer的玩家项目.
Chr*_*lay 26
或者,您可以使用基于块的NSNotificationCenter API:
func loopVideo(videoPlayer: AVPlayer) {
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil) { notification in
videoPlayer.seekToTime(kCMTimeZero)
videoPlayer.play()
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*ett 23
好的,我已经解决了.感谢Msencenb用Objective C答案指出了正确的方向.
player_1?.actionAtItemEnd = .None
//set a listener for when the video ends
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "restartVideoFromBeginning",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: player_1?.currentItem)
//function to restart the video
func restartVideoFromBeginning() {
//create a CMTime for zero seconds so we can go back to the beginning
let seconds : Int64 = 0
let preferredTimeScale : Int32 = 1
let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)
player_1!.seekToTime(seekTime)
player_1!.play()
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*ure 23
从iOS 10开始,无需使用通知来循环播放视频,只需使用AVPlayerLooper,就像这样:
let asset = AVAsset(url: videoURL)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
videoLooper = AVPlayerLooper(player: player, templateItem: item)
Run Code Online (Sandbox Code Playgroud)
确保在函数范围之外创建此循环器,以防函数返回时停止.
Swi*_*yle 12
斯威夫特 5.5:
func loopVideo(videoPlayer: AVPlayer) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
videoPlayer.seek(to: .zero)
videoPlayer.play()
}
}
Run Code Online (Sandbox Code Playgroud)
我已经设法在swift 3中为OSX创建了一个无缝的视频循环.它应该可以在iOS上运行,并且在swift 2上也几乎没有修改.
var player : AVQueuePlayer!
// Looping video initial call
internal func selectVideoWithLoop(url : URL)
{
let asset = AVAsset(url: url)
player.pause()
let playerItem1 = AVPlayerItem(asset: asset)
let playerItem2 = AVPlayerItem(asset: asset)
player.removeAllItems()
player.replaceCurrentItem(with: playerItem1)
player.insert(playerItem2, after: playerItem1)
player.actionAtItemEnd = AVPlayerActionAtItemEnd.advance
player.play()
let selector = #selector(ViewController.playerItemDidReachEnd(notification:))
let name = NSNotification.Name.AVPlayerItemDidPlayToEndTime
// removing old observer and adding it again for sequential calls.
// Might not be necessary, but I like to unregister old notifications.
NotificationCenter.default.removeObserver(self, name: name, object: nil)
NotificationCenter.default.addObserver(self, selector: selector, name: name, object: nil)
}
// Loop video with threadmill pattern
// Called by NotificationCenter, don't call directly
func playerItemDidReachEnd(notification: Notification)
{
let item = player.currentItem!
player.remove(item)
item.seek(to: kCMTimeZero)
player.insert(item, after: nil)
}
Run Code Online (Sandbox Code Playgroud)
如果要更改视频,只需再次使用其他网址调用selectVideoWithLoop.
Swift 3.0:
首先检查视频终点: -
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
func videoDidReachEnd() {
//now use seek to make current playback time to the specified time in this case (O)
let duration : Int64 = 0 (can be Int32 also)
let preferredTimeScale : Int32 = 1
let seekTime : CMTime = CMTimeMake(duration, preferredTimeScale)
player!.seek(to: seekTime)
player!.play()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42597 次 |
最近记录: |