iPhone HTML5音频标签无效 [英] iPhone HTML5 Audio tag not working

查看:367
本文介绍了iPhone HTML5音频标签无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的音频标签无法在iPonhe上运行时出现问题。它在W3Schools网站上测试时播放HTML5音频标签,但不适用于我的下方

I have a problem with an audio tag not working on an iPonhe. It plays HTML5 Audio tags when tested on W3Schools website but will not work with my one below

 <audio autoplay ><source  src="audio/alarm1.mp3" type="audio/mpeg"></audio>

此代码在桌面和Android浏览器上运行正常,而不是ipod或iphone safari

This code works fine on desktop and android browsers, just not ipod or iphone safari

已编辑

似乎音频文件工作正常,它不会自动播放

It would seem that the audio file is working ok, it just will not autoplay

我有没有办法让用户按下按钮然后播放音乐没有用户干预?

Is there a way I could let the user press a button and there after play the music when ever it is required without user intervention?

推荐答案

Autoplay在iOS和Android上不起作用。
他们需要某种用户互动来播放音频或视频。

Autoplay does not work on iOS and android. They require some kind of user interaction to play an audio or a video.

如果你在用户事件上播放音频(点击/ touchstart),它会发挥得很好。

If you play the audio on a user event(click/touchstart), it will play just fine.

如果你没有应该播放的用户事件,我通常做的是在文档上绑定一个touchstart事件并播放并暂停第一个音频我得到了touchstart。
然后,每当我需要播放音频时,我再次播放它。 (如果使用用户交互播放和暂停音频一次,则可以在下次没有用户交互的情况下再次播放。)

If you don't have a user event on which it should play, What I usually do is bind a touchstart event on document and play and pause the audio on the first touchstart I get. Then whenever I need to play the audio, I play it again. (If the audio is played and paused once using a user interaction, you can play it again the next time without a user interaction).

//play and pause it once
document.addEventListener('touchstart', function () {
    document.getElementsByTagName('audio')[0].play();
    document.getElementsByTagName('audio')[0].pause();
});

//now play whenever you want it to play

这篇关于iPhone HTML5音频标签无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆