Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

swift - iOS SwiftUI - Reset VideoPlayer to beginning of video

I can't seem to stop video, only pause, but that leads to the video starting where it was paused, instead of starting over again as I'd like it to on the next time a user plays the video.

VStack {
        VideoPlayer(player: player)
            .edgesIgnoringSafeArea(.all)
            .onAppear {
                player.play()
            }
            .onDisappear {
                player.pause()
            }
    }

How am I able to stop the video on disappear, so that it resets each time it plays?

question from:https://stackoverflow.com/questions/65909240/ios-swiftui-reset-videoplayer-to-beginning-of-video

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
.onDisappear {                        
  player.pause()
  player.seek(to: .zero)
}

Source: https://developer.apple.com/documentation/avfoundation/avplayer/1385953-seek


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...