ฝังวิดีโอ YouTube ในแอปพลิเคชัน iOS ด้วยไลบรารีผู้ช่วยของ YouTube
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
youtube-ios-player-helper
เป็นไลบรารีแบบโอเพนซอร์สซึ่งช่วยให้คุณฝังโปรแกรมเล่น iframe ของ YouTube ลงในแอปพลิเคชัน iOS ได้ ไลบรารีจะสร้าง WebView
และบริดจ์ระหว่างโค้ด Objective-C ของแอปพลิเคชันกับโค้ด JavaScript ในโปรแกรมเล่นของ YouTube ซึ่งช่วยให้แอปพลิเคชัน iOS ควบคุมโปรแกรมเล่น YouTube ได้ บทความนี้อธิบายถึงขั้นตอนการติดตั้งไลบรารีและเริ่มต้นใช้งานจากแอปพลิเคชัน iOS
การติดตั้ง
บทความนี้จะถือว่าคุณได้สร้างโปรเจ็กต์ iOS แอปพลิเคชันแบบมุมมองเดียวที่กําหนดเป้าหมายเป็น iOS เวอร์ชันล่าสุด และคุณได้เพิ่มไฟล์ต่อไปนี้เมื่อสร้างโปรเจ็กต์
Main.storyboard
ViewController.h
ViewController.m
คุณติดตั้งไลบรารีผ่าน CocoaPods หรือคัดลอกไฟล์ไลบรารีและไฟล์แหล่งที่มาจากหน้า GitHub ของโปรเจ็กต์ได้
- ไลบรารีสามารถติดตั้งผ่าน CocoaPods ได้ อีกทางเลือกหนึ่งคือไลบรารีไฟล์และไฟล์ต้นทางจะพร้อมให้บริการผ่านหน้า GitHub ของโปรเจ็กต์และคัดลอกลงในโปรเจ็กต์ที่มีอยู่ได้
ติดตั้งไลบรารีผ่าน CocoaPods
หากโปรเจ็กต์ใช้ CocoaPods ให้เพิ่มบรรทัดด้านล่างใน Podfile เพื่อติดตั้งไลบรารี
ในบรรทัดดังกล่าว ให้แทนที่ x.y.z
ด้วยเวอร์ชันล่าสุดของพ็อด ซึ่งจะระบุไว้ในหน้า GitHub ของโปรเจ็กต์
pod "youtube-ios-player-helper", "~> x.y.z"
เมื่อมีข้อความแจ้งบรรทัดคําสั่ง ให้พิมพ์ pod install
เพื่ออัปเดตพื้นที่ทํางานด้วยทรัพยากร Dependency
เคล็ดลับ: โปรดทราบว่าเมื่อใช้ CocoaPods คุณต้องเปิดไฟล์ .xcworkspace
ใน Xcode ไม่ใช่ไฟล์ .xcodeproj
ดูข้อมูลเพิ่มเติมได้ที่บทแนะนําของ CocoaPods
ติดตั้งไลบรารีด้วยตนเอง
หากต้องการติดตั้งไลบรารีผู้ช่วยด้วยตนเอง ให้ดาวน์โหลดแหล่งข้อมูลผ่านลิงก์ดาวน์โหลดของ GitHub หรือโคลนที่เก็บ เมื่อคุณมีสําเนาของโค้ดในเครื่องแล้ว ให้ทําตามขั้นตอนต่อไปนี้
เปิดโปรเจ็กต์ตัวอย่างใน Xcode หรือ Finder
เลือก YTPlayerView.h
, YTPlayerView.m
และโฟลเดอร์ชิ้นงาน หากคุณเปิดพื้นที่ทํางานใน Xcode ฟังก์ชันเหล่านี้จะอยู่ในพ็อด -> พ็อดการพัฒนา -> YouTube-Player-iOS-Helper และพ็อด -> พ็อดการพัฒนา -> YouTube-Player-iOS-Helper -> ทรัพยากร ใน Finder รายการเหล่านี้จะมีอยู่ในไดเรกทอรีรากของโปรเจ็กต์ในไดเรกทอรีชั้นเรียนและเนื้อหา
ลากไฟล์และโฟลเดอร์เหล่านี้ลงในโครงการ ตรวจสอบว่าได้เลือกตัวเลือกคัดลอกรายการไปยังโฟลเดอร์ของกลุ่มปลายทางแล้ว เมื่อลากโฟลเดอร์ชิ้นงาน โปรดตรวจสอบว่าได้เลือกตัวเลือกสร้างการอ้างอิงโฟลเดอร์สําหรับโฟลเดอร์ที่เพิ่มแล้ว
ตอนนี้ควรติดตั้งไลบรารีแล้ว
เพิ่ม YTPlayerView
ผ่านเครื่องมือสร้างอินเทอร์เฟซหรือสตอรีบอร์ด
วิธีเพิ่ม YTPlayerView
ผ่านเครื่องมือสร้างอินเทอร์เฟซหรือสตอรีบอร์ด
-
ลากอินสแตนซ์ UIView
ไปไว้ในมุมมอง
-
เลือกเครื่องมือตรวจสอบข้อมูลประจําตัวและเปลี่ยนคลาสของมุมมองเป็น YTPlayerView
-
เปิด ViewController.h
และเพิ่มส่วนหัวต่อไปนี้
#import “YTPlayerView.h”
และเพิ่มพร็อพเพอร์ตี้ต่อไปนี้ด้วย
@property(nonatomic, strong) IBOutlet YTPlayerView *playerView;
-
ในเครื่องมือสร้างอินเทอร์เฟซ ให้สร้างการเชื่อมต่อจากองค์ประกอบข้อมูลพร็อพเพอร์ตี้ที่คุณกําหนดในขั้นตอนก่อนหน้าไปยังพร็อพเพอร์ตี้ playerView
ของตัวควบคุมการดู
-
ตอนนี้ให้เปิด ViewController.m
แล้วเพิ่มโค้ดต่อไปนี้ที่ส่วนท้ายของเมธอด viewDidLoad
[self.playerView loadWithVideoId:@"M7lc1UVf-VE"];
สร้างและเรียกใช้แอปพลิเคชัน เมื่อภาพปกวิดีโอโหลดขึ้นมา ให้แตะที่ภาพปกวิดีโอเพื่อเปิดโปรแกรมเล่นวิดีโอแบบเต็มหน้าจอ
ควบคุมการเล่นวิดีโอ
เมธอด ViewController::loadWithVideoId:
มีตัวแปร loadWithVideoId:playerVars:
ที่ช่วยให้นักพัฒนาซอฟต์แวร์ส่งตัวแปรโปรแกรมเล่นเพิ่มเติมไปยังข้อมูลพร็อพเพอร์ตี้ได้ ตัวแปรโปรแกรมเล่นเหล่านี้สอดคล้องกับพารามิเตอร์โปรแกรมเล่นใน IFrame Player API พารามิเตอร์ playsinline
ช่วยให้วิดีโอเล่นในมุมมองได้โดยตรงแทนการเล่นแบบเต็มหน้าจอ เมื่อวิดีโอกําลังเล่นในบรรทัด แอปพลิเคชันใน iOS จะควบคุมการเล่นแบบเป็นโปรแกรมได้
แทนที่การเรียกใช้ loadWithVideoId:
ด้วยรหัสนี้
NSDictionary *playerVars = @{
@"playsinline" : @1,
};
[self.playerView loadWithVideoId:@"M7lc1UVf-VE" playerVars:playerVars];
เปิดสตอรีบอร์ดหรือเครื่องมือสร้างอินเทอร์เฟซ ลากปุ่ม 2 ปุ่มมายังมุมมอง โดยติดป้ายกํากับว่าเล่นและหยุด เปิด ViewController.h
และเพิ่มวิธีการเหล่านี้ซึ่งจะแมปกับปุ่ม
- (IBAction)playVideo:(id)sender;
- (IBAction)stopVideo:(id)sender;
ตอนนี้ ให้เปิด ViewController.m
และกําหนด 2 ฟังก์ชันต่อไปนี้
- (IBAction)playVideo:(id)sender {
[self.playerView playVideo];
}
- (IBAction)stopVideo:(id)sender {
[self.playerView stopVideo];
}
ฟังก์ชัน IFrame Player API ส่วนใหญ่มี Objective-C ที่เทียบเท่า แม้ว่าชื่อบางชื่ออาจแตกต่างกันเล็กน้อยเพื่อให้สอดคล้องกับหลักเกณฑ์การเขียนโค้ด Objective-C มากขึ้น ข้อยกเว้นที่สําคัญคือวิธีการควบคุมระดับเสียงของวิดีโอ เนื่องจากฮาร์ดแวร์เหล่านี้ควบคุมโดยฮาร์ดแวร์โทรศัพท์หรืออินสแตนซ์ UIView
ในตัวที่ออกแบบมาเพื่อวัตถุประสงค์นี้ เช่น MPVolumeView
เปิดสตอรีบอร์ดหรือเครื่องมือสร้างอินเทอร์เฟซแล้วลากตัวควบคุมเพื่อเชื่อมต่อปุ่มเล่นและหยุดกับเมธอด playVideo:
และ stopVideo:
สร้างและเรียกใช้แอปพลิเคชันเลย เมื่อภาพขนาดย่อของวิดีโอโหลดแล้ว คุณควรจะสามารถเล่นและหยุดวิดีโอโดยใช้ตัวควบคุมโฆษณาเนทีฟได้นอกเหนือจากตัวควบคุมโปรแกรมเล่น
จัดการโค้ดเรียกกลับของผู้เล่น
ซึ่งมีประโยชน์ในการจัดการเหตุการณ์การเล่นแบบเป็นโปรแกรม เช่น การเปลี่ยนแปลงสถานะการเล่นและข้อผิดพลาดในการเล่น ใน JavaScript API คุณจะดําเนินการได้ด้วยตัวฟังเหตุการณ์
ใน Objective-C ขั้นตอนนี้ทําด้วยการมอบสิทธิ์
โค้ดต่อไปนี้แสดงวิธีอัปเดตการประกาศอินเทอร์เฟซใน ViewController.h
เพื่อให้ชั้นเรียนสอดคล้องกับโปรโตคอลที่ได้รับมอบสิทธิ์ เปลี่ยนการประกาศอินเทอร์เฟซของ ViewController.h
ดังนี้
@interface ViewController : UIViewController<YTPlayerViewDelegate>
YTPlayerViewDelegate
เป็นโปรโตคอลสําหรับจัดการเหตุการณ์การเล่นในโปรแกรมเล่น
หากต้องการอัปเดต ViewController.m
เพื่อจัดการเหตุการณ์บางรายการ คุณต้องตั้งค่าอินสแตนซ์ ViewController
เป็นผู้รับมอบสิทธิ์ของอินสแตนซ์ YTPlayerView
ก่อน หากต้องการเปลี่ยนแปลง ให้เพิ่มบรรทัดต่อไปนี้ลงในเมธอด viewDidLoad
ใน ViewController.h
self.playerView.delegate = self;
เพิ่มเมธอดต่อไปนี้ลงใน ViewController.m
แล้ว
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state {
switch (state) {
case kYTPlayerStatePlaying:
NSLog(@"Started playback");
break;
case kYTPlayerStatePaused:
NSLog(@"Paused playback");
break;
default:
break;
}
}
สร้างและเรียกใช้แอปพลิเคชัน ดูเอาต์พุตบันทึกใน Xcode เมื่อสถานะโปรแกรมเล่นเปลี่ยนแปลง
คุณควรเห็นการอัปเดตเมื่อวิดีโอเล่นหรือหยุดเล่น
ไลบรารีจะมีค่าคงที่ที่ขึ้นต้นด้วยคํานําหน้า kYT*
เพื่อความสะดวกและความอ่านได้ง่าย ดูรายการค่าคงที่ทั้งหมดได้ที่ YTPlayerView.m
แนวทางปฏิบัติแนะนําและข้อจํากัด
ไลบรารีสร้างขึ้นจาก IFrame Player API ด้วยการสร้าง WebView
และแสดงผล HTML และ JavaScript ที่จําเป็นสําหรับโปรแกรมเล่นพื้นฐาน เป้าหมายของไลบรารีก็คือ
การใช้วิธีการรวมให้มากที่สุดที่นักพัฒนาซอฟต์แวร์ต้องเขียนลงในแพ็กเกจบ่อยครั้ง มีข้อจํากัดบางประการที่ควรทราบ
- ไลบรารีไม่รองรับการเล่นวิดีโอพร้อมกันใน
YTPlayerView
อินสแตนซ์หลายรายการ หากแอปพลิเคชันของคุณมีอินสแตนซ์ YTPlayerView
หลายรายการ แนวทางปฏิบัติแนะนําคือหยุดชั่วคราวหรือหยุดการเล่นในอินสแตนซ์ที่มีอยู่ก่อนเริ่มเล่นในอินสแตนซ์อื่น ในแอปพลิเคชันตัวอย่างที่จัดส่งด้วยโปรเจ็กต์ ViewController จะใช้ NSNotificationCenter
เพื่อส่งการแจ้งเตือนว่าการเล่นใกล้จะเริ่มขึ้น ตัวควบคุม View อื่นๆ จะได้รับการแจ้งเตือนและจะหยุดเล่นชั่วคราวในอินสแตนซ์ YTPlayerView
ของตน
- นําอินสแตนซ์
YTPlayerView
รายการที่มีอยู่มาใช้ซ้ํา หากเป็นไปได้ เมื่อจําเป็นต้องเปลี่ยนวิดีโอในข้อมูลพร็อพเพอร์ตี้ อย่าสร้างอินสแตนซ์ UIView
ใหม่หรืออินสแตนซ์ YTPlayerView
ใหม่ และอย่าเรียก loadVideoId:
หรือ loadPlaylistId:
แต่ให้ใช้กลุ่มฟังก์ชัน cueVideoById:startSeconds:
แทน ซึ่งจะไม่โหลด WebView
ซ้ํา เกิดความล่าช้าอย่างมากเมื่อโหลดโปรแกรมเล่น iframe ทั้งหมด
- โปรแกรมเล่นนี้ไม่สามารถเล่นวิดีโอส่วนตัว แต่จะเล่นวิดีโอที่ไม่เป็นสาธารณะได้ เนื่องจากไลบรารีนี้รวมโปรแกรมเล่น iframe ที่มีอยู่ ลักษณะการทํางานของโปรแกรมเล่นจึงน่าจะเกือบเหมือนกับของโปรแกรมเล่นที่ฝังอยู่ในหน้าเว็บในเบราว์เซอร์บนอุปกรณ์เคลื่อนที่
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2023-02-22 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2023-02-22 UTC"],[[["\u003cp\u003eThis library, \u003ccode\u003eyoutube-ios-player-helper\u003c/code\u003e, facilitates embedding a YouTube iframe player within an iOS application by creating a \u003ccode\u003eWebView\u003c/code\u003e and bridging Objective-C code with the player's JavaScript.\u003c/p\u003e\n"],["\u003cp\u003eInstallation is possible via CocoaPods by adding a specific line to the Podfile, or manually by downloading the source code from the project's GitHub page and dragging specific files into your iOS project.\u003c/p\u003e\n"],["\u003cp\u003eA \u003ccode\u003eYTPlayerView\u003c/code\u003e can be added through Interface Builder or Storyboard by dragging a \u003ccode\u003eUIView\u003c/code\u003e, changing its class to \u003ccode\u003eYTPlayerView\u003c/code\u003e, and connecting it to a \u003ccode\u003eplayerView\u003c/code\u003e property in the \u003ccode\u003eViewController.h\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe library allows for controlling video playback, enabling inline play and manipulation using the IFrame Player API through Objective-C equivalent methods.\u003c/p\u003e\n"],["\u003cp\u003eThe library allows the handling of playback events, such as start and stop, through a delegate protocol, \u003ccode\u003eYTPlayerViewDelegate\u003c/code\u003e, where the \u003ccode\u003eViewController\u003c/code\u003e conforms to this protocol to capture those events.\u003c/p\u003e\n"]]],["The `youtube-ios-player-helper` library enables embedding and controlling a YouTube iframe player within an iOS app. Installation is via CocoaPods or manual file copying from GitHub. To use, a `YTPlayerView` is added to the storyboard, connected to a `ViewController`, and initialized with a video ID. Playback is controlled through Objective-C methods (e.g., `playVideo`, `stopVideo`) linked to UI elements. Developers can pass player variables, and the library supports delegate-based event handling for playback states. The library is based on the iFrame Player API and includes a few limitations.\n"],null,["# Embed YouTube Videos in iOS Applications with the YouTube Helper Library\n\nThe `youtube-ios-player-helper` is an open source library that helps you embed a\nYouTube iframe player into an iOS application. The library creates a\n`WebView` and a bridge between your application's Objective-C code and the\nYouTube player's JavaScript code, thereby allowing the iOS application to control the\nYouTube player. This article describes the steps to install the library and get started\nusing it from your iOS application.\n\nInstallation\n------------\n\nThis article assumes you have created a new Single View Application iOS project targeting\nthe latest version of iOS, and that you add the following files when creating the\nproject:\n\n- `Main.storyboard`\n- `ViewController.h`\n- `ViewController.m`\n\nYou can install the library via\n[CocoaPods](https://cocoapods.org/) or by copying the library\nand source files from the\n[project's GitHub page](https://github.com/youtube/youtube-ios-player-helper).\n\n- The library is available to install via CocoaPods. Alternatively, the library and source files are available via the project's GitHub page and can be copied into an existing project.\n\n### Install the library via CocoaPods\n\nIf your project uses CocoaPods, add the line below to your Podfile to install the library.\nIn that line, replace `x.y.z` with the latest pod version, which will be\nidentified on the project's GitHub page. \n\n```text\npod \"youtube-ios-player-helper\", \"~\u003e x.y.z\"\n```\n\nAt the command line prompt, type `pod install` to update your workspace with the\ndependencies.\n\nTip: Remember that when using CocoaPods, you must open the `.xcworkspace` file\nin Xcode, not the `.xcodeproj` file.\n\nCheck out the [CocoaPods\ntutorial](https://guides.cocoapods.org/using/getting-started.html) to learn more.\n\n### Manually install the library\n\nTo install the helper library manually, either download the source via\n[GitHub's download link](https://github.com/youtube/youtube-ios-player-helper) or\nclone the repository. Once you have a local copy of the code, follow these steps:\n\n1. Open the sample project in Xcode or Finder.\n\n2. Select `YTPlayerView.h`, `YTPlayerView.m`, and the\n **Assets** folder. If you open the workspace in Xcode, these are available\n under **Pods -\\\u003e Development Pods -\\\u003e YouTube-Player-iOS-Helper** and\n **Pods -\\\u003e Development Pods -\\\u003e YouTube-Player-iOS-Helper -\\\u003e Resources** . In the Finder,\n these are available in the project's root directory in the **Classes** and\n **Assets** directories.\n\n3. Drag these files and folders into your project. Make sure the **Copy items into\n destination group's folder** option is checked. When dragging the Assets folder, make\n sure that the **Create Folder References for any added folders** option is\n checked.\n\nThe library should now be installed.\n\nAdd a `YTPlayerView` via Interface Builder or the Storyboard\n------------------------------------------------------------\n\nTo add a `YTPlayerView` via Interface Builder or the Storyboard:\n\n1. Drag a `UIView` instance onto your View.\n\n2. Select the Identity Inspector and change the class of the view to\n `YTPlayerView`.\n\n3. Open `ViewController.h` and add the following header:\n\n ```objective-c\n #import \"YTPlayerView.h\"\n ```\n\n Also add the following property: \n\n ```objective-c\n @property(nonatomic, strong) IBOutlet YTPlayerView *playerView;\n ```\n4. In Interface Builder, create a connection from the View element that you defined in the\n previous step to your View Controller's `playerView` property.\n\n5. Now open `ViewController.m` and add the following code to the end of your\n `viewDidLoad` method:\n\n ```objective-c\n [self.playerView loadWithVideoId:@\"M7lc1UVf-VE\"];\n ```\n\nBuild and run your application. When the video thumbnail loads, tap the video thumbnail to\nlaunch the fullscreen video player.\n\nControl video playback\n----------------------\n\nThe `ViewController::loadWithVideoId:` method has a variant,\n`loadWithVideoId:playerVars:`, that allows developers to pass additional player\nvariables to the view. These player variables correspond to the\n[player parameters in the\nIFrame Player API](https://developers.google.com/youtube/player_parameters). The `playsinline` parameter enables the video to play\ndirectly in the view rather than playing fullscreen. When a video is playing inline, the\ncontaining iOS application can programmatically control playback.\n\nReplace the `loadWithVideoId:` call with this code: \n\n```objective-c\nNSDictionary *playerVars = @{\n @\"playsinline\" : @1,\n};\n[self.playerView loadWithVideoId:@\"M7lc1UVf-VE\" playerVars:playerVars];\n```\n\nOpen up the storyboard or Interface Builder. Drag two buttons onto your View, labeling them\n**Play** and **Stop** . Open `ViewController.h` and add these methods, which\nwill be mapped to the buttons: \n\n```objective-c\n- (IBAction)playVideo:(id)sender;\n- (IBAction)stopVideo:(id)sender;\n```\n\nNow open `ViewController.m` and define these two functions: \n\n```objective-c\n- (IBAction)playVideo:(id)sender {\n [self.playerView playVideo];\n}\n\n- (IBAction)stopVideo:(id)sender {\n [self.playerView stopVideo];\n}\n```\n\nMost of the IFrame Player API functions have Objective-C equivalents, though some of the\nnaming may differ slightly to more closely match Objective-C coding guidelines. Notable\nexceptions are methods controlling the volume of the video, since these are controlled by\nthe phone hardware or with built in `UIView` instances designed for this purpose,\nsuch as [`MPVolumeView`](https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPVolumeView_Class/Reference/Reference.html).\n\nOpen your storyboard or Interface Builder and control-drag to connect the **Play** and\n**Stop** buttons to the `playVideo:` and `stopVideo:` methods.\n\nNow build and run the application. Once the video thumbnail loads, you should be able to\nplay and stop the video using native controls in addition to the player controls.\n\nHandle player callbacks\n-----------------------\n\nIt can be useful to programmatically handle playback events, such as playback state changes\nand playback errors. In the JavaScript API, this is done with\n[event listeners](https://developers.google.com/youtube/iframe_api_reference#Adding_event_listener).\nIn Objective-C,this is done with a\n[delegate](https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html).\n\n\nThe following code shows how to update the interface declaration in\n`ViewController.h` so the class conforms to the delegate protocol. Change\n`ViewController.h`'s interface declaration as follows: \n\n```objective-c\n@interface ViewController : UIViewController\u003cYTPlayerViewDelegate\u003e\n```\n\n`YTPlayerViewDelegate` is a protocol for handling playback events in the player.\nTo update `ViewController.m` to handle some of the events, you first need to set\nthe `ViewController` instance as the delegate of the `YTPlayerView`\ninstance. To make this change, add the following line to the `viewDidLoad` method\nin `ViewController.h`. \n\n```objective-c\nself.playerView.delegate = self;\n```\n\nNow add the following method to `ViewController.m`: \n\n```objective-c\n- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state {\n switch (state) {\n case kYTPlayerStatePlaying:\n NSLog(@\"Started playback\");\n break;\n case kYTPlayerStatePaused:\n NSLog(@\"Paused playback\");\n break;\n default:\n break;\n }\n}\n```\n\nBuild and run the application. Watch the log output in Xcode as the player state changes.\nYou should see updates when the video is played or stopped.\n\nThe library provides the constants that begin with the `kYT*` prefix for\nconvenience and readability. For a full list of these constants, look at\n`YTPlayerView.m`.\n\nBest practices and limitations\n------------------------------\n\nThe library builds on top of the IFrame Player API by creating a `WebView` and\nrendering the HTML and JavaScript required for a basic player. The library's goal is to be\nas easy-to-use as possible, bundling methods that developers frequently have to write into a\npackage. There are a few limitations that should be noted:\n\n- The library does not support concurrent video playback in multiple `YTPlayerView` instances. If your application has multiple `YTPlayerView` instances, a recommended best practice is to pause or stop playback in any existing instances before starting playback in a different instance. In the example application that ships with the project, the ViewControllers make use of `NSNotificationCenter` to dispatch notifications that playback is about to begin. Other ViewControllers are notified and will pause playback in their `YTPlayerView` instances.\n- Reuse your existing, loaded `YTPlayerView` instances when possible. When a video needs to be changed in a View, don't create a new `UIView` instance or a new `YTPlayerView` instance, and don't call either `loadVideoId:` or `loadPlaylistId:`. Instead, use the `cueVideoById:startSeconds:` family of functions, which do not reload the `WebView`. There is a noticeable delay when loading the entire IFrame player.\n- This player cannot play private videos, but it can play [unlisted videos](https://support.google.com/youtube/answer/157177). Since this library wraps the existing iframe player, the player's behavior should be nearly identical to that of a player embedded on a webpage in a mobile browser.\n\nContributions\n-------------\n\nSince this is an open-source project, please submit fixes and improvements to the\n[master branch of the GitHub\nproject](https://github.com/youtube/youtube-ios-player-helper)."]]