xcode iOS Phonegap 媒体对象 - 如何访问 WWW 文件夹中的资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11175489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Phonegap Media Object - How do I access a resource in the WWW folder?
提问by Braydon Batungbacal
I'm developing an application with phonegap, and I have a sound file I want to play that's in a path like so www/Sounds/sound.mp3, and I'm trying to access this file using the Media object of Phonegap in order to play it.
我正在开发一个带有 phonegap 的应用程序,我有一个我想播放的声音文件,它位于像 www/Sounds/sound.mp3 这样的路径中,我正在尝试使用 Phonegap 的 Media 对象依次访问该文件玩它。
I cannot figure out the path to access this sound file within a javascript file that uses the Media object? I've tried paths like, file:///www/Sounds/sound.mp3, relative paths, etc and I cannot access it. I keep getting the following error in xcode 4
我无法在使用 Media 对象的 javascript 文件中找出访问此声音文件的路径?我试过像 file:///www/Sounds/sound.mp3、相对路径等路径,但我无法访问它。我在 xcode 4 中不断收到以下错误
Will attempt to use file resource 'file:///www/Sounds/sound.mp3'
Unknown resource 'file:///www/Sounds/sound.mp3'
What path do I need to use to access the file? Or do I need to copy the sound file out of my www directory and into my Resources folder and access it there?
我需要使用什么路径来访问文件?或者我是否需要将声音文件从我的 www 目录复制到我的资源文件夹中并在那里访问它?
My WWW folder is referenced, not sure if that makes a difference.
引用了我的 WWW 文件夹,不确定这是否有所不同。
采纳答案by Alon Gubkin
As of Cordova 3.3+, the 2 other answers aren't correct. It should work even if you just pass it a file name.
从 Cordova 3.3+ 开始,其他 2 个答案不正确。即使您只是将文件名传递给它,它也应该可以工作。
But, it will only work in iOS if the file extension is .wav
.
但是,如果文件扩展名为.wav
.
This will work:
这将起作用:
var media = new Media('recording.wav', ...);
This won't:
这不会:
var media = new Media('recording.mp3', ...);
回答by Ben X Tan
Heres a modified version of getPhoneGapPath that works for me on both iOS and Android. It is also not restricted to only working on files that have a filename that is 10 characters long :)
这是 getPhoneGapPath 的修改版本,适用于 iOS 和 Android。它也不限于仅处理文件名长度为 10 个字符的文件:)
getPhoneGapPath: function () {
'use strict';
var path = window.location.pathname;
var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
return phoneGapPath;
}
var resource = getPhoneGapPath() + 'audio/audio.mp3';
getPhoneGapPath() will return:
getPhoneGapPath() 将返回:
iOS:/var/mobile/Applications/{GUID}/{appName}/www/
iOS:/var/mobile/Applications/{GUID}/{appName}/www/
Android:/android_asset/www/
安卓:/android_asset/www/
回答by ThinkingStiff
Use window.location.pathname
to get the path of your application. It will look something like this on iPhone:
使用window.location.pathname
让您的应用程序的路径。它在 iPhone 上看起来像这样:
/var/mobile/Applications/{GUID}/{appname}.app/www/index.html
/var/mobile/Applications/{GUID}/{appname}.app/www/index.html
And this on Android:
这在Android上:
/android_asset/www/index.html
/android_asset/www/index.html
Strip off the /index.html
, prepend file://
, and append your relative path Sounds/sound.mp3
.
去除/index.html
、 前置file://
和 附加您的相对路径Sounds/sound.mp3
。
Here's something to get you started:
这里有一些东西可以让你开始:
Demo: http://jsfiddle.net/ThinkingStiff/chNVY/
演示:http: //jsfiddle.net/ThinkingStiff/chNVY/
Code:
代码:
function getPhoneGapPath() {
var path = window.location.pathname;
path = path.substr( 0, path.length - 10 );
return 'file://' + path;
};
var resource = getPhoneGapPath() + 'Sounds/sound.mps';
回答by slypete
The best answeris outdated. You are now able to play wav
, mp3
, and caf
formats on iOS using this "relative path" method:
的最佳答案是过时的。您现在可以使用这种“相对路径”方法在 iOS 上播放wav
、mp3
和caf
格式:
var media = new Media('beep.wav', ...);
Note that the above code requires the sound file to be in the root of your www/
directory -- in this example at www/beep.wav
. If you want to put them in a subdiretory like www/sounds/
, you'll need to specify it using a path relative to your www/
directory. For example, this also works now:
请注意,上面的代码要求声音文件位于www/
目录的根目录中——在本例中为www/beep.wav
. 如果要将它们放在像 那样的子目录中www/sounds/
,则需要使用相对于www/
目录的路径来指定它。例如,这现在也有效:
var media = new Media('sounds/beep.wav', ...);
for the sound file at www/sounds/beep.wav
.
对于www/sounds/beep.wav
.
回答by Manuel Mumenthaler
I had the same problem and all the answers about file paths alone didn't work for me using the Phone Gap Developer app. The problem was with the Developer app not pushing the files physically onto the device, preventing me from accessing them in any way.
我遇到了同样的问题,并且使用Phone Gap Developer 应用程序,所有关于文件路径的答案都对我不起作用。问题在于开发人员应用程序没有将文件物理推送到设备上,从而阻止我以任何方式访问它们。
The Media plugin worked as intended using
媒体插件按预期使用
$ phonegap run android
$ phonegap run ios
$ phonegap运行android
$ phonegap 运行 ios
with this js code:
用这个js代码:
function getPhoneGapPath() {
var path = window.location.pathname;
var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
return phoneGapPath;
}
var sound = new Media(getPhoneGapPath()+"assets/sounds/sound.mp3"
//...
sound.play();
This returns an error on Developer app:
这会在开发者应用上返回一个错误:
'Cannot use audio file from source var/mobile/Containers/data/Application/{GUID}/Library/NoCloud/phonegapdevapp/www/assets/sounds/sound.mp3'
“无法使用来自源 var/mobile/Containers/data/Application/{GUID}/Library/NoCloud/ phonegapdevapp/www/assets/sounds/sound.mp3 的音频文件”
but works if built from the console.
但如果从控制台构建,则有效。
Details can be found here: https://forums.adobe.com/thread/2135718
可在此处找到详细信息:https: //forums.adobe.com/thread/2135718
回答by Agamemnus
Better try this to take into account any HTML5 History API code that might add in history entries with directories ("/"):
最好尝试这样做以考虑可能添加到带有目录 ("/") 的历史条目中的任何 HTML5 History API 代码:
function get_phonegap_path () {
var path = window.location.pathname
return path.slice(0, path.indexOf("/www/") + 5)
}