Picture in picture and FullScreen together – API can only be initiated by a user gesture

huangapple go评论137阅读模式
英文:

Picture in picture and FullScreen together - API can only be initiated by a user gesture

问题

Sure, here is the translated content you requested:

我正在同时运行两个视频,就像这张图片一样:

Picture in picture and FullScreen together – API can only be initiated by a user gesture

有一个名为“进入全屏”的按钮。当有人点击该按钮时,我想要做两件事:

  1. 视频播放器 2 将设置为画中画模式,并且
  2. 视频播放器 1 将设置为全屏模式。

我可以实现全屏或画中画,但无法同时实现全屏和画中画。错误信息如下:

> 在“元素”上执行“requestFullscreen”时失败:API只能由用户手势启动。
> 未捕获(在承诺中)TypeError:全屏错误

我正在使用 jQuery,以下是我的示例代码:

$('.enter-full-screen').click(event => {
  event.stopImmediatePropagation();
  event.stopPropagation();

  let pipResponse = $('#video-player-2')[0].requestPictureInPicture();

  pipResponse.then(() => {
    $('#video-player-1')[0].requestFullscreen() // 注意:我正在使用浏览器前缀
      .then(/* ... */)
      .catch(/* ... */);
  })
});

**更新:2020年7月1日:**我尝试同时请求两者,但也不起作用。它只对我首先请求的一个有效。

let pipResponse = $('#video-player-2')[0].requestPictureInPicture();
let fullscreenResponse = $('#video-player-1')[0].requestFullscreen();

Promise.all([pipResponse, fullscreenResponse])
    .then(/* 代码 */)
    .catch(/* 代码 */);

在这种情况下,只有画中画有效,全屏请求会引发错误。如果我首先请求全屏,那么只有全屏有效 - 画中画会引发错误。

> 我尝试使用 jQuery trigger('click') 来自动触发另一个点击事件。只能用于一个(画中画或全屏),但不能同时运行两者!

我非常感谢您的帮助。

英文:

I am running two video on top of one another like this picture :

Picture in picture and FullScreen together – API can only be initiated by a user gesture

There is a button named 'Enter FullScreen'. When someone click that button,i want to do two things.

  1. Video Player 2 will be set as picture-in-picture and
  2. Video Player 1 will be set as fullscreen.

I can do either fullscreen or picture-in-picture, but can't do fullscreen and picture-in-picture together. Error throws like this:

>Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.
>Uncaught (in promise) TypeError: fullscreen error

I am using jQuery and here is my sample code:

$('.enter-full-screen').click(event => {
  event.stopImmediatePropagation();
  event.stopPropagation();

  let pipResponse = $('#video-player-2')[0].requestPictueInPicture();

  pipResponse.then(() => {
    $('#video-player-1')[0].requestFullscreen() // Note: I am using a browser prefixes
      .then(/* ... */)
      .catch(/* ... */);
  })
});

Update: 07.01.2020: I tried request both simultaneously, but it doesn't work also. It works for only one which i request first.

let pipResponse = $('#video-player-2')[0].requestPictueInPicture();
let fullscreenResponse = $('#video-player-1')[0].requestFullscreen();

Promise.all([pipResponse, fullscreenResponse])
    .then(/* code */)
    .catch(/* code */);

In this case only pip works and fullscreen request throws an error. If i request fullscreen first, then only fullscreen works - pip throws an error.

> I tried with jQuery trigger('click') for automatic triggering another click event along with one. Works for only one(pip or fullscreen), BUT DOESN'T WORK BOTH TOGETHER!

I really appreciate your help.

答案1

得分: 2

以下是您要求的翻译内容:

"I'm not sure if the picture-in-picture (PiP) API is the right tool for this job - the fullscreen and PiP behaviour seem to be mutually exclusive in this case.

Since you're already emulating the PiP behaviour (as shown in your picture) you should be able to take the same approach when going fullscreen.

Instead of trying to make the individual video elements fullscreen/PiP, make a single common parent element of both videos fullscreen. Then you can just position the small video on top of the large one (as you're already doing) to give the picture-in-picture effect.

<!-- 1. Give the video players a common ancestor -->
<div id="video-group">
<div id="video-player-1">...</div>
<div id="video-player-2">...</div>
</div>

$('.enter-full-screen').click(event => {
event.stopImmediatePropagation();
event.stopPropagation();

// 2. Make the ancestor element fullscreen, not the videos themselves
$(&#39;#video-group&#39;)[0].requestFullscreen()
    .then(/* ... */)
    .catch(/* ... */);

});

Here is a quick-and-dirty example doing "picture-in-picture" with two YouTube videos:

<button type="button"
onclick="document.querySelector('#video-group').requestFullscreen();">
Enter fullscreen
</button>

<div id="video-group">
<iframe style="position: absolute"
width="100%" height="100%"
src="https://www.youtube.com/embed/9bZkp7q19f0" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

&lt;iframe style=&quot;position: absolute; bottom: 0; right: 0;&quot;
width=&quot;560&quot; height=&quot;315&quot;
src=&quot;https://www.youtube.com/embed/dQw4w9WgXcQ&quot; frameborder=&quot;0&quot; 
allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;

</div>"

请注意,我已经去除了代码部分并提供了翻译的文本。

英文:

I'm not sure if the picture-in-picture (PiP) API is the right tool for this job - the fullscreen and PiP behaviour seem to be mutually exclusive in this case.

Since you're already emulating the PiP behaviour (as shown in your picture) you should be able to take the same approach when going fullscreen.

Instead of trying to make the individual video elements fullscreen/PiP, make a single common parent element of both videos fullscreen. Then you can just position the small video on top of the large one (as you're already doing) to give the picture-in-picture effect.

&lt;!-- 1. Give the video players a common ancestor --&gt;
&lt;div id=&quot;video-group&quot;&gt;
	&lt;div id=&quot;video-player-1&quot;&gt;...&lt;/div&gt;
	&lt;div id=&quot;video-player-2&quot;&gt;...&lt;/div&gt;
&lt;/div&gt;


$(&#39;.enter-full-screen&#39;).click(event =&gt; {
	event.stopImmediatePropagation();
	event.stopPropagation();
	
	// 2. Make the ancestor element fullscreen, not the videos themselves
	$(&#39;#video-group&#39;)[0].requestFullscreen()
		.then(/* ... */)
		.catch(/* ... */);
});

Here is a quick-and-dirty example doing "picture-in-picture" with two YouTube videos:

&lt;button type=&quot;button&quot;
	onclick=&quot;document.querySelector(&#39;#video-group&#39;).requestFullscreen();&quot;&gt;
	Enter fullscreen
&lt;/button&gt;

&lt;div id=&quot;video-group&quot;&gt;
	&lt;iframe style=&quot;position: absolute&quot;
	width=&quot;100%&quot; height=&quot;100%&quot; 
	src=&quot;https://www.youtube.com/embed/9bZkp7q19f0&quot; frameborder=&quot;0&quot;
	allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;

	&lt;iframe style=&quot;position: absolute; bottom: 0; right: 0;&quot;
	width=&quot;560&quot; height=&quot;315&quot;
	src=&quot;https://www.youtube.com/embed/dQw4w9WgXcQ&quot; frameborder=&quot;0&quot; 
	allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2020年1月6日 17:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定