使用媒体插件播放mp3的Phonegap应用程序不起作用 [英] Phonegap app playing mp3 using Media plugin does not work

查看:84
本文介绍了使用媒体插件播放mp3的Phonegap应用程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下路径在phonegap中开发了文件阅读器:

I've developed file reader in phonegap with following path:

var defaultPath ='file:///storage/sdcard0/';

var defaultPath = 'file:///storage/sdcard0/';

因此,我能够列出sdcard上的所有文件,并选择一个要加载音频文件的文件夹.所以我的音频文件列表如下:

So i'm able to list all file on sdcard and select one folder with audio files to load. So my list of audio files looks like:

i[1] = 'file:///storage/sdcard0/Download/audio/file.mp3';
i[2] = 'file:///storage/sdcard0/Download/audio/file1.mp3';

然后,我尝试使用Media插件(已安装)通过以下代码初始化歌曲:

Then I try to init song using Media plugin ( installed ) with following piece of code:

var media = new Media(i[1]);
media.play();

但是音频无法播放或什么也不做.但是,当我尝试使用PhoneGap APP对其进行测试时,一切正常,但是在进行构建并尝试从构建中测试应用时,这是行不通的.

But audio does not play or do nothing. But when I'm trying to test it using PhoneGap APP all works fine this just isn't work when I make build and try to test app from build.

推荐答案

我自己发现了这个问题. Android不允许通过从root /storage/sdcard0 /storage/sdcard1开始的路径读取本地存储或sdcard上的文件.因此,当我们要访问本地存储或sdcard时,必须通过file:///mnt/sdcard/来访问sdcard,而通过file:///mnt/storage/sdcard1/来访问电话来进行存储.

I found the issue by myself. Android does not let read files on local storage or on sdcard via path starting from root /storage/sdcard0 /storage/sdcard1. So when we would like to access local storage or sdcard we must access it via file:///mnt/sdcard/ for sdcard and file:///mnt/storage/sdcard1/ for phone storage.

如何从sdcard或手机存储中读取条目的小示例:

Small example how to read entries from sdcard or phone storage:

var path = 'file:///mnt/storage/sdcard1/'; 
window.resolveLocalFileSystemURL(path, function(entry){
    var reader = entry.createReader();
    reader.readEntries(showEntries);
});

function showEntries(entries){
   var entries = [];
   for(var i =0; i < entries.length; i++){
       var entry = entries[i];
       var data = {
           name: entry.name,
           path: entry.fullPath,
       };

       entries[i] = data;
   }

   console.log(JSON.stringify(entries));
}

希望这将与我遇到的问题有所帮助,因为这没有记录在phonegap中.

Hope that this will help with same issue as I had because this is not documented in phonegap.

这篇关于使用媒体插件播放mp3的Phonegap应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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