Сканирование медиафайлов с помощью ionic с помощью плагина file cordova

Я создаю приложение для музыкального проигрывателя с помощью Ionic, так как я могу сканировать музыкальные файлы из хранилища файлов с помощью плагина файловой кордовы, чтобы пользователи могли выбирать песни для воспроизведения. PS Я новичок в Ionic.


person Hammed Dapo Ajibade    schedule 19.08.2016    source источник


Ответы (2)


Вам нужно будет получить доступ к музыкальным файлам и воспроизвести их. Есть несколько плагинов Cordova, которые должны помочь в доступе к нативным материалам. После добавления одного или нескольких плагинов в ваш проект я обнаружил, что у них есть довольно полезная документация и примеры в репозиториях github. Ссылка на них есть на странице npm.

Для доступа к файлам есть

В зависимости от типа устройства, на которое вы ориентируетесь, и от того, где вы ожидаете, что пользователи будут получать доступ к медиафайлам, вам может понадобиться использовать один или все из них.

Для воспроизведения файлов:

person Jinw    schedule 19.08.2016
comment
Знаете ли вы о плагине, который позволяет безопасно обслуживать медиафайлы в локальной сети? Я хочу получить доступ к музыкальным файлам на компьютере. - person arjun; 24.10.2017

Этот код выведет список всех папок и файлов:

    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();
  }
person jcsubmit    schedule 14.11.2017