Change Audio From Multple Audio Tracks Video

断了今生、忘了曾经 提交于 2020-12-10 07:56:50

问题


i have videos with multiple audio tracks. i want to play the video and want change audio tracks from video. Is there any way to make this in html or there is html supported player for this?


回答1:


There is an API for it, but it's not very supported. However, there is a plugin for video.js called videojs-audio-tracks, but it still uses the audioTracks API, which isn't supported by Chrome, and needs manual enabling in Firefox.




回答2:


If you want to grab the audio track from an html5 video you can get the video element by id and then use the .audioTracks array and pass in the index of the track you with to access.

var video = document.getElementById("video");

for (var i = 0; i < video.audioTracks.length; i += 1) {
  video.audioTracks[i].enabled = false;
}

MDN Docs: HTMLMediaElement.audioTracks

Furthermore, if you are looking to manipulate the audio, William provides a good answer (https://stackoverflow.com/a/15286719/6931862) using AudioContext

Below is the response provided in the link above, but with some edits to account for the updated adoption for AudioContext (used to be webkitAudioContext)

<script>

var video = document.createElement("video");
video.setAttribute("src", "ourMovie.mov");

video.controls = true;
video.autoplay = true;
document.body.appendChild(video);

var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();         // get access to audio context
var gainNode = audioCtx.createGain();
gainNode.gain.value = 1;                   // Change Gain Value to test
filter = audioCtx.createBiquadFilter();
filter.type = 2;                          // Change Filter type to test
filter.frequency.value = 5040;            // Change frequency to test

// Wait for window.onload to fire. See crbug.com/112368
window.addEventListener('load', function(e) {
  // Our <video> element will be the audio source.
  var source = audioCtx.createMediaElementSource(video); 
  source.connect(gainNode);
  gainNode.connect(filter);
  filter.connect(audioCtx.destination);

}, false);


</script>


来源:https://stackoverflow.com/questions/57248671/change-audio-from-multple-audio-tracks-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!