一个 YouTube 播放器,分别使用 2 个不同的 YouTube 视频作为音频和视频

发布于 2024-11-29 02:57:40 字数 142 浏览 0 评论 0原文

我想知道是否可以使用 (JavaScript) YouTube API 动态创建一个使用两个不同 YouTube 视频作为其音频和视频源的播放器。换句话说,是否可以在 YouTube API 中隔离一个视频的音频和另一个视频的音频,然后在一个播放器中将它们一起流式传输?

I want to know if it is possible to use the (JavaScript) YouTube API to dynamically create a player that uses two different YouTube videos for its audio and video sources. In other words, is it possible within the YouTube API to isolate audio of one video and video of another and then stream them together within one player?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-12-06 02:57:40

您可能可以通过加载并播放两个视频来伪造它,同时将纯音频视频保留在隐藏的 DIV 中,并使用 JS API 将纯视频视频静音。

所以我会尝试这样的事情:

 <script type="text/javascript" src="swfobject.js"></script>    
<div id="videodiv">
 The muted video will go here
</div>
<div id="audiodiv">
 the hidden video will go here
</div>

<script type="text/javascript">
var audioplayer; 
var videoplayer; 

var params = { allowScriptAccess: "always" };
var video_atts = { id: "vidonly" };
var audio_atts = { id: "audonly" };

// embed the video-only player
swfobject.embedSWF("http://www.youtube.com/e/VIDEO_ID?enablejsapi=1&playerapiid=ytplayer",
                   "videodiv", "425", "356", "8", null, null, params, atts);

// embed the audio-only player
swfobject.embedSWF("http://www.youtube.com/e/VIDEO_ID?enablejsapi=1&playerapiid=ytplayer",
                   "audiodiv", "425", "356", "8", null, null, params, atts);


function onYouTubePlayerReady(playerId) {
  audioplayer = document.getElementById("myytplayer");
  videoplayer  = document.getElementById("myytplayer"); 

  videoplayer.mute(); 

  audioplayer.play(); 
  videoplayer.play(); 
}

诀窍是使用 CSS 设置 audiodiv 的样式,以便它不会出现在页面上。您可能需要尝试一下才能找到最佳组合,但我猜测将其宽度和高度设置为 0 或 1 应该就足够了。

此外,您可能需要设置 onYouTubePlayerReady 函数,以确保两个视频在开始播放之前已完全加载。

you could probably fake it by loading and playing both videos while keeping the audio-only video in a hidden DIV, and using the JS API to mute the video-only video.

So I would try something like this:

 <script type="text/javascript" src="swfobject.js"></script>    
<div id="videodiv">
 The muted video will go here
</div>
<div id="audiodiv">
 the hidden video will go here
</div>

<script type="text/javascript">
var audioplayer; 
var videoplayer; 

var params = { allowScriptAccess: "always" };
var video_atts = { id: "vidonly" };
var audio_atts = { id: "audonly" };

// embed the video-only player
swfobject.embedSWF("http://www.youtube.com/e/VIDEO_ID?enablejsapi=1&playerapiid=ytplayer",
                   "videodiv", "425", "356", "8", null, null, params, atts);

// embed the audio-only player
swfobject.embedSWF("http://www.youtube.com/e/VIDEO_ID?enablejsapi=1&playerapiid=ytplayer",
                   "audiodiv", "425", "356", "8", null, null, params, atts);


function onYouTubePlayerReady(playerId) {
  audioplayer = document.getElementById("myytplayer");
  videoplayer  = document.getElementById("myytplayer"); 

  videoplayer.mute(); 

  audioplayer.play(); 
  videoplayer.play(); 
}

The trick then is to style audiodiv with CSS so that it doesn't appear on the page. You may need to play around with it to find the best combination, but I'm guessing that setting its width and height to 0 or 1 should be enough.

Also, you may need to set up the onYouTubePlayerReady function to make sure that both videos are completely loaded before you start playback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文