How do I Retrieve and Save Internal Audio (MP3) Locations?


#1

I would like to know how to retrieve and save the internal locations of all of the mp3 files in the ‘Music’ folder of a phone. Currently I wait until the platform is ready, then I use the Cordova File Plugin to search for and print all the mp3 files, and finally I use the Cordova Media Plugin to save location of the last processed mp3 file.

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { NavController } from 'ionic-angular';
import { File, MediaPlugin } from 'ionic-native';

declare var cordova: any;

@Component({
  selector: 'page-page1',
  templateUrl: 'page1.html'
})
export class Page1 {

  playing: Boolean = false;
  file: any;

  constructor(public navCtrl: NavController, public platform: Platform) {

    platform.ready().then(() => {

      const fs:string = cordova.file.externalRootDirectory;

      // Make sure the phone contains a 'Music' directory
      File.checkDir(fs, 'Music').then(_ => console.log('yay')).catch(err => console.log('boooh'));
      // Grab a list of all the files inside of the 'Music' directory and filter it until only mp3 files remain
      File.listDir(fs, 'Music').then(entries => entries.filter( value => {
        return value.name.substr(value.name.lastIndexOf('.')+1) == 'mp3';
      }).forEach(entry => {
        console.log(entry.nativeURL);
        this.file = new MediaPlugin(entry.nativeURL);
      }));
    });
  }

  public playAudio(): void {

    if(this.playing) {
      console.log("It should stop playing now!");
      this.playing = false;
      this.file.pause();
    }
    else {
      console.log("It should start playing now!")
      this.playing = true;
      try {
        this.file.play();
      } catch (error) {
        console.log(error);
      }
    }
  }
}

I have tried creating a string array called ‘files’ and pushing each internal location into the array, I can’t seem to figure out how to do this within a promise. Any help or advise is appreciated, thank you for your time.