在iOS中使用AVPlayer進行在線播放和控制播放進度條和音量條有以下幾個步驟:
import AVFoundation
let videoURL = URL(string: "https://example.com/video.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view.bounds
view.layer.addSublayer(playerLayer)
player.play()
let progressSlider = UISlider()
progressSlider.minimumValue = 0
progressSlider.maximumValue = Float(CMTimeGetSeconds(player.currentItem?.duration ?? CMTime()))
progressSlider.value = 0
player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: 1), queue: .main) { [weak self] time in
let currentTime = CMTimeGetSeconds(time)
self?.progressSlider.value = Float(currentTime)
}
let seekTime = CMTime(seconds: Double(progressSlider.value), preferredTimescale: 1)
player.seek(to: seekTime)
let volumeSlider = UISlider()
volumeSlider.minimumValue = 0
volumeSlider.maximumValue = 1
volumeSlider.value = player.volume
player.volume = volumeSlider.value
以上是使用AVPlayer進行在線播放和控制播放進度條和音量條的基本步驟,可以根據自己的需求進行定制和擴展。