• Stars
    star
    2,901
  • Rank 15,044 (Top 0.4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

YouTube video player for iOS, tvOS and macOS

About

Build Status Coverage Status Platform Pod Version Carthage Compatibility Swift Package Manager Compatibility License

XCDYouTubeKit is a YouTube video player for iOS, tvOS and macOS.

Are you enjoying XCDYouTubeKit? You can say thank you with a tweet. I am also accepting donations. ;-)

Donate button

Requirements

  • Runs on iOS 8.0 and later
  • Runs on macOS 10.9 and later
  • Runs on tvOS 9.0 and later

Warning

XCDYouTubeKit is against the YouTube Terms of Service. The only official way of playing a YouTube video inside an app is with a web view and the iframe player API. Unfortunately, this is very slow and quite ugly, so I wrote this player to give users a better viewing experience.

Installation

XCDYouTubeKit is available through CocoaPods, Carthage and Swift Package Manager.

CocoaPods:

pod "XCDYouTubeKit", "~> 2.15"

Carthage:

github "0xced/XCDYouTubeKit" ~> 2.15

Swift Package Manager:

Add XCDYouTubeKit to the dependencies value of your Package.swift

dependencies: [
	.package(url: "https://github.com/0xced/XCDYouTubeKit.git", from: "2.15.0")
]

Alternatively, you can manually use the provided static library or dynamic framework. In order to use the static library, you must:

  1. Create a workspace (File → New → Workspace…)
  2. Add your project to the workspace
  3. Add the XCDYouTubeKit project to the workspace
  4. Drag and drop the libXCDYouTubeKit.a file referenced from XCDYouTubeKit → Products → libXCDYouTubeKit.a into the Link Binary With Libraries build phase of your app’s target.

These steps will ensure that #import <XCDYouTubeKit/XCDYouTubeKit.h> will work properly in your project.

Usage

XCDYouTubeKit is fully documented.

iOS 8.0+ & tvOS (AVPlayerViewController)

AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[self presentViewController:playerViewController animated:YES completion:nil];

__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoIdentifier completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
    if (video)
    {
        NSDictionary *streamURLs = video.streamURLs;
        NSURL *streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[@(XCDYouTubeVideoQualityHD720)] ?: streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[@(XCDYouTubeVideoQualitySmall240)];
        weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
        [weakPlayerViewController.player play];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}];

iOS, tvOS and macOS

NSString *videoIdentifier = @"9bZkp7q19f0"; // A 11 characters YouTube video identifier
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoIdentifier completionHandler:^(XCDYouTubeVideo *video, NSError *error) {
	if (video)
	{
		// Do something with the `video` object
	}
	else
	{
		// Handle error
	}
}];

iOS 8.0

On iOS, you can use the class XCDYouTubeVideoPlayerViewController the same way you use a MPMoviePlayerViewController, except you initialize it with a YouTube video identifier instead of a content URL.

Present the video in full-screen

- (void) playVideo
{
	XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
	[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
}

- (void) moviePlayerPlaybackDidFinish:(NSNotification *)notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:notification.object];
	MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
	if (finishReason == MPMovieFinishReasonPlaybackError)
	{
		NSError *error = notification.userInfo[XCDMoviePlayerPlaybackDidFinishErrorUserInfoKey];
		// Handle error
	}
}

Present the video in a non full-screen view

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[videoPlayerViewController presentInView:self.videoContainerView];
[videoPlayerViewController.moviePlayer play];

See the demo project for more sample code.

Logging

Since version 2.2.0, XCDYouTubeKit produces logs. XCDYouTubeKit supports CocoaLumberjack but does not require it.

See the XCDYouTubeLogger class documentation for more information.

Credits

The URL extraction algorithms in XCDYouTubeKit are inspired by the YouTube extractor module of the youtube-dl project.

Contact

Cédric Luthi

License

XCDYouTubeKit is available under the MIT license. See the LICENSE file for more information.

More Repositories

1

iOS-Artwork-Extractor

Extract iOS artwork and emoji symbols into png files, generate glossy buttons png files
Objective-C
2,615
star
2

xcproj

Command line tool for manipulating Xcode project files
Objective-C
317
star
3

XCDFormInputAccessoryView

Input accessory view with previous, next and done buttons
Objective-C
238
star
4

FontReplacer

Easily create nibs with custom fonts
Objective-C
229
star
5

NSUUID

NSUUID implementation for iOS < 6.0 and OS X < 10.8
Objective-C
157
star
6

ABGetMe

ABGetMe implementation for iOS using undocumented APIs (safely)
Objective-C
145
star
7

XCDLumberjackNSLogger

CocoaLumberjack logger which sends logs to NSLogger
Objective-C
96
star
8

MPMoviePlayerController-XCDOverlayView

Overlay view synchronized with playback controls for MPMoviePlayerController
Objective-C
93
star
9

CLITool-InfoPlist

Xcode plugin to process Info.plist file for CLI Tool targets
Objective-C
53
star
10

Stealth-Messenger

Send email, SMS or tweet without any user interaction on iOS
Objective-C
45
star
11

otx

Mach-O disassembler
Objective-C
34
star
12

CLURLConnection

NSURLConnection done right
Objective-C
34
star
13

fulldescription

Recursively print instance variables of an Objective-C object.
Objective-C
30
star
14

SpotColor

Spot Color is a wrapper around the built-in Mac OS X color picker so you can use it as a stand alone app
Objective-C
27
star
15

quietxcode

Silences the "malloc: free_garbage: garbage ptr ..." Xcode warnings
Objective-C
15
star
16

pdfrasterize

Command line tool for converting PDF documents to various bitmap formats
Objective-C
13
star
17

NoLastUpgradeCheck

Xcode 4 plugin that prevents LastUpgradeCheck attribute alteration
Objective-C
13
star
18

otx-bblm

BBEdit/TextWrangler language module for otx disassemblies
C
12
star
19

crippng

QuickLook plugin to read crippled iPhone png files
C
12
star
20

Twexpand

SIMBL plugin for displaying full URLs in your favorite Twitter client
Objective-C
11
star
21

APELite-arm

Implementation of the APE Lite API for iPhone OS (ARM) using MobileSubstrate
C++
9
star
22

DocZoom

Usable Xcode documentation Pinch-to-Zoom
Objective-C
4
star
23

SingleFileAppDependencyContext

Experiments with single-file applications and Microsoft.Extensions.DependencyModel
C#
2
star
24

DbContextValidation

DbContext validation against an actual database
C#
2
star
25

iCalColors

SIMBL Plugin that allows you to customize today and selected day colors
Objective-C
1
star
26

RidGraph

Generate visualizable .NET runtime identifier graphs
C#
1
star
27

KeychainCredentials

Implementation of the System.Net.ICredentials interface using the macOS Keychain for .NET
C#
1
star
28

radars

Sample code for bugs submitted to bugreport.apple.com
Objective-C
1
star