Solved

Autoplay video in mobile device

Posted on 2016-11-04
3
112 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

896 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

18 Experts available now in Live!

Get 1:1 Help Now