先前提到可以用Web View來播放Youtube影片,現在我們可以更輕易地播放Youtube影片囉~只要使用第三方套件XCDYouTubeKit,就可以透過內建的MPMoviePlayerViewController來播放影片!
來用今年員工旅遊在韓國首爾空中滑翔到南怡島的影片做測試吧~
此第三方套件的特色是,可以全螢幕播放,也可在某個view中播放,也就是能夠不用全螢幕播放!
使用方式非常簡單,只要傳入Youtube影片的ID即可!
如:
Youtube網址:https://www.youtube.com/watch?v=uLwSGJgUgTQ
YoutubeID:uLwSGJgUgTQ
#import <XCDYouTubeKit/XCDYouTubeKit.h> NSString *videoIdentifier = @"uLwSGJgUgTQ"; // 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 } }]; // Present the video in full-screen XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"uLwSGJgUgTQ"]; [self presentMoviePlayerViewControllerAnimated:videoPlayerViewController]; // Present the video in a non full-screen view XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"uLwSGJgUgTQ"]; [videoPlayerViewController presentInView:self.videoContainerView]; [videoPlayerViewController.moviePlayer play];
同樣也可能遇到想要橫向和直向都能播放,先前也提到在WebView播放Youtube影片旋轉,我們也可註冊觀察來做到。
/** Theme: Play Youtube Video IDE: Xcode 6 Language: Objective C Date: 103/11/02 Author: HappyMan Blog: https://cg2010studio.wordpress.com/ */ // 註冊觀察Youtube播放影片 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
不一樣的是觀察的對象,先前用Web View是UIMoviePlayer,現在用MPMoviePlayerViewController是MPMoviePlayer,就這樣。
2015/11/02更新:
有網友覺得第四點很難找到該檔案,在此我重新嘗試了一下,並截圖下來。首先從GitHub下載作者的Demo專案,編譯運行若順利,在左方檔案結構那兒尋找,按右鍵Show In Finder就能取得目標檔案喔!
Comments on: "[iOS] 播放Youtube影片2" (7)
對了 我沒有使用cocoaPods…..
感謝
讚讚
不好意思 請問..
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.
github上的說明第四步 我找不到libXCDYouTubeKit.a
雖然有在library 裡面找到 但 import 到我的專案裡面會有問題
可以請教一下 版主使用的順序嗎??
感謝
讚讚
我重新嘗試了一下,下載作者的Demo專案後,編譯執行若順利,則會在左方資料檔案結構中,發現它所說的第四點:
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.
你把該檔案也就是Products下的libXCDYouTubeKit.a抓下來並引用到你的專案,即可順利在你專案中使用該套件。
希望上述所說你能找到。
讚讚
我有更新文章,你文章最下方可看到更新,能夠知道該檔案的位置喔~ 😉
讚讚
你好,我在#import 的部分就卡了,
我已經把XCDYouTubeKit匯入我的專案裡頭,
在import的時候卻找不到XCDYoutubeKit,
可以請版主幫幫忙嗎?
讚讚
問題解決了XD
讚Liked by 1 person
呵呵~恭喜你!
想說怎麼可能會不進去XDDD
讚讚