如何播放音频上点击一个Android应用程序按钮使用的PhoneGap /科尔多瓦建? [英] How do I play audio on clicking a button in an Android app built using PhoneGap/Cordova?

查看:95
本文介绍了如何播放音频上点击一个Android应用程序按钮使用的PhoneGap /科尔多瓦建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个基本的音板般的应用程序,它有几个按钮,播放声音。我使用的PhoneGap /科尔多瓦和我使用的PhoneGap构建服务来编译code到APK。起初,我用简单的HTML5 <音频> 这在桌面浏览器和Android浏览器工作正常,但是当打包到PhoneGap的应用程序标签,无声音正在播放。

I'm building a basic soundboard-like app which has a few buttons that play sounds. I'm using PhoneGap/Cordova and I'm using the PhoneGap Build service to compile the code to APK. At first, I was using simple HTML5 <audio> tags which worked fine in the desktop browser as well as the Android browser, but when packaged into the PhoneGap app, no sounds were being played.

我环顾四周,看到一些提到不必使用标准的HTML5音频PhoneGap的媒体API,而不是,所以我切换到与下面的code的变化。

I looked around and saw some mentions of having to use the Media API in PhoneGap instead of standard HTML5 audio, so I switched to that with the following code changes.

我已经制定了以下功能:

I have the following function in place:

function doPlay(soundId) {
  var my_media = new Media("/android_asset/www/"+soundId+".mp3",
    function() {
      navigator.notification.alert('Success!', alertDismissed);
    },
    function(err) {
    navigator.notification.alert('Error!', alertDismissed);
  });
  my_media.play();
}

每个按钮都有一个的onclick 处理类似这样...

Each button has an onclick handler similar to this...

onclick="doPlay('sound1');"

该文件在同一目录 index.html的,名为 sound1.mp3 sound2.mp3 等。而功能 alertDismissed()也被定义,但它是空的。

The files are in the same directory as the index.html, called sound1.mp3, sound2.mp3 and so on. And the function alertDismissed() has also been defined, but it's empty.

但是,它仍然无法正常工作。目前在点击没有声音。没有成功错误消息无论是。是的,我已经得到了的通知传媒的权限要求。

However, it still does not work. There is no sound on click. There is no 'Success' or 'Error' message either. Yes, I've got notification and media permissions requested.

我在这儿住难倒。任何想法?

I'm quite stumped here. Any ideas?

更新:的所建议的@西蒙·麦克唐纳,我把一个完整路径的本地文件(/ android_asset / WWW /),但仍然没有得到应用程序的工作。没有声音,没有错误/成功警报。 : - (

Update: As suggested by @simon-macdonald, I put a full path to the local files ("/android_asset/www/"), but that still hasn't gotten the app to work. No sound, no error/success alerts. :-(

推荐答案

的路径,你的文件是不正确的。你想做的事:

The path to you files is not correct. You want to do:

var my_media = new Media("/android_asset/www/" + soundId+".mp3",
function() {
  navigator.notification.alert('Success!', alertDismissed);
},
function(err) {
navigator.notification.alert('Error!', alertDismissed);
});
my_media.play();

看看我的小教程。

Check out my mini tutorial.

http://simonmacdonald.blogspot.ca/ 2011/05 /使用介质类功能于phonegap.html

这篇关于如何播放音频上点击一个Android应用程序按钮使用的PhoneGap /科尔多瓦建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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