播放声音在Meteor-Cordova应用程序 [英] Play Sound In Meteor-Cordova App
问题描述
使用没有cordova的流星我可以使用
在我的浏览器中播放一个音效('test.mp3') .play()
其中 test.mp3
位于公用文件夹中。但是,一旦我在设备上运行我的应用程序作为cordova应用程序,我就无法播放声音。我做错了什么?谢谢。
tl; dr
-
安装cordova媒体插件
meteor add cordova:org.apache.cordova.media@0.2.15
-
将音频文件添加到公共目录
-
使用
/android_asset/www/application/path/to/sound.wav
如果您查看项目中的此文件夹,
$ PROJECT_ROOT / .meteor / local / cordova-build / platforms / android / assets / www /
您将看到您的应用程序为cordova构建它为android。
使用Cordova媒体插件,我试图使用文件路径 /android_asset/www/path/to/sound.wav
无效。
#安装带有此命令的cordova媒体插件
meteor add cordova:org.apache.cordova.media@0.2.15
相反,我看到我的声音文件夹是在www目录,但在应用程序目录下。所以,这个文件路径最后为我工作,
/android_asset/www/application/path/to/sound.wav
以下是对我有用的相关代码。
function getMediaUrl(sound){
if(device.platform.toLowerCase()===android){
return cordova.file.applicationDirectory.replace file://','')+'www / application /'+ sound.substr(1);
}
else {
return cordova.file.applicationDirectory.replace('file://','')+ sound.substr
}
}
function playSound(sound){
return new Media(
getMediaUrl(sound),
function(success){
// success
},
function(err){
// error
}
);
}
var test = playSound('/ sounds / test.wav');
test.play();
注意:我将音频文件包含在公用文件夹中, p>
$ PROJECT_ROOT / public / sounds / test.wav
在Android上,此文件路径将翻译为
/android_asset/www/application/sounds/test.wav
Using meteor without cordova I can play a sound fine in my browser using
new Audio('test.mp3').play()
where test.mp3
is located in the public folder. However I cannot play the sound once I run my app as a cordova app on a device. What am I doing wrong? Thanks.
tl;dr
Install cordova media plugin
meteor add cordova:org.apache.cordova.media@0.2.15
Add audio files to public directory
Use
/android_asset/www/application/path/to/sound.wav
If you look inside this folder in your project,
$PROJECT_ROOT/.meteor/local/cordova-build/platforms/android/assets/www/
You will see your application as cordova built it for android.
Using the Cordova Media plugin, I attempted to use the file path /android_asset/www/path/to/sound.wav
to no avail.
# Install the cordova media plugin with this command
meteor add cordova:org.apache.cordova.media@0.2.15
Instead, I saw that my sounds folder was inside the www directory but under an application directory. So, this file path ended up working for me,
/android_asset/www/application/path/to/sound.wav
Here's the relevant code that worked for me.
function getMediaUrl(sound) {
if (device.platform.toLowerCase() === "android") {
return cordova.file.applicationDirectory.replace('file://', '') + 'www/application/' + sound.substr(1);
}
else {
return cordova.file.applicationDirectory.replace('file://', '') + sound.substr(1);
}
}
function playSound(sound) {
return new Media(
getMediaUrl(sound),
function (success) {
// success
},
function (err) {
// error
}
);
}
var test = playSound('/sounds/test.wav');
test.play();
NOTE: I included my audio files in the public folder i.e.
$PROJECT_ROOT/public/sounds/test.wav
and on Android, this file path is translated to
/android_asset/www/application/sounds/test.wav
这篇关于播放声音在Meteor-Cordova应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!