I'm trying to get an audio file to autoplay in Safari on an iPad. If I go to the page using Safari on my Mac, it's fine. On the iPad, autoplay does not work.
相关问题
- Can we recover audio from MFCC coefficients?
- CALayer - backgroundColor flipped?
- Is there a way to play audio on a mobile browser w
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
相关文章
- HTML5 control
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- Android Visualizer class throwing runtime exceptio
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
As of iOS 4.2.1, neither the fake click nor the .load() trick will work to autoplay audio on any iOS device. The only option I know of is to have the user actually perform a click action and begin playback in the click event handler without wrapping the .play() call in anything asynchronous (ajax, setTimeout, etc).
Someone please tell me if I'm mistaken. I had working loopholes for both iPad and iPhone before the most recent update, and all of them look like they've been closed.
Have puzzled over this whilst developing a quick prototype web app under iOS4.2 (dev build). Solution is actually quite simple. Note that you can't seem to already have the tag in your HTML page, you actually need to insert the tag into the document dynamically (note this example uses Prototype 1.7 for some extra Javascript snazziness):
As you've not set a controller, there shouldn't be any need to do anything tricksy with CSS to hide it / position it off-screen.
This seems to work perfectly.
I confirm that the audio isn't working as described (at least on iPad running 4.3.5). The specific issue is the audio won't load in an asynchronous method (ajax, timer event, etc) but it will play if it was preloaded. The problem is the load has to be on a user-triggered event. So if you can have a button for the user to initiate the playing you can do something like:
Then to play it, eg in an ajax request, you can do
Not the greatest solution, but it may help, especially knowing the culprit is the load function in a non-user-triggered event.
Edit: I found Apple's explanation, and it affects iOS 4+: http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html
Try calling the .load() method before the .play() method on the audio or video tag... which will be HTMLAudioElement or HTMLVideoElement respectively. That was the only way it would work on the iPad for me!
I handled this by attaching an event handler for all the events for which you are allowed to trigger audio to the body element which triggers any html audio elements with autoplay to play once.
This seems to work:
See it in action here: http://www.johncoles.co.uk/ipad/test/1.html (Archived)
As of iOS 4.2 this no-longer works. Sorry.