简体   繁体   中英

HTML video player

I want to play video file from local disk in browser so I found this code. Can You tell me why it does not work? On jsfiddle it works but when i copy it into project it won't work. Also can You tell me what gives function declaration like function name(x){variables}(window) .

The error I get is Uncaught TypeError: Cannot read property 'addEventListener' of null

Thanks for Your help :)

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

    <script>
        (function localFileVideoPlayerInit(win) {
            var URL = win.URL || win.webkitURL,
                    playSelectedFile = function playSelectedFileInit(event) {
                        var file = this.files[0];
                        var type = file.type;
                        var videoNode = document.querySelector('video');
                        var canPlay = videoNode.canPlayType(type);
                        canPlay = (canPlay === '' ? 'no' : canPlay);
                        var message = 'Can play type "' + type + '": ' + canPlay;
                        var isError = canPlay === 'no';
                        displayMessage(message, isError);
                        if (isError) {
                            return;
                        }
                        var fileURL = URL.createObjectURL(file);
                        videoNode.src = fileURL;
                    },
                    inputNode = document.querySelector('input');
            if (!URL) {
                displayMessage('Your browser is not ' +
                        '<a href="http://caniuse.com/bloburls">supported</a>!', true);
                return;
            }
            inputNode.addEventListener('change', playSelectedFile, false);
        }(window));
    </script>

    <h1>HTML5 local video file player example</h1>
    <div id="message"></div>
    <input type="file" accept="video/*"/>
    <video controls autoplay></video>
</body>
</html>

The problem is, your code is running before the DOM is ready/loaded.

There's two way to fix this.

1) Move the the entire javascript code block below <video controls autoplay></video>

or

2) Use document.addEventListener("DOMContentLoaded", function() { }); like this:

<script>
document.addEventListener("DOMContentLoaded", function() {
    var URL = window.URL || window.webkitURL,
            playSelectedFile = function playSelectedFileInit(event) {
                var file = this.files[0];
                var type = file.type;
                var videoNode = document.querySelector('video');
                var canPlay = videoNode.canPlayType(type);
                canPlay = (canPlay === '' ? 'no' : canPlay);
                var message = 'Can play type "' + type + '": ' + canPlay;
                var isError = canPlay === 'no';
                displayMessage(message, isError);
                if (isError) {
                    return;
                }
                var fileURL = URL.createObjectURL(file);
                videoNode.src = fileURL;
            },
            inputNode = document.querySelector(("input[type=file]"));
    if (!URL) {
        displayMessage('Your browser is not ' +
                '<a href="http://caniuse.com/bloburls">supported</a>!', true);
        return;
    }
    inputNode.addEventListener('change', playSelectedFile, false);
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM