我有一个在
Android上运行良好的Ionic 2应用程序.
现在我需要构建IOS版本.该应用程序可以从SoundCloud流式传输和下载音频.
现在我需要构建IOS版本.该应用程序可以从SoundCloud流式传输和下载音频.
当用户单击下载音频时,音频存储在Cordova文件插件提供的应用程序数据目录中.
我用来存储和检索数据的逻辑并不重要,因为在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,onError); } setTimeout(() => { if (this.mediaPlayer) { this.togglePlayPause(); this.updateTrackTime(); } },1000);
在Media plugin docs中说如果在IOS上仍有问题,请首先创建该文件.所以我尝试过,但我仍然遇到同样的问题:
this.file.createFile(cordova.file.dataDirectory,this.audio.fileName,true).then(() => { pathalone = this.audio.filePath.replace(/^file:\/\//,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:
一个奇怪的事实是,当对其api进行GET下载音频时,SoundCloud 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); this.audio.startRecord(); }); } }
请注意在createFile和media.create中使用路径名“this.filePath”之间的区别.
停止录制的代码如下
stopRecord() { this.audio.stopRecord(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。