**SwiftUI**
«`swift
import SwiftUI
import AVFoundation
struct VideoView: View {
@State private var isPlaying = false
let player: AVPlayer
var body: some View {
VStack {
VideoPlayer(player: player)
.frame(width: 300, height: 150)
HStack {
Button(action: { isPlaying.toggle() }) {
Image(systemName: isPlaying ? «pause.fill» : «play.fill»)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime)) { _ in
player.seek(to: .zero)
isPlaying = false
}
}
}
«`
**UIKit**
«`swift
import UIKit
import AVFoundation
class VideoViewController: UIViewController {
var player: AVPlayer!
var playerLayer: AVPlayerLayer!
override func viewDidLoad() {
super.viewDidLoad()
let playerItem = AVPlayerItem(url: URL(string: «path/to/video.mp4»)!)
player = AVPlayer(playerItem: playerItem)
playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x: 0, y: 0, width: 300, height: 150)
view.layer.addSublayer(playerLayer)
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: playerItem, queue: nil) { _ in
player.seek(to: .zero)
}
player.play()
}
@objc func togglePlayPause() {
if player.isPlaying {
player.pause()
} else {
player.play()
}
}
}
«`