我正在尝试制作带有 ionic 媒体插件的 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 上,它只能从互联网上播放,但在播放本地文件时会崩溃。请帮忙。
最佳答案
所以我找到了解决方案。就在这里。 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 播放文件,但随后你将面临另一个问题。您稍后将无法通过此路径删除它。
关于ios - Ionic 媒体插件无法在 iOS 上播放本地存储的 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903165/