如何使用音频在移动Chrome应用 [英] How to use Audio in a Mobile Chrome App

查看:241
本文介绍了如何使用音频在移动Chrome应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标有当用户presses某一按钮的声音出现。

I'm aiming to have a sound occur when the user presses a certain button.

这是我的工作桌面上,并在Chrome上我的Andr​​oid 4.4的手机,但是当我推出这个作为一个科尔多瓦应用音频停止工作。

This is working on my desktop and in Chrome on my Android 4.4 phone but when I launch this as a cordova app the audio stops working.

<audio hidden='true' id='win-audio'>
  <source src='audio.mp3' type='audio/mpeg' />
</audio>

同当点击事件发生以下被称为

with the following being called when a click event happens

document.querySelector('#win-audio').load();
document.querySelector('#win-audio').play();

视频&GT;

我已经使用&LT试图标签来代替(具有相同的MP3加载)。我也试过OGG格式的情况下,这是一个codeC的问题,但没有运气。

I have tried using a <video> tag instead (with the same mp3 loaded). I've also tried the ogg format in case it's a codec issue but with no luck.

在PhoneGap的人解决这个使用媒体插件 - ?我宁愿做正确使用HTML5音频,但如果这是不可能的,是有移动Chrome应用同等

On PhoneGap people solve this using the Media plugin - I'd rather do this properly using HTML5 audio but if that's impossible, is there an equivalent for Mobile Chrome Apps?

推荐答案

尝试了许多解决方案,我发现我们可以用科尔多瓦媒体插件来取代缺乏HTML5音频支持后。

After trying many solutions I discovered that we can use the Cordova media plugin to substitute for lack of HTML5 audio support.

首先使用 CCA插件添加org.apache.cordova.media项目目录中安装的命令行插件

接下来将下面的行添加到您的config.xml文件:

Next add the following line to your config.xml file:

<plugin name="Media" value="org.apache.cordova.AudioHandler" />

然后,您可以使用新的媒体对象在JavaScript如下:

You can then use the new Media object from within Javascript as follows:

var audio = new Media('/android_asset/www/path_to_your.mp3');
audio.play();

请注意,您必须发出科尔多瓦预计,正确的路径,相对这不是JavaScript文件的code在上面将针对Android的工作,并支持iOS的看到:<一href=\"http://stackoverflow.com/questions/11175489/ios-phonegap-media-object-how-do-i-access-a-resource-in-the-www-folder\">iOS PhoneGap的媒体对象 - ?如何在WWW文件夹访问资源

Note that you must issue the correct path that Cordova expects, which is NOT relative to the Javascript file the code is in. The above will work for Android, and to support iOS see: iOS Phonegap Media Object - How do I access a resource in the WWW folder?

这篇关于如何使用音频在移动Chrome应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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