54

Struggling with this problem for a few days in a row now...

Is there any way or 'hack' to disable playing video fullscreen on Safari on a iPhone. Of course I already tried the 'webkit-playsinline' attribute, but this will only work in your own app.

See:

<video class="" poster="" webkit-playsinline>
    <source src="" type="video/ogg" preload="auto">
    <source src="" type="video/mp4" preload="auto">                
</video>

Also I've tried to put the video on canvas, but as it looks video as a source for the canvas drawImage() method is not currently supported on iOS.

Is there any other way or alternative technique I can use? Or did I really waste my time the last few days?

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
  • 1
    Have you seen http://stackoverflow.com/questions/3699552/html5-inline-video-on-iphone-vs-ipad-browser – web_bod Oct 29 '13 at 18:23

7 Answers7

72

In iOS 10+

Apple finally enabled the attribute playsinline in all browsers on iOS 10, so this will work seamlessly:

<video src="file.mp4" playsinline>

In iOS 8 and iOS 9

You can work around this issue by simulating the playback by skimming the video instead of actually .play()'ing it.

In short, use iphone-inline-video, it takes care of the playback and audio sync (if any), and it keeps the <video> working as it should.

fregante
  • 29,050
  • 14
  • 119
  • 159
19
    <div id="video-player">
        <video src="http://movies.apple.com/media/us/html5/showcase/2011/demos/apple-html5-demo-tron-us_848x352.m4v"></video>
        <p><a href="javascript:playPause();">Play/Pause</a></p>
   </div>

   <script type="text/javascript">
        // intercept and cancel requests for the context menu
        var myVideo = document.querySelector('video');
        myVideo.addEventListener("contextmenu", function (e) { e.preventDefault(); e.stopPropagation(); }, false);

        // hide the controls if they're visible
        if (myVideo.hasAttribute("controls")) {
            myVideo.removeAttribute("controls")   
        }

        // play/pause functionality
        function playPause() {
            if (myVideo.paused)
                myVideo.play();
            else
                myVideo.pause();
         }

         // essentially you'll have to build your own controls ui
         // for position, audio, etc.

    </script>

The idea is to:

  1. Prevent the user getting to the context menu (to show the controls)
  2. Hide any player controls that might be visible

The downside is that you have to implement your own player UI - but it's not too complicated

*This code is only intended to show you how to solve the problem, not for use in a live application

A bit more research on the subject finds:

webkit-playsinline Indicates that a video element should play in-line instead of full-screen.

Related Tags “video” Availability Available in iOS 4.0 and later. (Enabled only in a UIWebView with the allowsInlineMediaPlayback property set to YES. source

I'm afraid it just not going to be possible using the video player in Safari

They have a guide for Video on Canvas, but as you probably know it isn't supported in IOS yet: video on canvas

This document summarises the current restrictions around mobile media content in IOS: mobile video status

web_bod
  • 5,728
  • 1
  • 17
  • 25
  • Also this resulted in a fullscreen playback. Though I definitely see the idea behind it. – Sebass van Boxel Oct 25 '13 at 12:07
  • I noticed on my Windows Phone that video always plays fullscreen - I have no choice in the matter, it may not be possible through a browser - why do you need this behaviour? (there might be a different approach) – web_bod Oct 25 '13 at 12:10
  • Certain elements within the website have to be triggered on specific timecodes. – Sebass van Boxel Oct 25 '13 at 12:28
4

just add 'playsinline':

in javascript:

yourclassName.setAttribute('playsinline', '');

in html:

<video class="Vidoo" src="https://.../video.mp4" playsinline></video>
mabbasi
  • 81
  • 3
2

My understanding is that iOS always plays video full screen. On Apple's own website they used encoded image data and Javascript drawing to do video-like playback. Here is a breakdown of how they did it:

https://docs.google.com/document/pub?id=1GWTMLjqQsQS45FWwqNG9ztQTdGF48hQYpjQHR_d1WsI

Dithermaster
  • 6,223
  • 1
  • 12
  • 20
1

I encountered this issue while developing a mobile app using react native. My solution was easy/stressful. If you are using Webview, this is my solution. Also, something to mention, If you are using low power mode on an ios device, you cannot autoplay a video. There is a function of some sort to get by it but for now, this will work.

import { WebView } from "react-native-webview";
<WebView
    style={styles.container}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    allowsInlineMediaPlayback={true}
    source={{
      html: `
      <video
      id = "video"
      style="width: 100%; height: 100%; object-fit: cover;"
      poster="" width="50%" height="50%" muted=" loop="" playsinline
      autoplay>
      <source src="https://aparissi.com/wp-content/uploads/2021/10/SplashVideo-1.mp4">
      <source>
    </video>
    `,
    }}
  />

In react native webview, there a property named "allowsInLineMediaPlayback". Set it to true! You also must have playsinline inside the video tag aswell. Another thing to mention was that you cannot auto-play a video if you don't have the muted property either. Happy Coding!

1

Don't be a muppet like me and forget that if using react, you have to use playsInline instead of playsinline. Trap for young players...

<video class="" poster="" playsInline webkit-playsInline>
    <source src="" type="video/ogg" preload="auto">
    <source src="" type="video/mp4" preload="auto">                
</video>
  • The documentation says to use small letter `i` [here](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/Attributes.html#//apple_ref/doc/uid/TP40008058-SW30) – Syfer Apr 01 '23 at 10:21
0

I managed to make it work by using the mp4 video as the preloader "poster".

my code:

<video poster="/path/to/video.mp4" autoplay loop muted webkit-playsinline playsinline type="video/mp4">
    Your browser does not support the video tag.
</video>

I just wanted a mp4 to be displayed as a GIF... I realized that the preloader itself was working perfectly. And.. to prevent a possible future failure with other browsers, I just added a condition to check if the current browser is a iOS browser, if yes, then I use this non sense work around, otherwise, I use the normal video tag with SRC and a preload gif on the "poster" parameter.

  • I just want to say that this worked PERCETLY, like a charm! Even better than I could ever imagine, because as I am using cordova and ALL my .mp4 files are locally on the app itself, there is 0 delay. As soon as the page loads (less than a second) the "mp4 as git" starts looping – Lincon Rezende Dec 17 '22 at 03:09