javascript – 音频无法在iPad上播放

<audio id="test" controls="controls">
    <source src="1.mp3" type="audio/mpeg" />
    <source src="1.ogg" type="audio/ogg" />
</audio>

js代码以dom加载播放视频

document.getElementById('test').load();
document.getElementById('test').play();

在浏览器中工作但在iPad中工作,我需要设置什么?

解决方法

根据apple的文档,音频/视频内容的加载和回放必须通过用户的操作触发:

In Safari on iOS (for all devices,including iPad),where the user may
be on a cellular network and be charged per data unit,preload and
autoplay are disabled. No data is loaded until the user initiates it.
This means the JavaScript play() and load() methods are also inactive
until the user initiates playback,unless the play() or load() method
is triggered by user action. In other words,a user-initiated Play
button works,but an onLoad=”play()” event does not.

This plays the movie: <input type="button" value="Play"
onClick="document.myMovie.play()">

This does nothing on iOS: <body onLoad="document.myMovie.play()">

取自这里:http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...