简体   繁体   English

iOS Safari Web Audio API 限制问题

[英]iOS Safari Web Audio API restriction problem

I'm currently building a Web Audio Player, which is already working on all major desktop browsers.我目前正在构建一个网络音频播放器,它已经在所有主要的桌面浏览器上运行。 However, Safari on iOS has a very strange behavior when it comes to its autoplay restrictions.然而,iOS 上的 Safari 在自动播放限制方面有一个非常奇怪的行为。

When a user loads up the audio player, he has two options to start playing a track.当用户加载音频播放器时,他有两个选项来开始播放曲目。 Either he clicks the obvious play button at the bottom of the play controls (I element) or he clicks a track in the playlist (div element).他要么单击播放控件底部的明显播放按钮(I 元素),要么单击播放列表中的曲目(div 元素)。

Both of these options execute the same code, the only difference is the event handler.这两个选项执行相同的代码,唯一的区别是事件处理程序。

The play button option works, safari is not complaining about it.播放按钮选项有效,safari 并没有抱怨它。 The playlist item click option does not work.播放列表项单击选项不起作用。 Safari gives me this error: Safari 给了我这个错误:

Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. Unhandled Promise Rejection: NotAllowedError: 当前上下文中用户代理或平台不允许该请求,可能是因为用户拒绝了权限。

Does Safari restrict by what kind of element click event handler is used? Safari 是否限制使用哪种元素单击事件处理程序?

Here's the code for both options:这是两个选项的代码:

var ePlay = jQuery('.player .player-controls .play-toggle'); 
// <--- <i> Element
var ePlaylistSingleTrack = jQuery('.player .playlist .single-track'); 
// <--- DIV Element

jQuery(ePlay).click(function(){
    if (audioElement.paused){
        playAndVisualize();
    } else {
        audioElement.pause();
        jQuery(this).removeClass('fa-pause-circle');
        jQuery(this).addClass('fa-play-circle');
    }
});

jQuery(ePlaylistSingleTrack).click(function(){
    if (audioElement.paused){
        playAndVisualize();
    } else {
        audioElement.pause();
        jQuery(this).removeClass('fa-pause-circle');
        jQuery(this).addClass('fa-play-circle');
    }
});

EDIT 1: I've tried using standard event listeners instead of using jquery.编辑 1:我尝试使用标准事件侦听器而不是使用 jquery。 No changes.没有变化。 I also tried using an I - Element instead of a div, but that also didnt help.我也尝试使用 I - 元素而不是 div,但这也没有帮助。

So Mobile Safari doesn't like fake click events because its a touch device.所以Mobile Safari 不喜欢虚假的点击事件,因为它是一个触摸设备。 you have to use touch events.你必须使用触摸事件。 Typically I do this on all projects because it solves other issues i have come across with mobile safari.通常我在所有项目上都这样做,因为它解决了我在移动 safari 中遇到的其他问题。

var myEvent = ('ontouchstart' in document.documentElement) ? 'touchend' : 'click';

document.querySelector('.your-selector-here').addEventListener(myEvent, function () {
    // Your Listener here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM