var videoPlayerScriptLoaded = false; var viewSongs = null; var videoPlayer = null; var videoPlayerMaxHeight = 400; var videoPlayerMinHeight = 50; var videoWindow = null; var videoPlayerDuration = 5*60; var videoWindowInterval = null; var hrefPlayInterval = null; var hrefPlayIntervalRetry = 0; var videoWindowTimeout = null; var playSeekProcessTimeout = null; var refreshSongIng = 0; // 目前正在播放的歌曲 var nowPlaySongVideoId = null; var nowPlaySongSongId = null; var nowPlaySongPrevVideoId = null; // 0 單曲重覆, 1 列表循環, 2 隨機播放, 3 電台 var videoPlayerMode = 0; var videoPlayerPrevMode = -1; // 0 是隱藏, 1 是顯示, 2 in popup window var videoPlayerStatus = 0; var videoPlayerPrevStatus = -1; function view_songs_message() { showLoading('歌曲讀取..'); } function view_songs_video_empty() { for (var i = viewSongs.length - 1; i >= 0; i--) { if(viewSongs[i]['videoId'] != '' || viewSongs[i]['cronStatus'] == '1') { return true; } }; return false; } function view_songs_data() { if(!viewSongs) { return false; } return viewSongs; } function view_songs(from, singerId, channelCode, albumId, singerType, fromType, pageNo, perPage) { if(viewSongs) { return viewSongs; } setTimeout(function(){ view_songs_message(); }, 1); // 取得每頁需要的歌曲 var baseUri = '/players/view_songs'; $.ajax({ type: 'GET', headers: {"cache-control": "no-cache"}, url: baseUri + '?rand=' + new Date().getTime(), async: true, cache: false, dataType : "json", data: { from: from, singer_id: singerId, channel_code: channelCode, album_id: albumId, singer_type: singerType, from_type: fromType, page_no: pageNo, per_page: perPage, }, success: function(response) { setTimeout(function(){ hideLoading(); if(response.status) { viewSongs = response.view_songs; // 網址有帶 song_id 就立即播放 if(typeof hrefPlay != 'undefined' && hrefPlay == '1') { hrefPlayInterval = setInterval(function() { if(videoPlayerScriptLoaded) { playSong(hrefPlayVideoId, hrefPlaySongId); clearInterval(hrefPlayInterval); } else if(hrefPlayIntervalRetry > 5) { clearInterval(hrefPlayInterval); } hrefPlayIntervalRetry++; }, 500); } else if(typeof autoPlayAlbum != 'undefined' && autoPlayAlbum == '1') { if(view_songs_video_empty()) { playAllAlbum(); } } else if(typeof autoPlaySong != 'undefined' && autoPlaySong == '1') { if(view_songs_video_empty()) { playAllSong(); } } else if(typeof hrefPlay != 'undefined' && hrefPlay == '2') { showLoading('目前沒有歌曲的影片..', 1000); } } else { showLoading(response.message, 1000); } }, 1); }, error: function(XMLHttpRequest, textStatus, errorThrown) { setTimeout(function(){ hideLoading(); }, 1); } }); return false; } function addCustomSongPopupProcess(videoId, songId) { addSong(songId, videoId); } function customSongPopupProcess(videoId, channelId, songId) { // show message showLoading('更換影片..'); var baseUri = '/players/process_custom_song'; $.ajax({ type: 'GET', headers: { "cache-control": "no-cache" }, url: baseUri + '?rand=' + new Date().getTime(), async: true, cache: false, dataType : "json", data: { song_id: songId, channel_id: channelId, video_id: videoId, }, success: function(response) { // hide message hideLoading(); // add song tr if(response.status) { update_view_song(response.song_id, response.song_name, response.singer_display_name, response.video_id, response.album_name, response.singer_id, response.duration, response.cron_status, response.start_seconds, response.end_seconds); // update_view_tr(response.song_id, response.song_name, response.singer_display_name, response.video_id, response.album_name, response.singer_id, response.duration, channelId, response.album_id); update_view_card(response.song_id, response.song_name, response.singer_display_name, response.video_id, response.album_name, response.singer_id, response.duration, channelId, response.album_id, response.song_title, response.song_description, response.song_url, response.site_url); } else { showLoading(response.message, 1000); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { hideLoading(); } }); return false; } function addCustomSongPopup() { $("#popupAddCustomSong").popup("open"); } function addCustomSong(songId) { var $customSongIframe = $('#popupAddCustomSongIframe'); if($customSongIframe.length > 0 && $customSongIframe[0].contentWindow && typeof $customSongIframe[0].contentWindow.addCustomSong == 'function') { $('#popupAddCustomSongIframe')[0].contentWindow.addCustomSong(songId); } return false; } function customSongPopup() { $("#popupCustomSong").popup("open"); } function customSong(songId, channelId, popupSelecter) { if(popupSelecter) { $(popupSelecter).popup('close'); } var $customSongIframe = $('#popupCustomSongIframe'); if($customSongIframe.length > 0 && $customSongIframe[0].contentWindow && typeof $customSongIframe[0].contentWindow.customSong == 'function') { $('#popupCustomSongIframe')[0].contentWindow.customSong(songId, channelId); } return false; } function getIndexByVideoId(videoId) { if(view_songs_data() == false) { return -1; } for (var i = 0; i < viewSongs.length; i++) { if(viewSongs[i].videoId == videoId) { return i; } }; return -1; } function getIndexBySongId(songId) { if(view_songs_data() == false) { return -1; } for (var i = 0; i < viewSongs.length; i++) { if(viewSongs[i].id == songId) { return i; } }; return -1; } function checkVideoWindow() { if (videoWindow && videoWindow.closed) { closeVideoWindow(); runNextVideo(); } } function closeVideoWindow() { // clear timer clearInterval(videoWindowInterval); clearTimeout(videoWindowTimeout); // close window if(videoWindow && !videoWindow.closed) { videoWindow.close(); } videoWindow = null; } function runVideoWindow(videoId) { if(view_songs_data() == false) { return false; } // 關掉原本的 window closeVideoWindow(); // open window videoPlayerStatus = 2; videoUrl = "https://www.youtube.com/watch?v="+videoId+"&loop=1"; videoWindow = window.open(videoUrl, "", "width=500, height=450"); // 檢查是否被關閉 timer videoWindowInterval = setInterval(checkVideoWindow, 500); // 播完後關閉 var songIndex = getIndexByVideoId(videoId); if(songIndex != -1) { closeSec = viewSongs[songIndex]['duration']; } else { closeSec = videoPlayerDuration; } videoWindowTimeout = setTimeout(function(){ closeVideoWindow(); runNextVideo(); }, closeSec*1000); } function runVideo(videoId, songId, direction) { closeVideoWindow(); if(!videoId) { var songIndex = getIndexBySongId(songId); var viewSong = viewSongs[songIndex]; if(refreshSongIng == 1) { return false; } // 這個 video 跑過 cron 但找不到 video 則跑下一個,不用去 refresh if(viewSong['cronStatus'] == '1') { refreshSong(songId, direction); } else { if(direction == '0') { runPrevVideo(); } else { runNextVideo(); } } return false; } if(view_songs_data() == false) { return false; } if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') { return false; } // nowPlaySongVideoId = videoId; // nowPlaySongSongId = songId; if(!videoPlayerScriptLoaded && typeof(YT) == 'undefined') { return false; } else { } // create player if(!videoPlayer) { videoPlayer = new YT.Player('videoPlayer', { videoId: videoId, events: { 'onReady': onVideoReady, 'onStateChange': onVideoStateChange, 'onError': onVideoError } }); } // load video else { videoPlayer.loadVideoById(videoId); var songIndex = getIndexBySongId(songId); var song = viewSongs[songIndex]; playSeekStart(song.startSeconds); renderPlaySeek(song.id); } renderVideoPlayer(); renderVideoTitle(); } function runPrevVideo() { closeVideoWindow(); if(view_songs_data() == false) { return false; } if(videoPlayerMode == 3) { broadSong(); return false; } var songIndex = getIndexBySongId(nowPlaySongSongId); if(songIndex == -1 && viewSongs.length > 0) { nowPlaySongVideoId = viewSongs[0]['videoId']; nowPlaySongSongId = viewSongs[0]['id']; runVideo(nowPlaySongVideoId, nowPlaySongSongId, '0'); return false; } // 全部播放 if(videoPlayerMode == 1) { if(songIndex == 0) { nowPlaySongVideoId = viewSongs[viewSongs.length -1]['videoId']; nowPlaySongSongId = viewSongs[viewSongs.length-1]['id']; } else { nowPlaySongVideoId = viewSongs[songIndex-1]['videoId']; nowPlaySongSongId = viewSongs[songIndex-1]['id']; } runVideo(nowPlaySongVideoId, nowPlaySongSongId, '0'); } // 重覆播放 else if(videoPlayerMode == 0) { runVideo(nowPlaySongVideoId, nowPlaySongSongId, '0'); } } function runNextVideo() { closeVideoWindow(); if(view_songs_data() == false) { return false; } if(videoPlayerMode == 3) { broadSong(); return; } // 如果所有歌曲都沒 video 則不再 run 下一個 if(!view_songs_video_empty()) { showLoading('沒有找到歌曲', 1000); return; } var songIndex = getIndexBySongId(nowPlaySongSongId); if(songIndex == -1 && viewSongs.length > 0) { nowPlaySongVideoId = viewSongs[0]['videoId']; nowPlaySongSongId = viewSongs[0]['id']; runVideo(nowPlaySongVideoId, nowPlaySongSongId); return; } // 全部播放 if(videoPlayerMode == 1) { if(songIndex+1 >= viewSongs.length) { nowPlaySongVideoId = viewSongs[0]['videoId']; nowPlaySongSongId = viewSongs[0]['id']; } else { nowPlaySongVideoId = viewSongs[songIndex+1]['videoId']; nowPlaySongSongId = viewSongs[songIndex+1]['id']; } runVideo(nowPlaySongVideoId, nowPlaySongSongId); } // 重覆播放 else if(videoPlayerMode == 0) { runVideo(nowPlaySongVideoId, nowPlaySongSongId); } } function playAddCustomSong(songId, popupSelecter) { if(!nowPlaySongSongId && !songId) { showLoading('你還沒播放任何歌曲無法加入歌單..', 1000); return false; } if(popupSelecter) { $(popupSelecter).popup('close'); } if(songId) { addCustomSong(songId); } else { addCustomSong(nowPlaySongSongId); } return false; } function playAddSong(songId) { if(!nowPlaySongSongId && !songId) { showLoading('你還沒播放任何歌曲無法加入歌單..', 1000); return false; } if(songId) { addSong(songId); } else { addSong(nowPlaySongSongId); } return false; } function playSeekStart(seconds) { if(!videoPlayer) { return false; } videoPlayer.seekTo(parseInt(seconds)); } function playSeekProcess(startSeconds, songId, channelId) { clearTimeout(playSeekProcessTimeout); if(!videoPlayer) { return false; } if(!songId) { return false; } // 預先記錄 start second var songIndex = getIndexBySongId(songId); var song = viewSongs[songIndex]; var endSeconds = getEndSeconds(song); update_view_song(song.id, song.name, song.singerDisplayName, song.videoId, song.albumName, song.singerId, song.duration, song.cronStatus, startSeconds, endSeconds); playSeekProcessTimeout = setTimeout(function(){ var baseUri = '/players/process_seek'; $.ajax({ type: 'GET', headers: { "cache-control": "no-cache" }, url: baseUri + '?rand=' + new Date().getTime(), async: true, cache: false, dataType : "json", data: { start_seconds: startSeconds, song_id: songId, channel_id: channelId, }, success: function(response) { if(!response.status) { showLoading(response.message, 1000); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { } }); }, 500); } function playNextSong() { videoPlayerMode = 1; runNextVideo(); return false; } function playPrevSong() { videoPlayerMode = 1; runPrevVideo(); return false; } function playRepeatSong() { if(view_songs_data() == false) { return false; } var songIndex = 0; if(nowPlaySongSongId) { songIndex = getIndexBySongId(nowPlaySongSongId); } var song = viewSongs[songIndex]; nowPlaySongVideoId = song['videoId']; nowPlaySongSongId = song['id']; repeatSong(nowPlaySongVideoId, nowPlaySongSongId); return false; } function playSingerCategory(popupSelecter) { if(popupSelecter) { $(popupSelecter).popup('close'); } $('#popupSingerCategory').popup('open'); return false; } function playShareSong(popupSelecter, siteUrl, songId) { if(!nowPlaySongSongId && !songId) { showLoading('你還沒播放任何歌曲無法分享..', 1000); return false; } if(popupSelecter) { $(popupSelecter).popup('close'); } var concat = ''; if(siteUrl.search(/\?/) != -1) { concat = '&'; } else { concat = '?'; } var songUrl = ''; if(songId) { songUrl = siteUrl + concat + 'song_id=' + songId; } else { songUrl = siteUrl + concat + 'song_id=' + nowPlaySongSongId; } $('#shareUrl').val(songUrl); $('#shareUrl').unbind('click').on("click", function () { $(this).select(); }); $('#popupShareSong').popup('open'); } function playAllAlbum() { if(view_songs_data() == false) { return false; } videoPlayerMode = 1; if(viewSongs.length > 0) { nowPlaySongVideoId = viewSongs[0]['videoId']; nowPlaySongSongId = viewSongs[0]['id']; runVideo(nowPlaySongVideoId, nowPlaySongSongId); } return false; } function playAllSong() { if(view_songs_data() == false) { return false; } videoPlayerMode = 1; if(viewSongs.length > 0) { nowPlaySongVideoId = viewSongs[0]['videoId']; nowPlaySongSongId = viewSongs[0]['id']; runVideo(nowPlaySongVideoId, nowPlaySongSongId); } return false; } function playSong(videoId, songId) { showLoading('播放..', 1000); closeVideoWindow(); videoPlayerMode = 1; var songIndex = getIndexBySongId(songId); var song = viewSongs[songIndex]; nowPlaySongVideoId = videoId; nowPlaySongSongId = songId; runVideo(videoId, songId); return false; } function update_view_card(songId, songName, singerDisplayName, videoId, albumName, singerId, duration, channelId, albumId, songTitle, songDescription, songUrl, siteUrl) { var newCard = '