【问题标题】:Scanning media files with ionic using the file cordova plugin使用文件cordova插件使用离子扫描媒体文件
【发布时间】:2016-12-26 03:04:12
【问题描述】:

我正在使用 Ionic 构建一个音乐播放器应用程序,那么如何使用文件 cordova 插件从文件存储中扫描音乐文件,以便用户能够选择要播放的歌曲。PS 我对 Ionic 还是很陌生。

【问题讨论】:

    标签: cordova ionic-framework mobile-application ngcordova apache-cordova


    【解决方案1】:

    您需要访问和播放音乐文件。有一些 Cordova 插件应该有助于访问本机内容。在您的项目中添加一个或多个插件后,我发现它们在 github 存储库中有非常有用的文档和示例。在 npm 页面中有指向这些内容的链接。

    访问文件有

    根据您定位的设备类型,以及您希望用户访问媒体文件的位置,您可能需要使用其中的一种或全部。

    播放文件:

    【讨论】:

    • 您知道有一个插件可以安全地在本地网络中提供媒体文件吗?我想访问计算机中的音乐文件。
    【解决方案2】:

    此代码将列出所有文件夹和文件:

        import { File } from "@ionic-native/file";
        import { Diagnostic } from "@ionic-native/diagnostic";
    
        constructor(
            ...
            public file: File,
            public diagnostic: Diagnostic
            ){
    
    
      // -------------------------
      listFileSys( which){
    
        // <which> chooses the file system - see switch below
        this.jcDebug += "\n" + "listFileSysSKOFLO(" + which + ")";
    
        let fileSysArray = [];
        let basePath = "";
    
        let tag = "unknown";
    
        // ************** RECURSE *************
        let jcListDir = (thisDir)=>{
    
          tag = "jcListDir: [" + thisDir + "]";
    
          this.file.listDir(basePath, thisDir)
          .then( (data)=>{
            tag = "listDir:" + thisDir;
            this.jcError += "\n" + tag + ":" + JSON.stringify( data);
            for( let ii = 0; ii < data.length; ii += 1){
              this.jcError += "\n" + data[ii].name + (data[ii].isDirectory? " [D]" : " [F]");
    
              let currentPath = thisDir;
              currentPath += (currentPath.length ? "/" : "");
              currentPath += data[ii].name;
              this.jcError += "\n" + "currentPath:" + currentPath;
    
              fileSysArray.push( currentPath);
    
              if( data[ii].isDirectory && ok2recurse){
                jcListDir( currentPath);         // RECURSE !!!
              }
            }
          }, (errData)=>{
            tag = "listDir";
            this.jcError += "\n" + tag + ":ERR:" + JSON.stringify( errData);
          });
        };
        // ************** RECURSE *************
    
        // ***********************
        let runListDir = ()=>{
            this.jcDebug += "\n" + "basePath:" + basePath;
    
            // !!! START listing from basePath !!!
            jcListDir( ".");
        }
    
        // ***********************
        switch( which)
        {
          case 1:
            this.diagnostic.getExternalSdCardDetails()
            .then( (data) => {
              this.jcDebug += "\n" + "sd:" + JSON.stringify( data);
              this.jcDebug += "\n" + "Number of cards: " + data.length;
              for( let ii = 0; ii < data.length; ii += 1){
                let thisElem = data[ii];
                if( thisElem.type.toLowerCase() === "application" && thisElem.canWrite){
                  basePath = thisElem.filePath;
                  break;
                }
              }
              if( !basePath){
                this.jcDebug += "\n" + "no SD card found";
                return;
              }
              runListDir();
            }, (errData)=>{
              tag = "getExternalSdCardDetails";
              this.jcError += "\n" + tag + ":ERR:" + JSON.stringify( errData);
            });
          break;
          case 2:
            basePath = this.file.externalDataDirectory;
            this.jcError += "\n" + "externalDataDirectory:" + basePath;
            runListDir();
            break;
          case 3:
            basePath = this.file.dataDirectory;
            this.jcError += "\n" + "dataDirectory:";
            runListDir();
            break;
          default:
            alert( "which???:" + which);
            return;
        }
    
        // wait for all to comnplete, then show
        // assume 1000 ms is adequate delay per promise
        let lastFileSysLen = -1
        let checkCount = 30; // max 30 * 1000 ms = 30 seconds
    
        // ************** RECURSE *************
          let checkComplete = () => {
          this.jcDebug += "\n" + "checkComplete " + checkCount + " [" + fileSysArray.length + "]";
          setTimeout( ()=>{
    
            // fileSysArr length stable?
            if( fileSysArray.length === lastFileSysLen){
              checkCount = 0;
            }
            lastFileSysLen = fileSysArray.length;
    
            checkCount -= 1;
            if( checkCount > 0){
              checkComplete();    // recurse
            } else {
    
              // STOP !!! and show FileSysArray
              this.jcInfo += "\n" + "show FileSysArray";
              this.jcInfo += "\n" + "fileSysArray.length = " + " [" + fileSysArray.length + "]";
    
              fileSysArray.sort();
    
              for( let elem of fileSysArray){
                this.jcInfo += "\n" + elem;
              }
            }
          }, 1000);
        };
        // ************** RECURSE *************
        checkComplete();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多