با کتابخانه راهنمای YouTube، ویدیوهای YouTube را در برنامه های iOS جاسازی کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
youtube-ios-player-helper
یک کتابخانه منبع باز است که به شما کمک می کند یک پخش کننده iframe YouTube را در یک برنامه iOS جاسازی کنید. این کتابخانه یک WebView
و پلی بین کد Objective-C برنامه شما و کد جاوا اسکریپت پخش کننده YouTube ایجاد می کند، بنابراین به برنامه iOS اجازه می دهد پخش کننده YouTube را کنترل کند. این مقاله مراحل نصب کتابخانه و شروع استفاده از آن را از برنامه iOS شما شرح می دهد.
نصب و راه اندازی
این مقاله فرض میکند که شما یک پروژه جدید iOS Application Single View ایجاد کردهاید که آخرین نسخه iOS را هدف قرار میدهد، و هنگام ایجاد پروژه، فایلهای زیر را اضافه میکنید:
-
Main.storyboard
-
ViewController.h
-
ViewController.m
میتوانید کتابخانه را از طریق CocoaPods یا با کپی کردن کتابخانه و فایلهای منبع از صفحه GitHub پروژه نصب کنید.
- این کتابخانه برای نصب از طریق CocoaPods در دسترس است. متناوبا، کتابخانه و فایلهای منبع از طریق صفحه GitHub پروژه در دسترس هستند و میتوانند در یک پروژه موجود کپی شوند.
کتابخانه را از طریق CocoaPods نصب کنید
اگر پروژه شما از CocoaPods استفاده می کند، خط زیر را به Podfile خود اضافه کنید تا کتابخانه را نصب کنید. در آن خط، xyz
با آخرین نسخه پاد جایگزین کنید، که در صفحه GitHub پروژه مشخص می شود.
pod "youtube-ios-player-helper", "~> x.y.z"
در خط فرمان، pod install
تایپ کنید تا فضای کاری شما با وابستگی ها به روز شود.
نکته: به یاد داشته باشید که هنگام استفاده از CocoaPods، باید فایل .xcworkspace
را در Xcode باز کنید، نه فایل .xcodeproj
.
برای کسب اطلاعات بیشتر ، آموزش CocoaPods را بررسی کنید.
کتابخانه را به صورت دستی نصب کنید
برای نصب دستی کتابخانه کمکی، منبع را از طریق لینک دانلود GitHub دانلود کنید یا مخزن را شبیه سازی کنید. هنگامی که یک کپی محلی از کد دریافت کردید، این مراحل را دنبال کنید:
پروژه نمونه را در Xcode یا Finder باز کنید.
YTPlayerView.h
، YTPlayerView.m
و پوشه Assets را انتخاب کنید. اگر فضای کاری را در Xcode باز کنید، اینها در زیر Pods -> Development Pods -> YouTube-Player-iOS-Helper and Pods -> Development Pods -> YouTube-Player-iOS-Helper -> Resources در دسترس هستند. در Finder، اینها در دایرکتوری ریشه پروژه در فهرستهای کلاسها و داراییها در دسترس هستند.
این فایل ها و پوشه ها را به داخل پروژه خود بکشید. مطمئن شوید که گزینه Copy items into destination's folder's تیک زده شده است. هنگام کشیدن پوشه Assets، مطمئن شوید که گزینه Create Folder References for any added folders تیک خورده باشد.
اکنون باید کتابخانه نصب شود.
یک YTPlayerView
از طریق Interface Builder یا Storyboard اضافه کنید
برای افزودن YTPlayerView
از طریق Interface Builder یا Storyboard:
یک نمونه UIView
را روی نمای خود بکشید.
Identity Inspector را انتخاب کنید و کلاس view را به YTPlayerView
تغییر دهید.
ViewController.h
باز کنید و هدر زیر را اضافه کنید:
#import “YTPlayerView.h”
همچنین ویژگی زیر را اضافه کنید:
@property(nonatomic, strong) IBOutlet YTPlayerView *playerView;
در Interface Builder، یک اتصال از عنصر View که در مرحله قبل تعریف کردید به ویژگی playerView
در View Controller خود ایجاد کنید.
حالا ViewController.m
باز کنید و کد زیر را به انتهای متد viewDidLoad
خود اضافه کنید:
[self.playerView loadWithVideoId:@"M7lc1UVf-VE"];
اپلیکیشن خود را بسازید و اجرا کنید. وقتی تصویر کوچک ویدیو بارگیری شد، روی تصویر کوچک ویدیو ضربه بزنید تا پخش کننده ویدیوی تمام صفحه راه اندازی شود.
کنترل پخش ویدیو
متد ViewController::loadWithVideoId:
یک نوع دارد، loadWithVideoId:playerVars:
که به توسعه دهندگان اجازه می دهد تا متغیرهای پخش کننده اضافی را به view ارسال کنند. این متغیرهای پخش کننده با پارامترهای پخش کننده در API IFrame Player مطابقت دارند. پارامتر playsinline
ویدیو را قادر میسازد تا بهجای پخش تمام صفحه، مستقیماً در نما پخش شود. هنگامی که یک ویدیو به صورت درون خطی در حال پخش است، برنامه حاوی iOS می تواند پخش را به صورت برنامه ای کنترل کند.
loadWithVideoId:
call را با این کد جایگزین کنید:
NSDictionary *playerVars = @{
@"playsinline" : @1,
};
[self.playerView loadWithVideoId:@"M7lc1UVf-VE" playerVars:playerVars];
استوری بورد یا Interface Builder را باز کنید. دو دکمه را روی View خود بکشید و روی آنها برچسب Play و Stop بزنید. ViewController.h
باز کنید و این روش ها را اضافه کنید که به دکمه ها نگاشت می شوند:
- (IBAction)playVideo:(id)sender;
- (IBAction)stopVideo:(id)sender;
حالا ViewController.m
باز کنید و این دو تابع را تعریف کنید:
- (IBAction)playVideo:(id)sender {
[self.playerView playVideo];
}
- (IBAction)stopVideo:(id)sender {
[self.playerView stopVideo];
}
بیشتر توابع IFrame Player API معادل Objective-C دارند، اگرچه برخی از نامگذاریها ممکن است کمی متفاوت باشد تا با دستورالعملهای کدنویسی Objective-C مطابقت داشته باشد. استثناهای قابل توجه روشهایی هستند که حجم ویدیو را کنترل میکنند، زیرا این موارد توسط سختافزار تلفن یا با نمونههای داخلی UIView
که برای این منظور طراحی شدهاند کنترل میشوند، مانند MPVolumeView
.
استوریبورد یا Interface Builder خود را باز کنید و برای اتصال دکمههای Play و Stop به متدهای playVideo:
و stopVideo:
کنترل کنید.
حالا اپلیکیشن را بسازید و اجرا کنید. پس از بارگیری تصویر کوچک ویدیو، باید بتوانید با استفاده از کنترل های بومی علاوه بر کنترل های پخش کننده، ویدیو را پخش و متوقف کنید.
رسیدگی به تماس های پخش کننده
مدیریت برنامهریزی رویدادهای پخش، مانند تغییرات وضعیت پخش و خطاهای پخش، میتواند مفید باشد. در JavaScript API، این کار با شنوندگان رویداد انجام می شود. در Objective-C، این کار با یک delegate انجام می شود.
کد زیر نحوه به روز رسانی اعلان رابط در ViewController.h
را نشان می دهد تا کلاس با پروتکل delegate مطابقت داشته باشد. اعلان رابط 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
نگاه کنید.
بهترین شیوه ها و محدودیت ها
این کتابخانه با ایجاد یک WebView
و رندر کردن HTML و جاوا اسکریپت مورد نیاز برای یک پخش کننده اصلی بر روی API IFrame Player ساخته می شود. هدف این کتابخانه این است که تا حد امکان استفاده آسان باشد و روشهایی را که توسعهدهندگان اغلب باید در یک بسته بنویسند، جمعبندی کند. چند محدودیت وجود دارد که باید به آنها توجه کرد:
- این کتابخانه از پخش همزمان ویدیو در چندین نمونه
YTPlayerView
پشتیبانی نمی کند. اگر برنامه شما چندین نمونه YTPlayerView
دارد، بهترین روش توصیه شده توقف یا توقف پخش در هر نمونه موجود قبل از شروع پخش در یک نمونه دیگر است. در برنامه مثالی که با پروژه ارسال میشود، ViewControllers از NSNotificationCenter
برای ارسال اعلانهایی استفاده میکند که پخش در شرف شروع است. سایر ViewController ها مطلع می شوند و پخش را در نمونه های YTPlayerView
خود متوقف می کنند. - در صورت امکان از نمونه های
YTPlayerView
بارگذاری شده موجود خود دوباره استفاده کنید. هنگامی که یک ویدیو باید در یک View تغییر کند، یک نمونه UIView
جدید یا یک نمونه جدید YTPlayerView
ایجاد نکنید، و نه loadVideoId:
و نه loadPlaylistId:
صدا نکنید. در عوض، از خانواده توابع cueVideoById:startSeconds:
استفاده کنید، که WebView
را مجدداً بارگیری نمی کند. هنگام بارگیری کل پخش کننده IFrame تاخیر قابل توجهی وجود دارد. - این پخش کننده نمی تواند ویدیوهای خصوصی را پخش کند، اما می تواند ویدیوهای فهرست نشده را پخش کند. از آنجایی که این کتابخانه پخش کننده iframe موجود را می پوشاند، رفتار پخش کننده باید تقریباً مشابه رفتار پخش کننده ای باشد که در یک صفحه وب در مرورگر تلفن همراه تعبیه شده است.
مشارکت ها
از آنجایی که این یک پروژه منبع باز است، لطفاً اصلاحات و بهبودها را به شاخه اصلی پروژه GitHub ارسال کنید.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2023-02-22 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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 بهوقت ساعت هماهنگ جهانی."],[[["\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)."]]