javascript – 使用iframe在YouTube应用中嵌入YouTube视频时内存泄漏
作者:互联网
我想在我的Metro应用中播放一些YouTube视频.我使用YouTube Iframe API(Link)在我的应用中嵌入了YouTube视频.然后我遇到了严重的内存泄漏问题.如果我嵌入了一个YouTube视频,然后将其删除,内存将增加大约5MB,并且智能手机将减少.演示代码在这里:
default.html中:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>iframeTest</title>
<!-- WinJS references -->
<link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
<!-- iframeTest references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js"></script>
</head>
<body style ="">
<button id="remove" style="display:block; float:left;">remove a video</button>
<button id="add" style="display:block; float:left;">add a video</button>
</body>
</html>
default.js片段:
document.getElementById("remove").addEventListener("click", function () {
var ifrs = document.querySelectorAll('div');
if (ifrs.length > 0) {
document.body.removeChild(ifrs[ifrs.length - 1]);
}
});
document.getElementById("add").addEventListener("click", function(){
var testYoutubeDiv = document.createElement('div');
testYoutubeDiv.style.cssFloat = 'left';
testYoutubeDiv.style.width = '400px';
testYoutubeDiv.style.height = '300px';
MSApp.execUnsafeLocalFunction(function () {
testYoutubeDiv.innerHTML = "<iframe id='player' type='text/html' width='400' height='300' src='http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1&origin=http://example.com' frameborder='0'></iframe>";
});
document.body.appendChild(testYoutubeDiv);
});
然后,我写了一个类似的.html文件,并在IE10.0和Chrome中进行测试.我发现IE10.0也有内存泄漏问题,但铬没有. IE10.0中的内存泄漏问题不如Metro App严重.
测试HTML代码在这里:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>iframeTest</title>
<script type = "text/javascript">
function load() {
document.getElementById("remove").addEventListener("click", function () {
var ifrs = document.querySelectorAll('div');
if (ifrs.length > 0) {
document.body.removeChild(ifrs[ifrs.length - 1]);
}
});
document.getElementById("add").addEventListener("click", function () {
var testYoutubeDiv = document.createElement('div');
testYoutubeDiv.style.cssFloat = 'left';
testYoutubeDiv.style.width = '400px';
testYoutubeDiv.style.height = '300px';
testYoutubeDiv.innerHTML = "<iframe id='player' type='text/html' width='400' height='300' src='http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1&origin=http://example.com' frameborder='0'></iframe>";
document.body.appendChild(testYoutubeDiv);
});
}
</script>
</head>
<body onl oad ="load()">
<button id="remove" style="display:block; float:right;">remove a video</button>
<button id="add" style="display:block; float:right;">add a video</button>
</body>
</html>
我注意到IE(可能还有Metro App)不使用WebKit Engine来处理javascript代码.有没有办法减少Metro App中的内存泄漏?
解决方法:
在html文件中创建iframe
<iframe id="ifr_video"></iframe>
然后使用javascript向该iframe添加来源,同时添加来源通过拆分查找YouTube网址的ID,例如,如果YouTube网址为http://www.youtube.com/watch?v=ZnehCBoYLbc,ID为ZnehCBoYLbc,则定义源代码,如下所示,以查找ID并添加来源
var Url_splits = Youtube_Video_url.split("/");
var Url_length = Url_splits.length;
var Ytube_id = Url_splits[(Url_length - 1)];
document.getElementById("ifr_video").src = "http://www.youtube.com/embed/" + Ytube_id;
在HTML中
<video id="VideoSource" style="border:1px solid black;" controls="controls" ></video>
<iframe class="youtube-player" id="ifr_video" type="text/html" width=100% height=100% allowfullscreen frameborder="0"></iframe>
在JS(内部就绪功能)
id("VideoSource").msPlayToSource.connection.addEventListener("statechanged", playToSrcStateChangeHandler);
function playToSrcStateChangeHandler(eventIn) {
var states = Windows.Media.PlayTo.PlayToConnectionState;
if (eventIn.currentState === states.disconnected) {
WinJS.log && WinJS.log("PlayTo connection state: Disconnected", "sample", "status");
} else if (eventIn.currentState === states.connected) {
WinJS.log && WinJS.log("PlayTo connection state: Connected", "sample", "status");
} else if (eventIn.currentState === states.rendering) {
WinJS.log && WinJS.log("PlayTo connection state: Rendering", "sample", "status");
}
}
Video_Path = "http://www.youtube.com/embed/" + new_id;
//new_id is the Youtube video id For example,..
in http://www.youtube.com/watch?v=ZnehCBoYLbc , id is ZnehCBoYLbc
function playYouTubeVideo(Video_Path) {
document.getElementById("ifr_video").src = Video_Path;
id("VideoSource").style.display='none';
}
function playWebContent(Video_Path) {
document.getElementById("ifr_video").style.display = 'none';
var localVideo = id("VideoSource");
var videoStabilization = Windows.Media.VideoEffects.videoStabilization;
localVideo.src = Video_Path;
localVideo.play();
}
参考:http://code.msdn.microsoft.com/windowsapps/Media-PlayTo-Sample-fedcb0f9
标签:javascript,youtube,iframe,microsoft-metro,html 来源: https://codeday.me/bug/20190709/1410514.html