總覽
本文說明如何在應用程式中嵌入 YouTube 播放器,並定義 YouTube 內嵌播放器中具備的參數。
只要為 IFrame 網址附加參數,即可在應用程式中自訂播放體驗。舉例來說,您可以使用 autoplay
參數來自動播放影片,或是使用 loop
參數讓影片重複播放。您也可以使用 enablejsapi
參數,透過 IFrame Player API 控製播放器。
本頁目前定義的是任何 YouTube 內嵌播放器支援的所有參數。每個參數定義都能識別支援相應參數的玩家。
注意:內嵌播放器的檢視區必須至少為 200 x 200 像素。如果播放器顯示控制項,其大小必須足以完整顯示控制項,且不會縮小到最小尺寸以下的畫面。我們建議 16:9 播放器的寬度至少 480 像素,高度至少 270 像素。
嵌入 YouTube 播放器
您可以使用下列方法,將 YouTube 播放器嵌入您的應用程式並指定播放器參數。請注意,以下指示示範如何嵌入可載入單一影片的播放器。以下章節將說明如何設定播放器載入播放清單或使用者上傳的影片。
使用 <iframe>
標記嵌入播放器
在應用程式中定義 <iframe>
標記,其中 src
網址會指定播放器載入的內容,以及您要設定的任何其他播放器參數。<iframe>
標記的 height
和 width
參數會指定播放器的尺寸。
如果您自行建立 <iframe>
元素 (而不是使用 IFrame Player API 建立元素),可以直接在網址結尾附加播放器參數。網址格式如下:
https://www.youtube.com/embed/VIDEO_ID
下方的 <iframe>
標記會載入 640x360 像素的播放器,用於播放 YouTube 影片 M7lc1UVf-VE
。由於網址會將 autoplay
參數設為 1
,因此影片會在播放器載入後自動播放。
<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com" frameborder="0"></iframe>
使用 IFrame Player API 嵌入播放器
按照 IFrame Player API 的指示,在 Player API 的 JavaScript 程式碼載入後,在您的網頁或應用程式中插入影片播放器。影片播放器建構函式中的第二個參數,是指定播放器選項的物件。在這個物件中,playerVars
屬性可識別播放器參數。
下方的 HTML 和 JavaScript 程式碼顯示簡單範例,用於將 YouTube 播放器插入 id
值為 ytplayer
的網頁元素中。載入 IFrame Player API 程式碼時,系統會自動呼叫這裡指定的 onYouTubePlayerAPIReady()
函式。這個程式碼不會定義任何播放器參數,也不會定義其他事件處理常式。
<div id="ytplayer"></div> <script> // Load the IFrame Player API code asynchronously. var tag = document.createElement('script'); tag.src = "https://www.youtube.com/player_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // Replace the 'ytplayer' element with an <iframe> and // YouTube player after the API code downloads. var player; function onYouTubePlayerAPIReady() { player = new YT.Player('ytplayer', { height: '360', width: '640', videoId: 'M7lc1UVf-VE' }); } </script>
選取要播放的內容
您可以將嵌入式播放器設為載入影片、播放清單或使用者上傳的影片。
以下清單說明這些選項:
-
載入影片
針對 IFrame 內嵌,請在 IFrame 的
src
網址中指定所要載入影片的 YouTube 影片 ID。https://www.youtube.com/embed/VIDEO_ID
如果您使用的是 YouTube Data API (v3),可以透過從搜尋結果、播放清單項目資源、影片資源或其他資源中擷取影片 ID,透過程式輔助方式建立這些網址。取得影片 ID 後,請將上方網址中的
VIDEO_ID
文字改成該值,以建立播放器網址。 -
載入播放清單
將
listType
播放器參數設為playlist
。另外,請將list
播放器參數設為想載入的 YouTube 播放清單 ID。https://www.youtube.com/embed?listType=playlist&list=PLAYLIST_ID
請注意,你必須在播放清單 ID 前面加上字母
PL
,如以下範例所示:https://www.youtube.com/embed?listType=playlist&list=PLC77007E23FF423C6
如果您使用的是 YouTube Data API (v3),可以透過程式輔助的方式擷取搜尋結果、頻道資源或活動資源中的播放清單 ID,透過程式輔助方式建立這些網址。取得播放清單 ID 後,請將上方網址中的
PLAYLIST_ID
文字改成該值。 -
載入使用者上傳的影片
將
listType
播放器參數設為user_uploads
。此外,請將list
播放器參數設為要載入上傳影片的 YouTube 使用者名稱。https://www.youtube.com/embed?listType=user_uploads&list=USERNAME
支援的參數
以下所有參數都是選用項目。
參數 | |
---|---|
|
此參數指定是否要在播放器載入時,開始播放初始影片。支援的值為 0 或 1 。預設值為 0 。如果您啟用了自動播放功能,那麼系統會在使用者未與播放器互動的情況下播放影片,因此網頁載入後就會收集 資料的收集和分享行為。 |
|
這個參數會指定播放器用來顯示字幕的預設語言。將參數值設為 ISO 639-1 雙字母語言代碼。 如果使用這個參數,並將 cc_load_policy 參數設為 1 ,播放器就會在播放器載入時以指定語言顯示字幕。如果您並未設定 cc_load_policy 參數,則根據預設,系統不會顯示字幕,但如果使用者選擇開啟字幕,系統會以該語言顯示字幕。
|
|
根據預設,將參數值設為 1 時,即使使用者關閉字幕,仍會顯示隱藏式輔助字幕,預設行為則是根據使用者偏好而定。
|
|
此參數指定要在播放器的影片進度列中使用的顏色,藉此突顯觀眾已看過的影片量。有效參數值為 red 和 white ,且根據預設,播放器會在影片進度列中以紅色顯示。如要進一步瞭解色彩選項,請參閱 YouTube API 網誌。注意:將 color 參數設為 white 會停用 modestbranding 選項。
|
|
此參數會指出是否要顯示影片播放器控制項:
|
|
如果將參數值設為 1 ,播放器將無法回應鍵盤控制項。預設值為 0 ,表示啟用鍵盤控制項。目前支援的鍵盤控制項如下:
|
|
將參數值設為 1 可讓玩家透過 IFrame Player API 呼叫來控制。預設值為 0 ,表示無法透過該 API 控製播放器。如要進一步瞭解 IFrame API 和使用方式,請參閱 IFrame API 說明文件。 |
|
此參數指定從影片開始播放時起算的秒數,表示播放器應停止播放影片的時間。參數值為正整數。 請注意,時間是從影片開頭算起,而非從 start 播放器參數的值或 startSeconds 參數 (用於在 YouTube Player API 函式中載入或排入影片)。 |
|
將這個參數設為 0 可避免播放器的全螢幕按鈕顯示。預設值為 1 ,因此會顯示全螢幕按鈕。
|
|
設定播放器的介面語言。參數值為 ISO 639-1 雙字母語言代碼或完整指定的語言代碼。舉例來說,fr 和 fr-ca 都是有效值。系統可能也會正確處理其他語言輸入碼,例如 IETF 語言標記 (BCP 47)。播放器的工具提示語言也會用來影響預設字幕軌。請注意,YouTube 可能會根據使用者本身的語言偏好設定和字幕軌的供應情形,為特定使用者選取不同的字幕軌語言。 |
|
根據預設,如果將參數值設為 1 ,影片註解就會顯示,而設為 3 則會預設顯示影片註解。預設值為 1 。 |
|
list 參數搭配 listType 參數可識別會在播放器中載入的內容。
list 和 listType 參數指定了值,IFrame 嵌入網址就不需要指定影片 ID。 |
|
listType 參數搭配 list 參數可識別會在播放器中載入的內容。有效參數值為 playlist 和 user_uploads 。如果為 list 和 listType 參數值指定值,IFrame 嵌入網址就不需要指定影片 ID。
注意:第三個參數值 search 已淘汰,並自 |
|
如果是單一影片播放器,透過 1 設定會讓播放器連續播放初始影片。如果是播放清單播放器 (或自訂播放器),播放器會播放整個播放清單,然後在第一部影片開始播放。支援的值為 0 和 1 ,預設值為 0 。注意:在 IFrame 嵌入中,這個參數僅支援部分資源。如要循環播放單一影片,請將 loop 參數值設為 1 ,並將 playlist 參數值設為已在 Player API 網址中指定的相同影片 ID:https://www.youtube.com/embed/VIDEO_ID?playlist=VIDEO_ID&loop=1 |
|
此參數可用來避免未顯示 YouTube 標誌的 YouTube 播放器。將參數值設為 1 可避免在控制列中顯示 YouTube 標誌。請注意,當使用者的滑鼠遊標懸停在播放器上時,暫停顯示的右上角仍會顯示小型的 YouTube 文字標籤。
|
|
此參數為 IFrame API 提供額外的安全性措施,且僅適用於 IFrame 嵌入。如果您使用 IFrame API (也就是將 enablejsapi 參數值設為 1 ),請一律將您的網域指定為 origin 參數值。 |
|
此參數會指定要播放的影片 ID 逗號分隔清單。如果您指定了值,播放的第一個影片會是網址路徑中指定的 VIDEO_ID ,而 playlist 參數中指定的影片將會播放。 |
|
這個參數可控管是否要在 iOS 裝置上以內嵌或全螢幕播放影片。有效值如下:
|
|
注意:這個參數會在 2018 年 9 月 25 日當天或之後變更。 這項異動生效前,這個參數會指出播放器在影片播放完畢後,是否應顯示相關影片。
rel 參數設為 0 ,相關影片則會從播放過的影片來自同一個頻道。
|
|
這個參數會使播放器從影片開頭算起的秒數開始開始播放。參數值為正整數。請注意,與 seekTo 函式類似,玩家會尋找與您指定的時間最接近的主要畫面格。這表示播放頭有時可能會超過要求時間,通常不超過兩秒。
|
|
此參數可識別內嵌播放器的網址。YouTube 播放器會在小工具中嵌入 YouTube 播放器,並將小工具嵌入網頁或應用程式中,就可以使用這個值。在這種情況下,origin 參數可用來辨識小工具供應商的網域,但 YouTube 數據分析不應將小工具供應商視為實際的流量來源。反之,YouTube 數據分析會使用 widget_referrer 參數值來識別與流量來源相關聯的網域。
|
修訂版本記錄
April 27, 2021
The playsinline
parameter definition has been updated
slightly to explain that the parameter pertains to both mobile browsers on iOS and iOS
WebViews.
October 15, 2020
The autoplay
parameter definition has been updated to
explain that if you enable Autoplay, playback will occur without any user interaction with the
player; playback data collection and sharing will therefore occur upon page load.
October 13, 2020
Note: This is a deprecation announcement for the embedded player
functionality that lets you configure the player to load search results. This announcement affects
the player's list
and
listType
parameters.
This change will become effective on or after listType
parameter value to search
will generate a
4xx
response code, such as 404
(Not Found
) or
410
(Gone
).
As an alternative, you can use the YouTube Data API's
search.list
method to retrieve search
results and then load selected videos in the player.
In addition to the change described above, the showinfo
parameter, which was
deprecated in 2018, has been removed from this document.
August 23, 2018
Note: This is a deprecation announcement for the showinfo
parameter. In addition, the behavior for the rel
parameter is changing. Titles, channel information, and related videos are an important part of YouTube’s core user experience, and these changes help to make the YouTube viewing experience consistent across different platforms.
- The behavior for the
rel
parameter is changing on or after September 25, 2018. The effect of the change is that you will not be able to disable related videos. However, you will have the option of specifying that the related videos shown in the player should be from the same channel as the video that was just played.
To be more specific:- Prior to the change, if the parameter's value is set to
0
, then the player does not show related videos. - After the change, if the
rel
parameter is set to0
, the player will show related videos that are from the same channel as the video that was just played.
- Prior to the change, if the parameter's value is set to
- The
showinfo
parameter, which indicates whether the player should display information like the video title and uploader before the video starts playing, is also being deprecated. Following the change, the channel avatar and video title will always display before playback begins, when playback is paused, and when playback ends. The avatar being displayed is new behavior that will be consistent across all embedded players.
These changes will become effective on or after September 25, 2018. After that time, the showinfo
parameter will be ignored. The behavior following the changes is consistent with the current default behavior for embedded players with the exception of the channel avatar changes mentioned above.
August 16, 2018
The cc_lang_pref
parameter can be used to specify the default language that the player will use to display captions. This parameter can be used in conjunction with the cc_load_policy
parameter to automatically show captions in the specified language during playback.
September 15, 2017
The controls
parameter's definition has been updated to remove references to the deprecated Flash (AS3) player. The value 2
has been deprecated as it was originally designed to provide a performance improvement for embeds that loaded a Flash player.
June 12, 2017
The new widget_referrer
parameter helps to enable more accurate YouTube Analytics reporting when the YouTube player is embedded in a widget, and that widget is then embedded in a web page or application.
November 1, 2016
This document has been updated to remove references to the deprecated Flash (AS3) player as well as to parameters only supported by that player. The YouTube Flash player was deprecated in January 2015.
October 20, 2016
This update contains the following changes:
-
The
disablekb
parameter definition has been corrected to note that the default value is0
, which means that keyboard controls are enabled. A value of1
indicates that keyboard controls should be disabled.The list of keyboard controls that the player supports has also been updated to include the following:
- [f]: Toggle full-screen display
- [j]: Jump back 10 seconds in the current video
- [k]: Play / Pause
- [l]: Jump ahead 10 seconds in the current video
- [m]: Mute or unmute the video
- [0-9]: Jump to a point in the video.
0
jumps to the beginning of the video,1
jumps to the time 10% into the video,2
jumps to the point 20% into the video, and so forth.
In addition, the effect of pressing the [arrow left] or [arrow right] keys has changed. These keys now jump 5 seconds back (arrow left) or ahead (arrow right) in the current video.
August 11, 2016
This update contains the following changes:
-
The newly published YouTube API Services Terms of Service ("the Updated Terms"), discussed in detail on the YouTube Engineering and Developers Blog, provides a rich set of updates to the current Terms of Service. In addition to the Updated Terms, which will go into effect as of February 10, 2017, this update includes several supporting documents to help explain the policies that developers must follow.
The full set of new documents is described in the revision history for the Updated Terms. In addition, future changes to the Updated Terms or to those supporting documents will also be explained in that revision history. You can subscribe to an RSS feed listing changes in that revision history from a link in that document.
December 18, 2015
European Union (EU) laws require that certain disclosures must be given to and consents obtained from end users in the EU. Therefore, for end users in the European Union, you must comply with the EU User Consent Policy. We have added a notice of this requirement in our YouTube API Terms of Service.
August 19, 2015
-
The
autohide
parameter has been deprecated for the HTML5 player. In HTML5 players, the video progress bar and player controls display or hide automatically. That behavior corresponds to anautohide
setting of1
. -
The
theme
parameter has been deprecated for the HTML5 player. HTML5 players now always use the dark theme.
March 9, 2015
-
The document has been updated to reflect the fact that YouTube
<object>
embeds, the YouTube Flash Player API and the YouTube JavaScript Player API have all been deprecated as of January 27, 2015. A deprecation warning appears in several sections of this document to help point readers to the IFrame Player API as an alternative. -
The definition of the
autohide
parameter has been updated to clarify the meaning of the parameter's values. The default behavior (autohide=2
) is that if the player has a 16:9 or 4:3 aspect ratio, the player's video progress bar and player controls display or hide automatically. Otherwise, those controls are visible throughout the video. -
The definition of the
hl
parameter has been updated to note that the parameter value could be an ISO 639-1 two-letter language code or a fully specified locale. For example,fr
andfr-ca
are both valid parameter values. -
The definition of the
enablejsapi
parameter has been reworded to clarify that the parameter enables a player to be controlled via API calls. The API could be either the IFrame Player API or the JavaScript Player API.
October 14, 2014
-
The HTML5 player now supports the
cc_load_policy
,disablekb
,end
,fs
,list
, andlistType
parameters. The parameter descriptions have been updated accordingly.
July 18, 2014
-
The new
hl
parameter can be used to set the player's interface language. The interface language is used for tooltips in the player and also affects the default caption track. The selected caption track may also depend on the availability of caption tracks and user's individual language preferences.The parameter's value is an ISO 639-1 two-letter language code, though other language input codes, such as IETF language tags (BCP 47) may also be handled properly.
-
The definition of the
playsinline
parameter, which only affects HTML5 players on iOS, has been modified slightly. The definition now notes that setting the parameter value to1
causes inline playback only forUIWebViews
created with theallowsInlineMediaPlayback
property set toTRUE
.
January 28, 2014
-
The
playsinline
parameter controls whether videos play inline or fullscreen in an HTML5 player on iOS. Setting the value to1
causes inline playback. -
The Selecting content to play section has been updated to explain how to find YouTube video IDs and playlist IDs using the YouTube Data API (v3) rather than the older API version.
-
The
controls
parameter's definition has been updated to reflect the fact that the parameter value only affects the time that the Flash player actually loads in IFrame embeds. In addition, for IFrame embeds, the parameter value also determines when the controls display in the player. If you set the parameter's value to2
, then the controls display and the Flash player loads after the user initiates the video playback.
May 10, 2013
This update contains the following changes:
-
YouTube no longer identifies experimental API features and services. Instead, we now provide a list of YouTube APIs that are subject to the deprecation policy.
July 20, 2012
This update contains the following changes:
-
The definition of the
controls
parameter has been updated to reflect support for a parameter value of2
and to explain that, for AS3 players, the parameter value determines the time when the Flash player actually loads. If thecontrols
parameter is set to0
or1
, the Flash player loads immediately. If the parameter value is2
, the Flash player does not load until the video playback is initiated.
June 5, 2012
This update contains the following changes:
-
The HTML5 player now supports the
color
,modestbranding
, andrel
parameters, and the definitions for these parameters have been updated accordingly. -
The definition of the
showinfo
parameter has been updated to explain how that if the player is loading a playlist, and you explicitly set the parameter value to1
, then, upon loading, the player will also display thumbnail images for the videos in the playlist. Note that this functionality is only supported for the AS3 player since that is the only player that can load a playlist.
May 4, 2012
This update contains the following changes:
-
The
showinfo
parameter's definition has been updated to reflect the fact that the HTML5 player supports this parameter.
May 3, 2012
This update contains the following changes:
-
The new
end
parameter specifies the time, measured in seconds from the start of the video, when the player should stop playing a video. Note that the time when playback is stopped is measured from the beginning of the video and not from the value of either thestart
player parameter or thestartSeconds
parameter, which is used in YouTube Player API functions for loading or queueing a video.
March 29, 2012
This update contains the following changes:
-
The new Embedding a YouTube player section explains different ways to embed a YouTube player in your application. This section covers manual IFrame embeds, IFrame embeds that use the IFrame Player API, and AS3 and AS2 object embeds. This section incorporates information from the old Example usage section, which has been removed.
-
The new Selecting content to play section explains how to configure the player to load a video, a playlist, search results for a specified query, or uploaded videos for a specified user.
-
The new
list
andlistType
parameters let you specify the content that the player should load. You can specify a playlist, a search query, or a particular user's uploaded videos. -
The definitions of the
fs
andrel
parameters have been updated to more clearly explain the default parameter values for the AS3 player. -
The
border
,color1
,egm
,hd
, andshowsearch
parameters, which are all only supported for the deprecated AS2 Player API, have been moved to a new section named deprecated parameters only used in the AS2 Player API. -
The document no longer provides a way to filter the parameter list to only display parameters supported in either the AS3, AS2, or HTML5 player. Instead, each parameter definition has been updated to identify the players that support that parameter.
August 11, 2011
This update contains the following changes:
-
The new
theme
andcolor
parameters let you customize the appearance of the embedded player's player controls. See the YouTube API blog for more information.
June 8, 2011
This update contains the following changes:
-
The new
modestbranding
parameter lets you use a YouTube player that does not show a YouTube logo. As of this release, the parameter was only supported for the AS3 embedded player and for IFrame embeds that loaded the AS3 player. As of June 5, 2012, the HTML5 player also supported this parameter.
February 14, 2011
This update contains the following changes:
-
The documentation has been updated to note that the AS2 Player API has been deprecated. The deprecation of the AS2 Player API was actually announced on October 14, 2009.
February 3, 2011
This update contains the following changes:
-
The documentation has been updated to identify parameters supported in the HTML5 (IFrame) embedded player.
-
The document now displays all of the parameters supported in any of YouTube's embedded players (HTML5, AS3, AS2).
-
The following parameters are supported in the AS2 player but have been deprecated for the newer AS3 and HTML5 players:
border
,color1
,color2
,egm
,hd
, andshowsearch
.In addition, the
loop
parameter has limited support in the AS3 player and in IFrame embeds, which could load either an AS3 or HTML player. Currently, theloop
parameter only works in the AS3 player when used in conjunction with theplaylist
parameter. To loop a single video, set theloop
parameter to1
and set theplaylist
parameter value to the same video ID already specified in the Player API URL:https://www.youtube.com/v/VIDEO_ID?version=3&loop=1&playlist=VIDEO_ID
Similarly, the
controls
andplaylist
parameters are supported in the AS3 and HTML5 players but are not and will not be supported in the AS2 player.As noted above, IFrame embeds can load either an AS3 or HTML5 player. Though some parameters are not supported in both players, an IFrame embed that loads the AS3 player will support all parameters that work with that player and ignore all other parameters. Similarly, an IFrame embed that loads the HTML5 player will support all parameters that work with that player while ignoring all other parameters.
-
The parameter list has been updated to include the
autohide
parameter, which indicates whether the player's video controls will automatically hide after a video begins playing. -
The parameter list has been updated to include the
controls
parameter, which indicates whether the player's video controls will display at all. (Player controls do display by default.) -
The parameter list has been updated to include the
playlist
parameter, which specifies a comma-separated list of video IDs to play. -
The definition of the
fs
has been updated to note that the fullscreen option will not work if you load the YouTube player into another SWF. -
The example at the end of the document has been updated to use the embedded AS3 player instead of the embedded AS2 player. The parameters used in the example have also been updated to only include parameters that the AS3 player supports.
In addition, the instructions for constructing the URLs that contain player parameters have been updated to reflect the URL formats used by the AS3 and AS2 embedded and chromeless players as well as by the HTML5 player.