Audio files download and play within the app

hi,
i am fairly new with ionic. I am using ionic 3 with angular 2. My requirement is, i want to download an audio file with .mp3 extention inside my App (only should be inside the app and shouldn’t be accessible outside.)

after downloading the same i wanted to play it using ionic cordova streaming plugin. Can anyone help me with how to achieve this?

since i am able to download file but when trying to access the app crashes. and streaming works fine with a remote URL. but i think i am missing out something for download and so its not able to stream downloaded file.

Please provide me some guidance.

To do this, I use a combination of the File plugin (for managing files on the device), the File Transfer plugin (for downloading), and the Media plugin (for playing the downloaded files). Here’s a theoretical function that you could put into your provider for downloading and playing an audio file:

import { Injectable } from '@angular/core';
import { File, FileEntry } from '@ionic-native/file';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import { Media, MediaObject } from '@ionic-native/media';

@Injectable()
export class DownloadAndPlayProvider {

  constructor(public file: File, public txfr: FileTransfer, public audio: Media) {} 

  public download(url: string) {
    let ft: FileTransferObject = this.txfr.create();
    let fn = this.file.dataDirectory + url.substring(url.lastIndexOf('/') + 1);
    ft.download( url, fn ).then(
      (fe: FileEntry) => {
        let song: MediaObject = this.audio.create(fe.nativeURL);
        song.play();
      },
      err => {
        console.log(JSON.stringify(err));
      }
    );
  }
}
1 Like

hey @nst !
I want to do the same thing which you’re trying. Can you provide me the github link for this ? It will be very helpful for me.
Thank you :slight_smile: