【问题标题】:Ionic 4 : Recording Audio and Send to Server (File has been corrupted)Ionic 4:录制音频并发送到服务器(文件已损坏)
【发布时间】:2019-08-20 04:19:30
【问题描述】:

我想在移动应用程序(iOS 和 Android)中录制音频文件并作为 ionic 4 中的 formData 传输到服务器。我使用“cordova-plugin-media”通过以下逻辑捕获音频

  if (this.platform.is('ios')) {
      this.filePaths = this.file.documentsDirectory;
      this.fileExtension = '.m4a';
    } else if (this.platform.is('android')) {
      this.filePaths = this.file.externalDataDirectory;
      this.fileExtension = '.3gp';
    }

  this.fileName = 'recording'+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+this.fileExtension;

    if(this.filePaths) {
this.file.createFile(this.filePaths,this.fileName,true).then((entry:FileEntry)=> {
        this.audio = this.media.create(entry.toInternalURL());
        this.audio.startRecord();
      });
   }

我什至尝试过不使用“文件创建”直接创建媒体

我可以录制和播放音频,但如果我尝试发送此文件 使用以下逻辑到服务器它不会正确发送(损坏的数据) 并且 Web 应用程序也无法播放 .m4a 扩展名

.

如果我在代码中做错了什么,请纠正我

上传逻辑:

let formData:FormData = new FormData();
 formData.append('recordID' , feedbackID);
  that.file.readAsDataURL(filePath,file.name).then((data)=>{
       const audioBlob = new Blob([data], { type: file.type });
       formData.append('files', audioBlob, file.name);
       that.uploadFormData(formData,feedbackID); //POST Logics - 
     })

;

我已经按照 Ankush 的建议使用了灵魂,它工作正常。 使用 readAsArrayBuffer 而不是 readAsDataURL。 .m4a 格式同时支持 ios 和 android。我也可以 从 Web 应用程序下载相同的文件。

【问题讨论】:

  • 在 android 3gp 扩展用于视频文件

标签: angular6 ionic4 form-data cordova-media-plugin


【解决方案1】:

我正在使用以下代码将图像上传到服务器。我假设在此代码中只需要进行一些修改即可传输媒体而不是图像文件。

private uploadPicture(imagePath: string, apiUrl: string): Observable<ApiResponse<ImageUploadResponseModel>> {

    return this.convertFileFromFilePathToBlob(imagePath).pipe(
      switchMap(item => this.convertBlobToFormData(item)),
      switchMap(formData => this.postImageToServer(formData, apiUrl))
    );
}

上面代码中用到的Rest函数:

private postImageToServer(formData: FormData, apiUrl: string): Observable<ApiResponse<ImageUploadResponseModel>> {
    const requestHeaders = new HttpHeaders({ enctype: 'multipart/form-data' });
    return this.http.post<ApiResponse<ImageUploadResponseModel>>(apiUrl, formData, { headers: requestHeaders });
}

private convertBlobToFormData(blob: Blob): Observable<FormData> {
    return new Observable<FormData>(subscriber => {
      // A Blob() is almost a File() - it's just missing the two properties below which we will add
      // tslint:disable-next-line: no-string-literal
      blob['lastModifiedDate'] = new Date();
      // tslint:disable-next-line: no-string-literal
      blob['name'] = 'sample.jpeg';
      const formData = new FormData();
      formData.append('file', blob as Blob, 'sample.jpeg');
      subscriber.next(formData);
      subscriber.complete();
    });
}

private convertFileFromFilePathToBlob(imagePath: string): Observable<Blob> {
    return new Observable<Blob>(subscriber => {
      const directoryPath = imagePath.substr(0, imagePath.lastIndexOf('/'));
      let fileName = imagePath.split('/').pop();
      fileName = fileName.split('?')[0];
      this.file.readAsArrayBuffer(directoryPath, fileName).then(fileEntry => {
          const imgBlob: any = new Blob([fileEntry], { type: 'image/jpeg' });
          imgBlob.name = 'sample.jpeg';
          subscriber.next(imgBlob);
          subscriber.complete();
      }, () => {
        subscriber.error('Some error occured while reading image from the filepath.');
      });
    });
}

【讨论】:

  • 谢谢。它工作正常。我使用了 readAsArrayBuffer 而不是 readAsDataURL。
猜你喜欢
  • 2019-10-04
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多