Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offering smaller versions of videos with media query or when mobiles are detected

I went to this discussion from 2012: How to load different video for mobile devices and desktop web version? because I was having the same problem. My mp4, webm and ogg videos of 7MB or so work fine on my desktop computer but hang up miserably on my Android smartphone.

@Subhajit gave this sample code that he had tried unsuccessfully:

<video controls>
<source src="mySmallVideo.webm" type="video/webm" media="all and  (max-width:600px)">
<source src="myVideo.webm" type="video/webm">
</video>

and was referred to a stackoverflow discussion on the best way to detect a handheld device.

But that only gave advice for detecting when the site was being viewed on a mobile device. Nobody offered alternative code to his sample, for showing a smaller version of the video after a mobile device was detected.

My responsive site CSS code has @media (max-width: media queries. The one for mobile devices is set at 500px. How could I modify the sample code of @Subhajit to show a smaller version of a video, perhaps referencing the media query @media (max-width: 500px) coding in my CSS file?

I'd rather not use mobile detection, commercial video players, or online storage sites.

Thanks, @Fabio Here is my code so far from your first suggestion:

<video controls>
<source src="portfolioVideos/testMovieWEnd480x270.mp4.mp4" type="video/mp4" media="all and (max-width: 500px)">
<source src="portfolioVideos/testMovieWEnd480x270.webmsd.webm" type="video/webm" media="all and (max-width: 500px)">
<source src="portfolioVideos/testMovieWEnd720x405.mp4.mp4" type="video/mp4"> 
<source src="portfolioVideos/testMovieWEnd720x405.webmsd.webm" type="video/webm">

I made two five-second "movies," consisting of a "test pattern" still which ends with a "the end" superimposed on it. The big movie for computer screens is 720x405 pixels and labeled "for 'puter." The small one for mobile screens is 480x270 pixels and is labeled "for mobile." When I alternate window sizes in Firefox and Safari (and refresh), they actually bring up the proper version. Chrome only shows the mobile version. (Reversing the order in the coded only makes everything worse.)

The mobile version also comes up when I enter the video html in Mobile Phone Emulator. But when I call it up on my Android smartphone, the vid is so minascule I can't read which version it is, or even click on it.

I looked at that the Chris Coyier page you recommended, but I couldn't figure it out. How would I search for more on this jQuery code? Thanks for previous and any possible follow.

I tried taking a couple of new tacks on this problem.

First, I tried using a jquery "window width" method and an if/else conditional to bring either a small or large version of the video into an iframe.

$(document).ready(function () {
if ($(window).width() < 550) {
<iframe src=... width="480" height="270"></iframe>
}
else {
<iframe src=... width="720" height="405"></iframe>
}
});
</script>

That didn't work. Safari and Chrome both give me an "Unexpected token '<'" error, which I understand "happens when you're including or posting to a file which doesn't exist." These files exist and work when opened directly. I tried using absolute urls, as recommended by @amenadiel, but that didn't work either.

Second, I tried "window location" with an if/else conditional.

$(document).ready(function () {
if ($(window).width() < 330) {
window.location.href = ...;
}
else {
window.location.href = ...;
}
});

This works when tested in the various simulated devices and window sizes on two online emulators. But it doesn't work on my Android smartphone. There, just a tiny, empty, unclickable video window in the upper left of the screen.

I may have answered my own question.

I found here: https://github.com/etianen/html5media/wiki/embedding-video

this super simple code:

<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
</head>

<body>
<video poster="poster.jpg" width="618" height="347" controls preload> 
<source src="video.mp4" media="only screen and (min-device-width: 568px)"></source> 
<source src="video.iphone.mp4" media="only screen and (max-device-width: 568px)"></source> 
</video>

which seems, miraculously, to have worked. Check out this page, http://willsoper.net/videoPortfolio.html which contains two videos, each with a large and small version. Try the "Test Pattern" one on the right first; it's only ten seconds long. The larger version is labeled "for 'puter" and the smaller one, "for Mobi." On my Samsung Galaxy Android in vertical position, the "Mobi" version appears. If I hit the back button, turn the phone to horizontal, and try it again, the larger, "'puter" version appears.

The video on the left, with the man, also has a small and large version and seems to work the same way. It is much longer and on my smartphone it sometimes re-buffers briefly once or twice.

Ironically, my previous coding worked in online emulators but not on my actual smartphone. This one works on my smartphone but not on the emulators.

I'd appreciate knowing what others, especially @Fabio , experience and how I might improve on the code.

like image 252
WillS Avatar asked Sep 05 '14 21:09

WillS


People also ask

What is media query and why it is used?

Media queries are a key part of responsive web design, as they allow you to create different layouts depending on the size of the viewport, but they can also be used to detect other things about the environment your site is running on, for example whether the user is using a touchscreen rather than a mouse.

Why we use only screen in media query?

only screen: The only keyword is used to prevent older browsers that do not support media queries with media features from applying the specified styles.


2 Answers

Well, you have 2 options:

1) Serving video based on media queries just as you mention. You simply set different sizes based on the device width, like this:

<video> 
   <source src="video-ipad.mp4" type="video/mp4" media="all and (max-width: 768px)"> 
   <source src="video-mobi.mp4" type="video/webm" media="all and (max-width: 768px)"> 
   <source src="video-mobi.mp4" type="video/mp4" media="all and (max-width: 480px)"> 
   <source src="video-mobi.webm" type="video/webm" media="all and (max-width: 480px)"> 
   <source src="video.mp4" type="video/mp4"> 
   <source src="video.webm" type="video/webm"> 
</video>

You can also use some JQuery screen detection as shown on this page

var mainVideo = $('#the-video');

if ($(window).width() < 1200 && medQualVersion) {
  mainVideo.append("<source type='video/mp4' src='" + medQualVersionSrc + "' />");
} else {
  mainVideo.append("<source type='video/mp4' src='" + highQualVersionSrc + "' />");
}
mainVideo.append("<source type='video/webm' src='" + webMSrc + "' />");

// Wait until sources are appended to call MediaElements.js
mainVideo.mediaelementplayer();

The second option is to simply server high quality video and resize it, like this:

video{max-width:100%; height:auto}

this way is bullet proof and always work, not to mention you serve better quality video. But of course, you'll have the data transfer issue, so for big videos I'd recommend method 1 .

like image 105
Devin Avatar answered Nov 03 '22 01:11

Devin


well, Frist add class to each video I have two videos with different size

<div class="fullscreen-video-wrap" height="800px">
        <video id="video" class="vidbig" src="./picture/ThisEgypt.mp4" autoplay="true" loop="true" muted="false" width="100%" ></video>

        <video id="video" class="vid-mobi" src="./picture/Dolphin.mp4" autoplay="true" loop="true" muted="false" width="100%" ></video>

</div>

and on css media query

@media only screen and (min-width: 900px){
    .vid-mobi{
        display: none;}
}
@media only screen and (max-width: 900px){
    
    .vidbig {
    display: none;
   }
}
like image 21
Hossam Elnaggar Avatar answered Nov 03 '22 01:11

Hossam Elnaggar