The YouTubePlayer is the central object for interacting with the YouTube iFrame API. You can initialize it with a simple URL string or with full control using Source, Parameters, and Configuration.
- Source: Defines what to play (video, playlist, channel, etc.).
- Parameters: Controls the behavior and style of the YouTube player (e.g.,
autoPlay, loopEnabled, showControls). Note: Updating parameters at runtime causes the player to reload. - Configuration: Linked to the underlying web view (e.g.,
fullscreenMode, allowsInlineMediaPlayback). Configuration cannot be modified after instantiation.
let youTubePlayer = YouTubePlayer(
// Possible values: .video, .videos, .playlist, .channel
source: .video(id: "psL_5RIBqnY"),
// The parameters of the player
parameters: .init(
autoPlay: true,
loopEnabled: true,
startTime: .init(value: 5, unit: .minutes),
showControls: true
),
// The configuration of the underlying web view
configuration: .init(
fullscreenMode: .system,
allowsInlineMediaPlayback: true,
customUserAgent: "MyCustomUserAgent"
)
)