Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of playing Local Files by HTML5 Video/Audio

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to play local files in HTML5 Video/Audio". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "HTML5 Video/Audio 's method of playing local files".

We can still play local files with the permission of the user, insert an input node in the page, specify type as file, and then set the url to the SRC value of audio or video

During this time, I often see developers asking the same question again and again: why can't local media files be played by setting the src property? For example, video.src= "D:/test.mp4".

This is because the JavaScript in the browser cannot directly access local resources (such as file systems, cameras, microphones, etc.) unless the user has prior permission. It is also necessary for browsers to impose this restriction. Imagine that if JavaScript can unscrupulously access the local file system, it will be easy to steal users' private data. When users visit a web page on the network, they unknowingly store their credit card numbers and passwords on their machines. Private files such as the company's secret files may have been uploaded to remote servers by malicious JavaScript programs, which is intolerable for users.

We can still play local files with the permission of the user. Here is a method.

Insert an input node in the page and specify type as file. If you need to play multiple files, you can add the attribute multiple. Register the callback function when the file node is updated, call the URL.createObjectURL function in the callback function to get the url of the file you just selected, and then set the url to the SRC value of audio or video.

The code example is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report