如何在HTML中用视频外部播放音频? [英] How can I play audio externally with video in html?

查看:133
本文介绍了如何在HTML中用视频外部播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何在外部将音频与html中的视频一起播放?

How can I play audio externally with video in html?

例如:

<video controls="controls" style="margin-bottom:20px;width:590px">
<source src="https://drive.google.com/uc?export=download&amp;id=1MQMkk7BUzx5K4vYt5s5GXUXTiVLAd20q" type="video/ogg" /> 
<source src="https://drive.google.com/uc?export=download&amp;id=1r9HFQeSUjE2InM7LOuNx7qaMzaVkQNfF" type="video/mp4" />
</video>

.ogg文件是音频文件,.mp4是视频文件 当用户单击播放按钮时,我希望他们观看视频,但音频来自.ogg文件

The .ogg file here is an audio file and the .mp4 is a video file When users click on the play button I want them to see the video but for the audio to come from the .ogg file

注意:.mp4文件具有自己的音频,但我不希望用户听该音频,我希望他们听.ogg文件中的外部音频

NOTE: .mp4 file have its own audio but I don't want users to listen to that audio I want them to listen to the external audio from .ogg file

推荐答案

那是您的代码:

<video controls="controls" style="margin-bottom:20px;width:590px">
    <source src="https://drive.google.com/uc?export=download&amp;id=1MQMkk7BUzx5K4vYt5s5GXUXTiVLAd20q" type="video/ogg" /> 
    <source src="https://drive.google.com/uc?export=download&amp;id=1r9HFQeSUjE2InM7LOuNx7qaMzaVkQNfF" type="video/mp4" /> 
</video>

提供视频和音频标签的"id"属性

Give Your Video and Audio Tags "id" attribute

<video id="video" controls muted>
    <source src="https://drive.google.com/uc?export=download&amp;id=1r9HFQeSUjE2InM7LOuNx7qaMzaVkQNfF" type="video/mp4" />
    <audio id="audio" controls>
        <source src="https://drive.google.com/uc?export=download&amp;id=1MQMkk7BUzx5K4vYt5s5GXUXTiVLAd20q" type="video/ogg" />
    </audio>
</video>

并编写脚本

<script>
    var v = document.getElementById("myvideo");
    var a = document.getElementById("myaudio");
    v.onplay  = function() {
        a.play(); 
    }
    v.onpause = function() {
        v.pause();
    }
</script>

仅此而已. 实际上,这里发生的是,当播放视频时,将同时播放音频文件,并且每当暂停视频时,也会暂停音频. 同时,您的视频将始终处于静音状态.

That's all. Actually, What's happening here is that when your video will be played than your audio file will be played simultaneously and whenever your video will be paused than your audio will be paused also. At the same time, your video will be muted all the time.

这篇关于如何在HTML中用视频外部播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆