加载/卸载 HTML5

发布于 2024-10-19 16:55:07 字数 1094 浏览 2 评论 0原文

我开发了一个带有多个 UIWebView 的 iPad 应用程序,它采用以下 HTML 和 JavaScript。由于 iPad 一次只能播放一个视频,因此在特定的 Web 视图聚焦之前我不会加载视频。

这是通过调用 UIWebView 上的 stringByEvaluatingJavaScriptFromString 方法,发送对 JS 方法 getFocus() 的调用来完成的。同样,当不再需要 Web 视图时,我调用lostFocus() 来卸载视频。这使得另一个 UIWebView 可以播放另一个视频。

到目前为止,一切都很好。一切都很完美。除了一件事:这会导致内存泄漏。每当观看时,

我都尝试重新加载 UIWebView(而不是使用 JS 卸载视频),但这不起作用。我还尝试了无数不同的 JavaScript 变体、标签变体等等。

<div id="videoDiv"></div>

<script type="text/javascript">
    var movieDiv = document.getElementById('videoDiv'),
        movieHtml = '<video id="video" src="../Documents/<%= VideoFileName %>" width="768" height="911"></video>';

    var gotFocus = function () {
        movieDiv.innerHTML = movieHtml;
        var movie = document.getElementById('video');
        movie.play();
        movie.addEventListener('pause', function () {
            movie.currentTime = 0;
            movie.play();
        }, false);
    };
    var lostFocus = function () {
        movieDiv.innerHTML = '';
    };
</script>

I've developed an iPad app with several UIWebViews that takes the following HTML and JavaScript. Since the iPad can only play one video at a time, I don't load the video until the particular web view is focused.

This is done by calling the stringByEvaluatingJavaScriptFromString method on the UIWebView, sending in a call to the JS method getFocus(). Similarly, when the web view is no longer needed I call lostFocus() to unload the video. This enables another UIWebView to play another video.

So far, so good. Everything works perfectly. Except for one thing: This causes a memory leak. Whenever viewing

I've tried reloading the UIWebView (instead of unloading the video with JS), that didn't work. I've also tried a zillion of different JavaScript variations, tag variations et cetera.

<div id="videoDiv"></div>

<script type="text/javascript">
    var movieDiv = document.getElementById('videoDiv'),
        movieHtml = '<video id="video" src="../Documents/<%= VideoFileName %>" width="768" height="911"></video>';

    var gotFocus = function () {
        movieDiv.innerHTML = movieHtml;
        var movie = document.getElementById('video');
        movie.play();
        movie.addEventListener('pause', function () {
            movie.currentTime = 0;
            movie.play();
        }, false);
    };
    var lostFocus = function () {
        movieDiv.innerHTML = '';
    };
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-10-26 16:55:07

我终于解决了这个问题!解决方案是清空 src 并“加载”不存在的视频。这不会导致内存泄漏。看看:

<div id="videoDiv"></div>

<script type="text/javascript">
    var movieDiv = document.getElementById('videoDiv'),
        movieHtml = '<video id="video" src="" width="768" height="911"></video>';

    movieDiv.innerHTML = movieHtml;
    var movie = document.getElementById('video');

    var gotFocus = function () {
        movie.src = '../Documents/<%= VideoFileName %>';
        movie.load();
        movie.play();
        movie.addEventListener('pause', function () {
            movie.currentTime = 0;
            movie.play();
        }, false);
    };
    var lostFocus = function () {
        movie.src = '';
        movie.load(); // This line may be removed, see comments
    };
</script>

I finally solved this! The solution was to empty the src and "load" a non-existing video. This causes no memory leak. Have a look:

<div id="videoDiv"></div>

<script type="text/javascript">
    var movieDiv = document.getElementById('videoDiv'),
        movieHtml = '<video id="video" src="" width="768" height="911"></video>';

    movieDiv.innerHTML = movieHtml;
    var movie = document.getElementById('video');

    var gotFocus = function () {
        movie.src = '../Documents/<%= VideoFileName %>';
        movie.load();
        movie.play();
        movie.addEventListener('pause', function () {
            movie.currentTime = 0;
            movie.play();
        }, false);
    };
    var lostFocus = function () {
        movie.src = '';
        movie.load(); // This line may be removed, see comments
    };
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文