Upload Youtube 當我們上傳影音到 youtube 後, 一般串接影音作法,只要點擊介面上的嵌入影音 就會得到一串 iframe 的方式! 但是今天我們需要偵測用戶暫停或者 replay,甚至記載點擊數 以上種種客製化,就需要使用到 youtube提供的 API
程式碼 index.html 1 2 3 4 5 6 7 8 9 10 11 12 <div class ="video" > <div id ="video_Player" > </div > </div > <script > var tag = document .createElement('script' ); tag.src = "https://www.youtube.com/iframe_api" ; var firstScriptTag = document .getElementsByTagName('script' )[0 ]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); </script >
app.js 1 2 3 4 5 6 7 import Creative_video from "./Creative_video.js" ;$(function ( ) { new Creative_video("video_Player" , 0 , 0 , 'vTtcFMY7-vI' ); })
Creative_video.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 class Creative_video { player = null ; stateDone = false ; firstPlay = true ; id = null ; sec = null ; auto = null ; videoid = null ; constructor (id, sec, auto, videoid) { this .id = id; this .sec = sec; this .auto = auto; this .videoid = videoid; if (typeof (YT) == 'undefined' || typeof (YT.Player) == 'undefined' ) { this .getYoutubeApi(); window .onYouTubeIframeAPIReady= () => { this .YouTubePlayer(); } } else { window .onYouTubeIframeAPIReady= () => { this .YouTubePlayer(); } } }; getYoutubeApi= () => { var tag = document .createElement('script' ); tag.src = "https://www.youtube.com/iframe_api" ; var firstScriptTag = document .getElementsByTagName('script' )[0 ]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); }; YouTubePlayer= () => { this .player = new YT.Player(this .id, { width: '100%' , height: '100%' , playerVars: { start: this .sec, rel: 0 , fs: 1 , autoplay: this .auto }, videoId: this .videoid, events: { onReady: () => { console .log('player is Ready' ); if (!isMobile()) { this .player.setVolume(50 ); this .player.playVideo(); } }, onStateChange: (event )=> { this .onStateChange(event); }, onError: (event )=> { this .catchError(event); } } }); }; onStateChange= (event )=> { if (!this .stateDone && event.data === 1 && this .firstPlay) { this .firstPlay = false ; } if (event.data === 0 ) { this .stateDone = true ; } if (this .stateDone && event.data === 1 ) { this .stateDone = false ; } }; catchError= (event )=> { if (event.data == 100 ) console .log("error" ); }; } export default Creative_video;
如果使用上述有問題 可以看一下你的.babelrc檔,我有使用babel-plugin-transform-class-properties
這套件簡單層面來看就是,你可以將狀態、參數寫在constructor之前來做管理,以及reset 一般的寫法是不准許在constructor之前寫下任何東西的!一定要在裡面做宣告!
1 2 3 4 5 6 7 { "presets": ["env","stage-2"], "plugins": [ "babel-plugin-transform-class-properties", "transform-async-to-generator" ] }
補充 很多面試官一定會詢問:你知道 arrow function 的使用方法嗎?
method 1 2 3 4 5 6 7 targetGetA() { console .log(this ); return this .targrt; }; targetGetB= () => { console .log(this ); };
上述targetGetB 就是一個arrow的使用
差別就是在引入一個第三方套件(JQuery)時
JQuery 1 2 3 $('html,body' ).animate({ scrollTop : $(anchor).offset().top }, 600 , this .targetGetA); $('html,body' ).animate({ scrollTop : $(anchor).offset().top }, 600 , this .targetGetB);
你會發現這兩個this
指向不同targetGetA
指向html和body
targetGetB
指向是類別的名稱Nav_to_scroll_click_handler
而this指向很重要! 所以你可以回面試官:差別就是在引入一個第三方套件(JQuery)時,你會發現這兩個this指向不同
Ref: 遇到: javascript - Youtube iFrame API 'YT is undefined'
https://code.i-harness.com/en/q/1ad8837 實作: 播放單支影片,其他暫停
http://jsfiddle.net/anubhavranjan/Y8P7y/ 官方: YouTube Player API Reference for iframe Embeds
https://developers.google.com/youtube/iframe_api_reference#playVideo 相關: JavaScript 的原型鍊
https://blog.techbridge.cc/2017/04/22/javascript-prototype/ 相關: Plugin 網路開源的裝置判斷CURRENT-DEVICE
https://github.com/matthewhudson/current-device