【问题标题】:How to get Path for local mp3 file from www in Phonegap IOS?如何在 Phonegap IOS 中从 www 获取本地 mp3 文件的路径?
【发布时间】:2015-02-05 08:32:50
【问题描述】:

我在我的 phonegap 应用程序中使用 phonegap 音频插件。 我有一些音频文件存储在 www/audio 文件夹中。

对于 android,我使用 "file:///android_asset/www/audio" 来读取本地文件。

对于 IOS,我需要帮助。

谢谢

【问题讨论】:

标签: ios cordova phonegap-plugins


【解决方案1】:

终于明白了 参考:link

为了获取路径我使用了这个函数

function getPhoneGapPath() {
   var path = window.location.pathname;
   path = path.substr( path, path.length - 10 );
   return path;
};

用法:

对于IOS

var snd = new Media(getPhoneGapPath() + 'test.mp3');

对于 Android前置 'file://'

var snd = new Media('file://' + getPhoneGapPath() + 'test.mp3' );

【讨论】:

    【解决方案2】:

    如果从 'index.html' 以外的文件调用你会遇到麻烦,因为要修剪的部分可能不是 10 个字符。

    试试这个:

    // Get absolute file path on the device.
    function getPhoneGapPath() {
        var path = window.location.pathname;
        var sizefilename = path.length - (path.lastIndexOf("/")+1);
        path = path.substr( path, path.length - sizefilename );
        return path;
    };
    

    【讨论】:

    • 这对于处理意外的路径大小非常有用。与减去固定长度 10 相比,这是一种更好的获取路径的方法。
    • 如果应用名称包含在 window-location-pathname 中编码为 %NN 的 unicode 字符,您必须使用 path = decodeURIComponent(path) 将其转回 unicode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    相关资源
    最近更新 更多