Swift에서 AVPlayer를 어떻게 반복합니까?
어떤 이유로 든 답을 찾을 수없는 간단한 질문입니다.
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()
스위프트 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()
}
스위프트 3
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: kCMTimeZero)
self?.player?.play()
}
스위프트 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()
}
잊지 마세요 import AVFoundation
@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()
}
}
그러나 그의 답변 아래에서 언급했듯이 여러 플레이어가있는 경우 개체를 AVPlayer의 플레이어 항목으로 지정해야합니다.
또는 블록 기반 NSNotificationCenter API를 사용할 수 있습니다.
func loopVideo(videoPlayer: AVPlayer) {
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil) { notification in
videoPlayer.seekToTime(kCMTimeZero)
videoPlayer.play()
}
}
좋아 나는 그것을 해결했다. Objective C 답변으로 올바른 방향을 제시해 주신 Msencenb에게 감사드립니다.
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()
}
Starting from iOS 10, there's no need to use notifications to loop a video, simply use a AVPlayerLooper, just like this:
let asset = AVAsset(url: videoURL)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
videoLooper = AVPlayerLooper(player: player, templateItem: item)
make sure this looper is created outside of a function scope, in case it stops when function returns.
I've managed to create a seamless video looping for OSX in swift 3. It should work on iOS and with little modification on swift 2 as well.
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)
}
When you want to change the video, just call selectVideoWithLoop with a different url again.
Swift 3.0:
First check for video end point:-
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()
}
참고URL : https://stackoverflow.com/questions/27808266/how-do-you-loop-avplayer-in-swift
'code' 카테고리의 다른 글
ORA-01461 : LONG 열에 삽입하기 위해서만 LONG 값을 바인드 할 수 있습니다-질의시 발생합니다 (0) | 2020.11.22 |
---|---|
솔루션에서 TFS 연결 제거 (0) | 2020.11.22 |
자바 십진 문자열 형식 (0) | 2020.11.22 |
반올림 정수 나누기 (잘라 내기 대신) (0) | 2020.11.22 |
Ubuntu 16.04 LTS 64 비트에서 Postman 기본 v4.10.3을 설치 / 시작하는 방법은 무엇입니까? (0) | 2020.11.22 |