Control your 360 videos with the YouTube IFrame Player API

Ever since we launched 360° videos in 2015, we've been exploring ways to unleash the full potential of this new video format, including Cardboard mode, 360° live streams, and improved video quality. We are excited to share with you some new APIs for controlling 360° videos in embedded videos.

The Spherical Video Control API gives developers full control over the user’s perspective when using the YouTube IFrame Player SDK. Developers can get and set the view’s current yaw, pitch, roll, and field-of-view. This opens the door to many different scenarios such as narration-driven tours, custom controllers, multi-display installations all via JavaScript.



Here is a simple example of using the API. Google Spotlight Stories, collaborating with Justin Lin, brought us this wonderful story centered around a mysterious alien. We loved the experience, but it is easy to lose track of the alien while exploring the surroundings, so we added an “Alien” button to the video. Try wandering through the story, using your mouse to look around, and using the button to bring the alien back to the center of the scene.

We hope this helps you to incorporate 360° videos as an integral part of your applications and to create new and novel 360° experiences. To get you started, this short script will create an embed that pans in the horizontal direction while oscillating vertically.

<div id="player"></div>
<script src="https://www.youtube.com/iframe_api"></script>

<script>
let player;
let panStarted = false;

function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: 'FAtdv94yzp4',
events: {
'onStateChange': onPlayerStateChange
}
});
}

// Start animation when video starts playing.
function onPlayerStateChange(event) {
if (event.data == 1 && !panStarted) {
requestAnimationFrame(panVideo);
panStarted = true;
}
}

function panVideo() {
// 20 seconds per rotation.
const yaw = (performance.now() / 1000 / 20 * 360) % 360;
// 2 up-down cycle per rotation.
const pitch = 20 * Math.sin(2 * yaw / 360 * 2 * Math.PI);
player.setSphericalProperties({
yaw: yaw,
pitch: pitch
});
requestAnimationFrame(panVideo);
}
</script>

Yingyu Yao, Software Engineer, recently watched "The Earth's Internet: How Fungi Help Plants Communicate".