- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的应用程序中用 swift 4 和 xcode 9 逐个播放多个视频。我可以使用 avqueue 播放器来做到这一点。但是,一旦播放器播放完所有视频,就无法重播播放器。仅出现一个空白屏幕。
代码:
var selectedVideos : NSMutableArray = []
var videoPlayer: AVQueuePlayer!
var playerLayer: AVPlayerLayer!
var videoQueue: [AVPlayerItem] = []
var currentVideoTime: Double = 0
var previousVideoTime: Double = 0
var total_video_duration: Double = 0.0;
var total_video: Int = 0
@IBOutlet weak var video_container: UIView!
@IBOutlet weak var video_duration: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
setup_video_player()
setup_layer()
NotificationCenter.default.addObserver(self, selector: #selector(self.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
}
func setup_video_player() {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
currentVideoTime = 0
previousVideoTime = 0
total_video_duration = 0
video_duration.text = "00:00"
videoPlayer = nil
videoQueue = []
total_video = selectedVideos.count
var playerItem: AVPlayerItem!
for index in selectedVideos {
print("selected url: \(index as! URL)")
playerItem = AVPlayerItem(url: index as! URL)
videoQueue.append(playerItem)
}
videoPlayer = AVQueuePlayer(items: videoQueue)
for playerItem in self.videoPlayer.items() {
let duration = playerItem.asset.duration
let seconds = CMTimeGetSeconds(duration)
total_video_duration = total_video_duration + seconds
}
let interval = CMTime(seconds: 1, preferredTimescale: 2)
timeObserver = videoPlayer.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { elapsedTime in
self.currentVideoTime = self.previousVideoTime + CMTimeGetSeconds(elapsedTime)
self.video_duration.text = self.video_duration(value: self.currentVideoTime)
})
print("finish setup video palyer")
play_video()
}
func setup_layer() {
if(playerLayer != nil){
playerLayer.removeFromSuperlayer()
}
playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = video_container.bounds
video_container.layer.addSublayer(playerLayer)
print("finish setup layer ")
}
func play_video() {
if videoPlayer == nil {
return
}
videoPlayer.play()
videoPlayer.volume = 1.0
print("playing video")
}
@objc func finishVideo(_ notification: NSNotification) {
previousVideoTime = currentVideoTime
print("current video time: \(currentVideoTime)")
print("total video duration: \(total_video_duration)")
if Int(currentVideoTime) != Int(total_video_duration) {
return
}
play_video()
}
没有错误。但不明白为什么 avqueue 播放器在播放完一次后不重新播放。
最佳答案
By looking at the documentation, I found that There is no direct way to loop through all the AVPlayerItem in AVQueuePlayer. You can use AVPlayerLooper to repeat one AVPlayerItem but can't repeat entire player list.
你能做的是:
在完成的方法中检查您自己的计数器,当它到达结束时,您可以使用函数removeAllItems()删除现有的播放器项目,并将[AVPlayerItem]重新分配给您的AVQueuePlayer实例。
希望有帮助。
关于ios - Swift:在 avqueueplayer 中播放完毕后重播视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711870/
我搜索了很多东西,比如: How to show icon blinking on google map 但是根据这个我没有找到如何让通知图标闪烁 我想在这个上闪烁图标 int icon = R.d
我很想知道我是否可以删除或更改 android 在选项卡布局下方提供的类似 drop-shadow 的效果。 最佳答案 我一问这个问题,我就在另一个stackoverflow post中得到答案.无论
我必须通过 AlaramManager 调用我的 BroadcastReceiver。 为此,我所做的不起作用: AlarmActivity.java public class AlarmActivi
我正在尝试执行以下操作: CPU_COUNT=$(cat /proc/stat | grep -E "^cpu[[:digit:]]+ " | wc -l) let CPU_COUNT=CPU_COU
我是一名优秀的程序员,十分优秀!