I have a simple app in SwiftUI, and I want to add video player in app like that, everything works, but when I change the bottom tabbar, video still playing, how we can stop the video?
Home:
struct Home : View {
@State var index = 0
@State var top = 0
@State var data = [
Video(id: 0, player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video4", ofType: "mp4")!)), replay: false),
Video(id: 1, player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video5", ofType: "mp4")!)), replay: false),
]
var body: some View{
ZStack{
PlayerScrollView(data: self.$data)
VStack{
HStack(spacing: 15){
Button(action: {
self.top = 0
}) {
Text("Following")
.foregroundColor(self.top == 0 ? .white : Color.white.opacity(0.45))
.fontWeight(self.top == 0 ? .bold : .none)
.padding(.vertical)
}
Button(action: {
self.top = 1
}) {
Text("For You")
.foregroundColor(self.top == 1 ? .white : Color.white.opacity(0.45))
.fontWeight(self.top == 1 ? .bold : .none)
.padding(.vertical)
} }
HStack{
Spacer()
VStack(spacing: 35){
Button(action: {
}) {
Image("pic")
.renderingMode(.original)
.resizable()
.frame(width: 55, height: 55)
.clipShape(Circle())
}
Button(action: {
}) {
VStack(spacing: 8){
Image(systemName: "suit.heart.fill")
.font(.title)
.foregroundColor(.white)
Text("22K")
.foregroundColor(.white)
}}
Button(action: {
}) {
VStack(spacing: 8){
Image(systemName: "message.fill")
.font(.title)
.foregroundColor(.white)
Text("1021")
.foregroundColor(.white)
} }
Button(action: {
}) {
VStack(spacing: 8){
Image(systemName: "arrowshape.turn.up.right.fill")
.font(.title)
.foregroundColor(.white)
Text("Share")
.foregroundColor(.white)
} } }
.padding(.bottom, 55)
.padding(.trailing)
}
HStack(spacing: 0){
Button(action: {
self.index = 0
}) {
Image("home")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(self.index == 0 ? .white : Color.white.opacity(0.35))
.padding(.horizontal) }
Spacer(minLength: 0)
Button(action: {
self.index = 1
}) {
Image("search")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(self.index == 1 ? .white : Color.white.opacity(0.35))
.padding(.horizontal)
}
Spacer(minLength: 0)
Button(action: {
}) {
Image("upload")
.renderingMode(.original)
.resizable()
.frame(width: 50, height: 35)
.padding(.horizontal)
}
Spacer(minLength: 0)
Button(action: {
self.index = 2
}) {
Image("comment")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(self.index == 2 ? .white : Color.white.opacity(0.35))
.padding(.horizontal)
}
Spacer(minLength: 0)
Button(action: {
self.index = 3
}) {
Image("profile")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(self.index == 3 ? .white : Color.white.opacity(0.35))
.padding(.horizontal)
}}
.padding(.horizontal) }
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
.padding(.bottom, (UIApplication.shared.windows.first?.safeAreaInsets.bottom)! + 5)}
.background(Color.black.edgesIgnoringSafeArea(.all))
.edgesIgnoringSafeArea(.all)
}}
PlayerView:
struct PlayerView : View {
@Binding var data : [Video]
var body: some View{
VStack(spacing: 0){
ForEach(0..<self.data.count){i in
ZStack{
Player(player: self.data[i].player)
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.offset(y: -5)
if self.data[i].replay{
Button(action: {
self.data[i].replay = false
self.data[i].player.seek(to: .zero)
self.data[i].player.play()
}) {
Image(systemName: "goforward")
.resizable()
.frame(width: 55, height: 60)
.foregroundColor(.white) }
}}
} }
.onAppear {
self.data[0].player.play()
self.data[0].player.actionAtItemEnd = .none
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.data[0].player.currentItem, queue: .main) { (_) in
self.data[0].replay = true
} }
}}
Player:
struct Player : UIViewControllerRepresentable {
var player : AVPlayer
func makeUIViewController(context: Context) -> AVPlayerViewController{
let view = AVPlayerViewController()
view.player = player
view.showsPlaybackControls = false
view.videoGravity = .resizeAspectFill
return view }
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
}}
class Host : UIHostingController<ContentView>{
override var preferredStatusBarStyle: UIStatusBarStyle{
return .lightContent
}}
Video:
struct Video : Identifiable {
var id : Int
var player : AVPlayer
var replay : Bool
}