简体   繁体   English

在iPhone网络应用中播放声音

[英]Play sound in iPhone web app

I assume there might be a HTML5 or some JS that can be used to play sound? 我假设可能有一个HTML5或一些JS可以用来播放声音?

Before you mark this as duplicate, this question is old, so I believe outdated: 在将此标记为重复之前,这个问题已经过时了,所以我相信过时了:

Play sound in iPhone web app with Javascript? 使用Javascript在iPhone网络应用中播放声音?

Maybe you could use a JS event, send a event to your UIWebView delegate and then play a sound with in objective-c ? 也许您可以使用JS事件,将事件发送到UIWebView委托,然后在objective-c中播放声音?

Best solution I think ^^ 最好的解决方案我认为^^

For a solution in HTML5 I have no idea. 对于HTML5中的解决方案,我不知道。 You could take a look at Sound effects in JavaScript / HTML5 您可以在JavaScript / HTML5中查看声音效果

But I'm not sure this solution would work on all device. 但我不确定这个解决方案是否适用于所有设备。 It depends if you need to play the sound "often" or not. 这取决于你是否需要“经常”播放声音。

If noticed some issues in Chrome but otherwise seems to work in other major browsers. 如果发现Chrome中存在某些问题,但在其他主流浏览器中似乎也有效。

HTML: HTML:

<audio id="sound_example" title="Sample" autobuffer>
  <source src="sample1.wav" type="audio/x-wav">
  <source src="sample2.ogg" type="application/ogg">
  <source src="sample3.mp3" type="audio/mpeg">
</audio>

Javascript: 使用Javascript:

var playThis = document.getElementById("sound_example");
if (!playThis.paused) {
    playThis.pause();
    playThis.currentTime = 0.0;
}
tmpAudio.play();

Obviously you'll need to provide your own wav , ogg , or mp3 to try this yourself. 显然你需要提供自己的wavoggmp3来自己试试。 The check for it being paused is there so if the condition is met, it will reset before playing it again. 暂停检查是否存在,如果满足条件,它将在重新播放之前重置。

If you'd like to skip the audio tag altogether you can go with this: 如果您想完全跳过音频标签,可以使用以下内容:

var sound_example = new Audio("sample3.mp3");
sound_example.play();

There are some minor pros and cons to both approaches but if you're needs are straight-forward then either should suffice. 这两种方法都有一些小的利弊,但如果你的需求很直接,那么要么就足够了。

The current version of mobile Safari (iOS 5.0.1) has poor audio HTML5 support. 当前版本的移动Safari(iOS 5.0.1)支持HTML5音频。 The play method of an Audio object will work sporadically, but only as the result of direct user click and it will not preload (expect a random delay on first play.) This makes it impractical to use audio in your iPhone web applications at this time. 偶尔使用Audio对象的播放方法,但只是直接用户点击的结果,它不会预加载(预计第一次播放时会出现随机延迟。)这使得此时在iPhone Web应用程序中使用音频变得不切实际。 。

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

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