【问题标题】:Ionic media plugin does not play locally stored mp3 files on iOSIonic 媒体插件无法在 iOS 上播放本地存储的 mp3 文件
【发布时间】:2019-02-27 10:22:36
【问题描述】:

我正在尝试使用离子媒体插件制作 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();

整个应用程序崩溃而没有给我任何错误提示。 这个插件通常从 localPath 和 internetPath 播放 Android 上的文件。 在 iOS 上,它只能从互联网播放,但在播放本地文件时会崩溃。请帮忙。

【问题讨论】:

  • 您可以查看file.onSuccess.subscribe(() => console.log('Action is successful')); file.onError.subscribe(error => console.log('Error!', error)); 来查看您在本地播放时遇到的错误。
  • 我尝试同时使用两者。但他们只是不开火。尽管没有它们,音乐在 android 上也能正常播放。谢谢你顺便解决了我的问题。

标签: ios ionic3 ionic-native-media


【解决方案1】:

所以我找到了解决方案。它就在这里。 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 播放文件,但是您将面临另一个问题。您以后将无法通过此路径将其删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    相关资源
    最近更新 更多