Code Monkey home page Code Monkey logo

iphone-inline-video's Introduction

iphone-inline-video

Make videos playable inline on Safari on iPhone (prevents automatic fullscreen)

gzipped size iOS 8 and 9.3 supported Travis build status npm version

This lets you:

  • Play videos without forcing the fullscreen on the iPhone (demo)
  • Play silent videos without user interaction
  • Autoplay silent videos with the autoplay attribute (demo)
  • Play multiple silent videos at the same time

Demo

Main features

  • ~1KB, standalone (no frameworks required)
  • No setup: include it, call makeVideoPlayableInline(video), done
  • No custom API for playback, you can just call video.play() on click
  • Supports audio
  • Supports autoplay on silent videos
  • Doesn't need canvas
  • Doesn't create new elements/wrappers
  • It works with existing players like jPlayer

Limitations:

  • Needs user interaction to play videos with sound (standard iOS limitation)
  • Currently limited to iPhone, untested on Android, unneeded on iPad
  • The video framerate depends on requestAnimationFrame, so avoid expensive animations and similar while the video is playing. Try stats.js to visualize your page's framerate
  • Known issues

Install

npm install --save iphone-inline-video
import makeVideoPlayableInline from 'iphone-inline-video';

If you don't use node/babel, include this:

<script src="dist/iphone-inline-video.browser.js"></script>

Usage

You will need:

  • a <video> element

     <video src="file.mp4"></video>
  • the native play buttons will still trigger the fullscreen, so it's best to hide them (without breaking the video controls)

     .IIV::-webkit-media-controls-play-button,
     .IIV::-webkit-media-controls-start-playback-button {
         opacity: 0;
         pointer-events: none;
         width: 5px;
     }
  • the activation call

     // one video
     var video = document.querySelector('video');
     makeVideoPlayableInline(video);
     // or if you're already using jQuery:
     var video = $('video').get(0);
     makeVideoPlayableInline(video);
     // or if you have multiple videos:
     $('video').get().forEach(makeVideoPlayableInline)

Done! It will only be enabled on iPhones and iPod Touch devices.

Now you can keep using it just like you would on a desktop. Run video.play(), video.pause(), listen to events with video.addEventListener() or $(video).on(), etc...

BUT you still need user interaction to play the audio, so do something like this:

makeVideoPlayableInline(video);
video.addEventListener('touchstart', function () {
	video.play();
});

If at some point you want to open the video in fullscreen, use the standard (but still prefixed) webkitEnterFullScreen() API, but it has some caveats.

Usage with audio-less videos

If your video file doesn't have an audio track, then you need this:

makeVideoPlayableInline(video, /* hasAudio */ false);

Usage with autoplaying videos

You can also have silent videos autoplay. This module can load and play the video without user interaction, but not play the audio, so you have to set the hasAudio to false

makeVideoPlayableInline(video, /* hasAudio */ false);

Once that's run, if video has the autoplay attribute, it will automatically start playing:

<video autoplay muted src="video.mp4"></video>

License

MIT © Federico Brigante

iphone-inline-video's People

Contributors

bfred-it avatar timdp avatar wpsmithtwc avatar webpapaya avatar

Watchers

Andrew Kim avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.