Solved

Autoplay video in mobile device

Posted on 2016-11-04
3
60 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
  • 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 30

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 30

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now