Solved

Autoplay video in mobile device

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A new Acronis survey shows people often have conflicting thoughts about data protection.
Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
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.

617 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