Hook Banner
Hook Banner

Reputation: 11

Ionic media plugin does not play locally stored mp3 files on iOS

I'm trying to make a mp3 player with ionic media plugin. First I download song on local storage via path

let filePath;

if(this.platform.is('android'))
{
    filePath = this.file.externalDataDirectory;
}
else 
{
  filePath = this.file.documentsDirectory;
}

filePath = filePath +'810.mp3';

after that we get such path

file:///var/mobile/Containers/Data/Application/5DC6D2D0-6336-4C6E-A39E-5D5FD7D8AF7B/Library/Cloud/810.mp3

So for example we have such an object

track = {

    'name':'Name of Song',
    'filePath': filePath
}

I create

const file:MediaObject = this.media.create(track.filePath);

and when time comes to play

file.play();

Whole app crashes without giving me a hint of error. This plugin normally plays files on Android from both localPath and internetPath. On iOS it plays only from internet but crashes when plays local files. Please help.

Upvotes: 0

Views: 1155

Answers (1)

Hook Banner
Hook Banner

Reputation: 11

So I found the solution. It was here. https://forum.ionicframework.com/t/ios-9-cant-play-audio-video-files-that-are-downloaded-to-device/37580

For example.

track = {    
    'name':'Name of Song',
    'filePath': '';
}

if(this.platform.is('android'))
{
  track.filePath = this.file.externalDataDirectory;
}
else 
{
  track.filePath = this.file.documentsDirectory;
}

track.filePath = track.filePath +'810.mp3';

const transfer = this.transfer.create();

let url = "http://someLink.com/someFileName.mp3";

transfer.download(url, track.filePath).then((entry)=>{

// **code here will be executed when track is downloaded**
// and only for ios local file system we need to create a special link to this file 
// so that  cordova media plugin could read this file and if necessary to delete it 
// with cordova file plugin by this link also.

if(this.platform.is('ios'))
   this.file.resolveLocalFilesystemUrl(track.filePath).then((entry)=>{
     track.filePath = entry.toInternalURL;
   });

Actually there was another way to make iOS play file by removinh file:// from beging of track.filePath but then you will face with another issue. You won't be able do delete it by this path later.

Upvotes: 1

Related Questions