Solved

Autoplay video in mobile device

Posted on 2016-11-04
3
273 Views
Last Modified: 2016-11-07
Is there a hack to auto play mp4 video in mobile device.  It seems as if this functionality is turned off in mobile devices but I am looking for a script/hack to over rife it.
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 41874973
you mean, people open a web page and you play the mp4 on their mobile phone right ?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41876019
Unfortunately, many browsers in mobiles have disabled autoplay function: play() method can only be iniitiated by user gesture.
What you can do is to add an event listener to some event. For instance I attached it to window touchstart using MediaElement.js:
		<script>
			$(document).ready(function () {
				var player = new MediaElementPlayer('#ID-OF-AUDIO-OR-VIDEO-TAG');
				player.load();
				window.addEventListener('touchstart', function audioStart() {
					player.play();
					this.removeEventListener('touchstart', audioStart);
				});
			});
		</script>

Open in new window

0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41876026
But I've just read in my console this message:
Performing operations that require explicit user interaction on touchstart events is deprecated and will be removed in M54, around October 2016. See https://www.chromestatus.com/features/5649871251963904 for more details.
So, it looks like Google doesn't want allow us to get play() working ecept the user don't explicitely tap on play button.
You can, if you want to increase the probability the audio/video start, using something like this:
			$(document).ready(function () {
				var player = new MediaElementPlayer('#player2');
				var playerPlaying = false;
				player.load();
				$('body').on('click', function () {
					if (playerPlaying) {
						player.pause();
						playerPlaying = false;
					} else {
						player.play();
						playerPlaying = true;
					}
				});
				window.addEventListener('touchstart', function audioStart() {
					player.play();
					this.removeEventListener('touchstart', audioStart);
					playerPlaying = true;
				});
			});
		</script>

Open in new window

This ensure that if user taps everywhere, the media starts playing. In addition, touchstart and body click will work together, at least until Chrome wont remove touchstart ability to trigger events whih require user gesture.
0

Featured Post

2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question