简体   繁体   English

在手机上自动播放音频 safari

[英]Autoplay audio on mobile safari

Before I get flamed to death, I know this doesn't work currently due to Apple's concern over downloading an audio file automatically.在我被活活烧死之前,我知道这目前行不通,因为苹果公司担心自动下载音频文件。

However, my question is: Has anyone found a cunning workaround yet?但是,我的问题是:有没有人找到一个巧妙的解决方法? I just want to play a start up sound on the launch of a game and currently have to wait for the user to click somewhere before I can play the audio.我只想在游戏启动时播放启动声音,目前必须等待用户点击某处才能播放音频。 One of you clever chaps must have got this working by now?你们其中一个聪明的家伙现在一定已经开始工作了吗?

There is no chance to get autoplay working in mobile browsers.没有机会在移动浏览器中进行自动播放。 Android and iOS doesn't allow it and personally I think that is a feasible confinement! Android 和 iOS 不允许,我个人认为这是可行的限制! Imagine every second website you will open plays and ugly sound at start!想象一下,每隔一个网站,您都会在开始时打开播放和丑陋的声音!

But you can make a little hack, so that the user will not remark that he currently started the audio for your application:但是你可以做一些小改动,这样用户就不会评论他当前为你的应用程序启动了音频:

  1. You WILL need an user interaction to start your audio.您将需要用户交互来启动您的音频。 So, your app or game maybe has a startscreen or a welcome button which needs a click to get to mainmenu or start the game.因此,您的应用程序或游戏可能有一个开始屏幕或一个欢迎按钮,需要单击才能进入主菜单或开始游戏。 Bind to an user event (the accepted events are: "click", "touchend", "doubleclick" and "keydown") and call the load() method for your <audio> .绑定到一个用户事件(接受的事件是:“click”、“touchend”、“doubleclick”和“keydown”)并为您的<audio>调用 load() 方法。

  2. Bind to the "canplaythrough" event of the <audio> .绑定到<audio>的“canplaythrough”事件。 This event is triggered when your source is ready to play.当您的源准备好播放时会触发此事件。 Here you can now call play(), pause() or wait for other interactions.您现在可以在这里调用 play()、pause() 或等待其他交互。 So the audio is ready but now you have full controll when to start or stop the sound.因此音频已准备就绪,但现在您可以完全控制何时开始或停止声音。

  3. I also advise you to use audio sprites on mobile clients.我还建议您在移动客户端上使用音频精灵。 iOS (and Android?) internally implemented audio support through a Singleton. iOS(和 Android?)通过 Singleton 在内部实现音频支持。 That means that you cannot, like in desktop browser, have 10 audio elements and play differents sound at once.这意味着您不能像在桌面浏览器中那样拥有 10 个音频元素并同时播放不同的声音。 You can only play one file.您只能播放一个文件。 So changing the source for different sounds takes to much time.因此,改变不同声音的来源需要花费很多时间。 With an audio sprite you can start your sprites when the user first interact with your website or game.使用音频精灵,您可以在用户首次与您的网站或游戏互动时启动您的精灵。 Pause your sprite and when you need to play sound you have to set the currentTime to the beginning of the sprite and pause the sprite when currentTime of your file reaches the end of your sprite.暂停您的精灵,当您需要播放声音时,您必须将 currentTime 设置为精灵的开头,并在文件的 currentTime 到达精灵的末尾时暂停精灵。 There is an timeupdate Event where your can check the currentTime of your sprite.有一个 timeupdate 事件,您可以在其中检查精灵的当前时间。

If you are more interested I can prepare my javascript audio sprites player for you!!如果您更感兴趣,我可以为您准备我的 javascript 音频精灵播放器!!

Only solution I have seen so far is to create a shell app and put the web app inside a UIWebView.到目前为止我看到的唯一解决方案是创建一个 shell 应用程序并将 web 应用程序放在 UIWebView 中。

http://flowz.com/2011/03/apple-disabled-audiovideo-playback-in-html5/ http://flowz.com/2011/03/apple-disabled-audiovideo-playback-in-html5/

UIWebView.allowsInlineMediaPlayback = YES;
UIWebView.mediaPlaybackRequiresUserAction = NO;

I also would really like to know how to do this in a plain-old web app.我也很想知道如何在普通的网络应用程序中执行此操作。

I believe I just solved this for my own app.我相信我刚刚为我自己的应用程序解决了这个问题。

The steps,步骤,

Controller loads up,控制器加载,

Then.... in viewDidLoad然后....在viewDidLoad

have your web view load the HTML: loadHTMLString:htmlFile baseURL:[self getBasePath]];让您的网络视图加载 HTML:loadHTMLString:htmlFile baseURL:[self getBasePath]];

THEN... set mediaPlaybackRequiresUserAction = NO on the webview.那么... 在 webview 上设置 mediaPlaybackRequiresUserAction = NO。

If you set it too soon, I think the load for the html resets it.如果你设置得太早,我认为 html 的负载会重置它。

I have a bot chat app that has voice messages and I needed them to be autoplayed whenever needed... so here is what worked for me in my angular app:我有一个带有语音消息的机器人聊天应用程序,我需要它们在需要时自动播放......所以这在我的角度应用程序中对我有用:

just attach a click eventlistener to document and call player.load() , after that whenever you set player.src = x;只需将点击事件监听器附加到文档并调用player.load() ,之后无论何时设置player.src = x; it will autoplay.它会自动播放。

<audio id="voicePlayer" controls autoplay playsinline #voicePlayer></audio>

@ViewChild('voicePlayer', { read: ViewContainerRef }) voicePlayerRef: ViewContainerRef;

...

  ngAfterContentInit(): void {
    this.voicePlayer = this.voicePlayerRef.element.nativeElement;
    document.addEventListener('click', this._onDocumentClick.bind(this));
  }

  _onDocumentClick() {
    this.voicePlayer.load();
    document.removeEventListener('click', this._onDocumentClick);
  }

...


this.voicePlayer.src = 'x';

HowlerJS creates is a workaround. HowlerJS创建的是一种解决方法。 The user doesn't need to allow autoplay for the audio to be played automatically用户不需要允许自动播放音频自动播放

Explanation from Docs on how this workaround is created: Docs 关于如何创建此解决方法的说明:

howler.js is an audio library for the modern web. It defaults to Web Audio API and falls back to HTML5 Audio. howler.js 是现代 web 的音频库。它默认为 Web 音频 API 并回落到 HTML5 音频。 This makes working with audio in JavaScript easy and reliable across all platforms.这使得在所有平台上轻松可靠地处理 JavaScript 中的音频。

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

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