我有一个在Android上运行良好的Ionic 2应用程序。 现在我需要构建 IOS 版本。该应用程序可以从SoundCloud流式传输和下载音频。
当用户单击下载音频时,音频存储在Cordova file plugin
提供的应用程序数据目录中。
我用来存储和检索数据的逻辑并不重要,因为在Android中它可以工作。问题是我想在IOS中恢复和播放音频。
这是我创建MediaObject
并在稍后播放的代码部分,暂停它:
var pathalone: string = '';
if (this.platform.is('ios')) {
pathalone = this.audio.filePath.replace(/^file:///, '');
console.log("Pathalone: " + pathalone);
this.mediaPlayer = this.media.create(pathalone, onStatusUpdate, onSuccess, onError);
} else {
pathalone = this.audio.filePath.substring(8);
this.mediaPlayer = this.media.create(pathalone, onStatusUpdate, onSuccess, onError);
}
setTimeout(() => {
if (this.mediaPlayer) {
this.togglePlayPause();
this.updateTrackTime();
}
}, 1000);
在媒体插件文档中,如果IOS仍有问题,请先创建文件。所以我尝试了,但我仍然有同样的问题:
this.file.createFile(cordova.file.dataDirectory, this.audio.fileName, true).then(() => {
pathalone = this.audio.filePath.replace(/^file:///, '');
console.log("Pathalone: " + pathalone);
this.mediaPlayer = this.media.create(pathalone, onStatusUpdate, onSuccess, onError);
});
在控制台中,我得到这些日志:
console.log: Pathalone:
/Users/ivan/Library/Developer/CoreSimulator/Devices/0D75C1A9-591A-4112-BBE4-AB901953A38F/data/Containers/Data/Application/509D1136-86F6-4C9B-84B5-8E0D0D203DAC/Library/NoCloud/311409823.mp3
[14:07:48] console.log: Cannot use audio file from resource
'/Users/ivan/Library/Developer/CoreSimulator/Devices/0D75C1A9-591A-4112-BBE4-AB901953A38F/data/Containers/Data/Application/509D1136-86F6-4C9B-84B5-8E0D0D203DAC/Library/NoCloud/311409823.mp3'
也许它发生在模拟器上,但不会在真实设备上发生?我无法在iPhone上进行测试,因为我没有:/
PD:一个奇怪的事实是,SoundCloud api在制作GET到其API下载音频时会返回重定向。 响应的状态为 301,因此我使用response.headers.Location
来处理重定向,在那里我可以执行下载。 而且我注意到在IOS中它从不执行重定向,它直接以"积极"的方式进行,控制台显示"已下载"。也许它从未真正下载过...
在创建记录器媒体对象之前,确实需要创建一个文件。即使使用iOS和Android上的模拟器也可以录制。下面说明了如何使用Ionic2+完成此操作
首先,您需要导入以下内容
import { NavController, Platform } from 'ionic-angular';
import { Media, MediaObject } from '@ionic-native/media';
import { File } from '@ionic-native/file';
确保在构造函数中注入了导入,如下所示
constructor(public navCtrl: NavController, private media: Media, private file: File, public platform: Platform) {
// some more code here
}
在构造函数之前声明所需的变量,如下所示
filePath: string;
fileName: string;
audio: MediaObject;
开始录制的代码如下
startRecord() {
if (this.platform.is('ios')) {
this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.3gp';
this.filePath = this.file.dataDirectory;
this.file.createFile(this.filePath, this.fileName, true).then(() => {
this.audio = this.media.create(this.filePath.replace(/^file:///, '') + this.fileName);
this.audio.startRecord();
});
} else if (this.platform.is('android')) {
this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.3gp';
this.filePath = this.file.externalDataDirectory;
this.file.createFile(this.filePath, this.fileName, true).then(() => {
this.audio = this.media.create(this.filePath.replace(/^file:///, '') + this.fileName);
this.audio.startRecord();
});
}
}
请注意在createFile
中使用路径名"this.filePath"和media.create
之间的差异。
停止录制的代码如下
stopRecord() {
this.audio.stopRecord();
}