Server side
Data streaming, can be cached online
//Get parameters var params=(,true).query const ROOT_PATH = ();//The absolute path must be used, and the relative path will directly download the file let path =ROOT_PATH+; let stat = (path); //Get file information let fileSize = ; let range = ; if (range) { //The 206 status code is used only if there is a range header let parts = (/bytes=/, "").split("-"); let start = parseInt(parts[0], 10); let end = parts[1] ? parseInt(parts[1], 10) : start + 9999999; // The end value is fileSize - 1 end = end > fileSize - 1 ? fileSize - 1 : end; let chunksize = (end - start) + 1; let file = (path, { start, end }); let head = { 'Content-Range': `bytes ${start}-${end}/${fileSize}`, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4', }; (206, head); (res); //The server-side HTTP response in Node is a Writable stream } else { let head = { 'Content-Length': fileSize, 'Content-Type': 'video/mp4', }; (200, head); (path).pipe(res); }
Client
1. Install the video-player plug-in
cnpm install vue-video-player --save
2. References in components
<video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions" ></video-player>
3. Data in the called data
data() { return { // Video playback playerOptions: { playbackRates: [0.5, 1.0, 1.5, 2.0], //Play speed autoplay: false, //If true, playback starts when the browser is ready. muted: false, // By default, any audio will be eliminated. loop: false, // causes the video to start again as soon as it ends. preload: 'auto', // It is recommended that the browser start downloading video data after the <video> loads the element. Auto browser selects the best behavior and start loading videos immediately (if the browser supports it) language: 'zh-CN', aspectRatio: '16:9', // Put the player in smooth mode and use this value when calculating the dynamic size of the player. The value should represent a proportion - two numbers separated by colons (e.g. "16:9" or "4:3") fluid: true, // When true, the player will have the fluid size. In other words, it will scale to fit its container. sources: [{ type: "", //src: require('@/assets/'+)//url address src: 'http://localhost:10086/videos?url=/public/videos/'+, //url address, the request needs to include the specific video file name }], poster: '', //Your cover address // width: , notSupportedMessage: 'This video cannot be played for the time being,Please try again later', //Allows to overwrite the default information displayed when the media source cannot be played. controlBar: { timeDivider: true, durationDisplay: true, remainingTimeDisplay: false, fullscreenToggle: true //Full screen button } } } },
appendix: vue+flvjs play live stream FLV, how to disconnect the live stream before when paging
Use the flvjs library to play flv files at the same time, and you need to find out that the live stream has not been disconnected before, which greatly affects performance. Please refer to the following code to disconnect the live stream on the previous page
// Destroy the player instance closePlayer() { if ( > 0) { ((item) => { (true); ('ended', function () {}); ('error', function () {}); item = null; }); = []; } }, // Initialize the player initPlayer(id, url, img) { let doms = (id); = ''; ( new FlvPlayer({ id: id, url: url, poster: img, isLive: true, autoplay: true, volume: id == 'videos0' ? 0.5 : 0, preloadTime: 10, minCachedTime: 5, cors: true, }) ); [ - 1].on('ended', () => { //The event name can be found in the above query (); }); [ - 1].on('error', () => { //The event name can be found in the above query (); }); }, // Page turn operation currentChange(e) { (); = e; (); }, // Get the list in the live broadcast async getLivingList() { let res = await this.$req(, { ..., pageno: - 1, pagesize: , }); (res); if ( == 200) { // Get the data, assign the value and then loop instantiate it ([0]); ([0]); ([0]); ([0]); = ; = .total_page - 0; = .total_showers; = false; if ( > 0) { setTimeout(() => { ((item, idx) => { let domid = 'videos' + idx; ( domid, item.play_url, this.$ + ); }); }, 400); } else { // If you return without live broadcast data, it is not the first page. Just skip to the first page and request it if ( > 1) { = 1; (); } } } },
Summarize
This is the article about the example code for online video playback in vue+node. For more related online video playback content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!