The Swift YouTube Player is a simple Swift wrapper around the YouTube IFrame Player API, which allows you to embed YouTube videos in your iOS apps. It provides a convenient way to play YouTube videos with customizable player controls.
Key Features
- Embed YouTube videos: Easily integrate YouTube videos in your iOS apps.
- Customizable player controls: Tailor the player controls to match your app’s design.
- Control playback options: Play, pause, stop, seek, and adjust volume.
- Listen to playback events: Receive notifications when the video starts, pauses, finishes, etc.
- React to user interaction: Detect when the user interacts with the player, such as tapping play or pause.
Installation
- Open your project in Xcode.
- Go to File -> Swift Packages -> Add Package Dependency.
- Enter the package URL:
https://github.com/gilesvangruisen/Swift-YouTube-Player.git
- Click Next and choose the desired version rule.
- Add the package to your required targets and click Finish.
Usage
Follow the steps below to embed and control YouTube videos in your app:
Step 1: Import the Library
// Import the YouTubePlayer module
import YouTubePlayer
Step 2: Create a YouTubePlayerView
// Create a YouTubePlayerView instance
let playerView = YouTubePlayerView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
Step 3: Set the API Key (Optional)
// Set your YouTube Data API key (optional)
playerView.apiKey = "YOUR_API_KEY"
Step 4: Load a Video
// Load a video by providing the YouTube video ID
playerView.loadVideoID("YOUR_VIDEO_ID")
Step 5: Control Playback
// Control video playback
playerView.play() // Play the video
playerView.pause() // Pause the video
playerView.stop() // Stop the video
playerView.seekTo(30, seekAhead: true) // Seek to a specific time (in seconds)
// Adjust the volume (0.0 - 1.0)
playerView.setVolume(0.5)
Step 6: Listen to Playback Events
// Add a playback event listener
playerView.delegate = self
// Implement the YouTubePlayerDelegate protocol
func playerStateChanged(_ videoPlayer: YouTubePlayerView, playerState: YouTubePlayerState) {
// Handle playback state change events
switch playerState {
case .Playing:
print("Video is playing")
case .Paused:
print("Video is paused")
case .Ended:
print("Video playback ended")
case .Buffering:
print("Video is buffering")
case .Queued:
print("Video is queued")
case .Unknown:
print("Unknown playback state")
}
}
func playerReady(_ videoPlayer: YouTubePlayerView) {
// Player is ready to receive commands
}
Step 7: React to User Interaction
// Handle user interaction events
playerView.on(.play) { [weak self] _ in
print("User tapped play button")
// Perform necessary actions
}
playerView.on(.pause) { [weak self] _ in
print("User tapped pause button")
// Perform necessary actions
}
Wrap Up
With the Swift YouTube Player, you can easily add YouTube videos to your iOS apps and have full control over the playback. Its simple API and customizable player controls make it a versatile tool for integrating YouTube content into your applications.