我正试图用离子媒体插件制作一个mp3播放器。 首先,我通过路径
将歌曲下载到本地存储中let filePath;
if(this.platform.is('android'))
{
filePath = this.file.externalDataDirectory;
}
else
{
filePath = this.file.documentsDirectory;
}
filePath = filePath +'810.mp3';
之后,我们获得了这样的路径
file:///var/mobile/Containers/Data/Application/5DC6D2D0-6336-4C6E-A39E-5D5FD7D8AF7B/Library/Cloud/810.mp3
例如,我们有一个这样的对象
track = {
'name':'Name of Song',
'filePath': filePath
}
我创建
const file:MediaObject = this.media.create(track.filePath);
还有时间玩
file.play();
整个应用程序崩溃而没有给我提示错误。 此插件通常在Android上从localPath和internetPath播放文件。 在iOS上,它只能从互联网播放,但是在播放本地文件时会崩溃。请帮忙。
答案 0 :(得分:0)
所以我找到了解决方案。在这里 https://forum.ionicframework.com/t/ios-9-cant-play-audio-video-files-that-are-downloaded-to-device/37580
例如。
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;
});
实际上,还有另一种方法可以通过从track.filePath的开头删除 file:// 来使iOS播放文件,但是您将面临另一个问题。您以后将无法通过此路径将其删除。