我们如何将音频文件发送到服务器

时间:2018-06-13 15:19:38

标签: angular webrtc recordrtc

我正在使用以下文档来录制录音。我已将代码集成到本地,并且运行正常。

https://medium.com/@SumanthShankar/integrate-recordrtc-with-angular-2-typescript-942c9c4ca93f

HTML文件:

              <div class="audio-voice">
                <video #video class="video">
                </video>
              </div>
              <div class="all-audio-btn">
                <button class="btn btn-primary" (click)="backRecording()"> Back </button> &nbsp;
                <button class="btn btn-primary" (click)="startRecording()"> Record </button> &nbsp;
                <button class="btn btn-primary" (click) ="stopRecording()"> Stop</button>&nbsp;
                <button class="btn btn-primary" (click)="download()"> Download</button>&nbsp;
                <button class="btn btn-primary" (click)="saveRecording()"> Save</button>
              </div>

组件文件

 stopRecording() {
    let recordRTC = this.recordRTC;
    recordRTC.stopRecording(this.processVideo.bind(this));
    let stream = this.stream;
    stream.getAudioTracks().forEach(track => track.stop());
    stream.getVideoTracks().forEach(track => track.stop());
    let video: HTMLAudioElement = this.video.nativeElement;
    video.src = window.URL.createObjectURL(stream);
  }

以上代码是停止录制。

现在我想将录制的音频文件发送到服务器。但是无法发送数据。

我使用下面的代码发送数据:

 saveRecording(){
     let blob = this.recordRTC instanceof Blob ? this.recordRTC : this.recordRTC.blob;
     let fileType = blob.type.split('/')[0] || 'audio';
     let fileName = (Math.random() * 1000).toString().replace('.', '');
     console.log(fileName);

      if (fileType === 'audio') {
        fileName += '.' + (!!navigator.mozGetUserMedia ? 'ogg' : 'wav');
       } else {
        fileName += '.webm';
      }
      let formData: FormData = new FormData();
      // create FormData
      formData.append(fileType + '-filename', fileName);
      formData.append(fileType + '-blob', blob);

      this.dashboard.saveRecording(formData).subscribe(
        data => this.saveRecordingSuccess(data),
        error => this.saveRecordingFail(error)
      );

  }

0 个答案:

没有答案