SvenTiigi / YouTubePlayerKit

A Swift Package to easily play YouTube videos 📺
https://sventiigi.github.io/YouTubePlayerKit/
MIT License
723 stars 64 forks source link

Skip to timestamp using Youtube URL #82

Closed rollbahn closed 9 months ago

rollbahn commented 9 months ago

What happened?

I am trying to get the YT videos in my app to skip forward to the section that relates to each place page on a travel app I am building. I have the YT video ID's with the timestamps saved in a JSON file as "Zvb3ciV4qCs?t=150" and have also tried "Zvb3ciV4qCs&t=150" but both just load the video and not start at the 150 (2min 30 sec) mark. If I use them directly on Youtube they both work without an issue.

Am I doing something wrong or is this not supported?

What are the steps to reproduce?

Load the view with the videos and play the videos

What is the expected behavior?

The Youtube video skips automatically forward to the designate timestamp.

SvenTiigi commented 9 months ago

Hi @rollbahn,

Could you please provide the full qualified URL which you are using including your initialization code of the YouTubePlayer instance?

On my side passing in the following URL starts the video at the specified time location:

let youtTubePlayer = YouTubePlayer(source: .url("https://www.youtube.com/watch?v=Zvb3ciV4qCs&t=150"))
rollbahn commented 9 months ago

Thanks @SvenTiigi, My current code is below and I am storing the VideoID's in a JSON file (in an array) and pulling them from there so I'm not aware of what the full url is - again probably my lack of knowledge. Everything works beautifully until I add a parameter to the VideoID.

            if let youtubeVideoIDs = place.youtubeVideoIDs, !youtubeVideoIDs.isEmpty {
                VStack(alignment: .leading) {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 10) {
                        ForEach(youtubeVideoIDs, id: \.self) { videoID in
                            YouTubePlayerView(YouTubePlayer(source: .video(id: videoID)))
                                .frame(width: 300, height: 168)
                                .cornerRadius(8)
                        }
                    }
                }
            }
                .padding()
        }
SvenTiigi commented 9 months ago

If you are using the .video case of the YouTubePlayer.Source you need to specify the start seconds via the startSeconds parameter:

let youTubePlayer = YouTubePlayer(source: .video(id: "Zvb3ciV4qCs", startSeconds: 150))
rollbahn commented 9 months ago

Thanks @SvenTiigi - much appreciated and thank you for the code overall.