gpt4 book ai didi

javascript - 使用youtube iframe播放器API播放背景视频

转载 作者:行者123 更新时间:2023-12-03 05:24:10 24 4
gpt4 key购买 nike

我已使用Youtube iframe Player API在我的使用 Angular <4构建的Web应用程序中显示播放列表并播放带有youtube id的youtube视频,在台式机浏览器中播放播放列表时一切都很完美,但是如果我在移动浏览器中打开Web应用程序,能够播放音乐和观看视频,但是一旦我最小化了我的移动浏览器视频,它就会暂停,并且在锁定移动设备时也是如此。
如何通过最小化和锁定屏幕在后台播放视频来继续听音乐?
我已如下配置我的iframe播放器API

  if (window['onYouTubeIframeAPIReady']) {
return this.initYoutubePlayer(videoId);
}

/**
* Initiate youtube iframe player.
*/
private initYoutubePlayer(videoId: string, resolve) {
this.youtube = new YT.Player('youtube-player', {
videoId: videoId,
playerVars: this.getPlayerVars(),
events: {
onReady: () => this.onPlayerReady(resolve),
onError: this.tryToPlayFallbackVideos.bind(this),
onStateChange: this.onYoutubePlayerStateChange.bind(this)
}
});
}

/**
* Handle youtube player state changes.
*/
private onYoutubePlayerStateChange(e: YT.OnStateChangeEvent) {
switch (e.data) {
case YT.PlayerState.ENDED:
this.state.firePlaybackEnded();
this.setState('playing', false);
break;
case YT.PlayerState.PLAYING:
this.numberOfTracksSkipped = 0;
this.setState('playing', true);
break;
case YT.PlayerState.PAUSED:
this.setState('playing', false);
break;
}
}

/**
* Get youtube player vars.
*/
private getPlayerVars(): YT.PlayerVars {
return {
autoplay: 0,
rel: 0,
showinfo: 0,
disablekb: 1,
fs: 0,
controls: 0,
modestbranding: 1,
iv_load_policy: 3,
playsinline: 1,
}
}

在移动浏览器中打开后,有什么方法可以在网络应用程序的背景下播放youtube视频吗?
任何建议都受到高度赞赏。

最佳答案

Mobile Considerations

Autoplay and Scripted Playback The HTML5 element, in certain mobile browsers (such as Chrome and Safari), only allows playback to take place if it's initiated by a user interaction (such as tapping on the player). Here's an excerpt from Apple's documentation:

"Warning: To prevent unsolicited downloads over cellular networks at the user’s expense, embedded media cannot be played automatically in Safari on iOS — the user always initiates playback."



由于此限制,因此功能和参数(例如自动播放,
playVideo(),loadVideoById()不适用于所有移动环境。

资料来源: https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations

关于javascript - 使用youtube iframe播放器API播放背景视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com