audio-recorder.component.ts 4.95 KB
Newer Older
liujiaxin's avatar
liujiaxin committed
1
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output, NgZone, OnChanges} from '@angular/core';
liujiangnan's avatar
liujiangnan committed
2 3
import {NzMessageService, NzNotificationService, UploadFile} from 'ng-zorro-antd';
import {HttpClient, HttpEvent, HttpEventType, HttpRequest} from '@angular/common/http';
liujiaxin's avatar
liujiaxin committed
4
import {environment} from '../../../environments/environment';
liujiangnan's avatar
liujiangnan committed
5 6 7 8 9 10 11

declare var Recorder;

@Component({
  selector: 'app-audio-recorder',
  templateUrl: './audio-recorder.component.html',
  styleUrls: ['./audio-recorder.component.scss']
liujiaxin's avatar
liujiaxin committed
12
})
liujiangnan's avatar
liujiangnan committed
13 14 15 16 17 18 19 20 21
export class AudioRecorderComponent implements OnInit, OnChanges, OnDestroy {
  _audioUrl: string;
  audio = new Audio();
  playIcon = 'play';
  isPlaying = false;
  isRecording = false;
  isUploading = false;
  type = Type.UPLOAD; // record | upload
  Type = Type;
22
  @Input()
liujiangnan's avatar
liujiangnan committed
23 24
  withRmBtn = false;

liujiaxin's avatar
liujiaxin committed
25 26
  uploadUrl = (window as any).courseware.uploadUrl();
  uploadData = (window as any).courseware.uploadData();
liujiangnan's avatar
liujiangnan committed
27

liujiangnan's avatar
liujiangnan committed
28 29 30 31 32 33 34 35
  @Input()
  needRemove = false;

  @Input()
  audioItem: any = null;

  @Input()
  set audioUrl(url) {
liujiaxin's avatar
liujiaxin committed
36
    this._audioUrl = url;
liujiangnan's avatar
liujiangnan committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    if (url) {
      this.audio.src = this._audioUrl;
      this.audio.load();
    }
    this.init();
  }

  get audioUrl() {
    return this._audioUrl;
  }

  @Output() audioUploaded = new EventEmitter();
  @Output() audioUploadFailure = new EventEmitter();
  @Output() audioRemoved = new EventEmitter();
  percent = 0;
  progress = 0;
  recorder: any;
  audioBlob: any;

liujiaxin's avatar
liujiaxin committed
56 57

  constructor( private nzMessageService: NzMessageService ) {
liujiangnan's avatar
liujiangnan committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

  }

  init() {
    this.playIcon = 'play';
    this.isPlaying = false;
    this.isRecording = false;
    this.isUploading = false;
    this.percent = 0;
    this.progress = 0;
    this.audioBlob = null;
  }
  ngOnChanges() {
    // if (!this.audioItem || !this.audioItem.type) {
    //   return;
    // }
    // this.beforeUpload(this.audioItem);
  }
  ngOnInit() {
liujiangnan's avatar
liujiangnan committed
77

liujiangnan's avatar
liujiangnan committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    this.audio.onplay = () => {
      this.onPlay();
    };
    this.audio.onpause = () => {
      this.onPause();
    };
    this.audio.ontimeupdate = (event) => {
      this.onTimeUpdate(event);
    };
    this.audio.onended = (event) => {
      this.onEnded();
    };
  }

  ngOnDestroy() {
    this.audio.pause();
    this.isPlaying = false;
    this.audio.remove();
    // if (this.recorder.worker) {
    //   this.recorder.worker.terminate();
    // }
  }

  progressText(percent) {
    return ``;
  }

  onPlay() {
    console.log('play');
    this.playIcon = 'pause';
    this.isPlaying = true;
  }

  onPause() {
    console.log('pause');
    this.playIcon = 'play';
    this.isPlaying = false;
  }

  onEnded() {
    console.log('on end');
    this.playIcon = 'play';
    this.percent = 0;
    this.isPlaying = false;
  }

  onTimeUpdate(event) {
    this.percent = Math.floor((this.audio.currentTime / this.audio.duration) * 100);
  }

  onBtnPlay() {
    if (this.isRecording) {
      this.nzMessageService.warning('In Recording');
      return;
    }
    if (this.isPlaying) {
      this.audio.pause();
    } else {
      this.audio.play();
    }
  }

  // 开始录音
liujiaxin's avatar
liujiaxin committed
141
  onBtnRecord = () => {
liujiangnan's avatar
liujiangnan committed
142 143 144
  }

  // 切换模式
liujiaxin's avatar
liujiaxin committed
145
  onBtnSwitchType() {
liujiangnan's avatar
liujiangnan committed
146 147
  }
  onBtnClearAudio() {
148
    this.audioUrl = null;
liujiangnan's avatar
liujiangnan committed
149 150 151 152 153 154 155 156
    this.audioRemoved.emit();
  }

  onBtnDeleteAudio() {
    this.audioUrl = null;
    this.audioRemoved.emit();
  }

liujiaxin's avatar
liujiaxin committed
157
  handleChange(info: { type: string, file: UploadFile, event: any }): void {
liujiangnan's avatar
liujiangnan committed
158 159 160 161 162 163 164
    switch (info.type) {
      case 'start':
        this.isUploading = true;
        this.progress = 0;
        break;
      case 'success':
        this.isUploading = false;
liujiangnan's avatar
liujiangnan committed
165
        this.uploadSuccess(info.file.response);
liujiangnan's avatar
liujiangnan committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        this.audioUploaded.emit(info.file.response);
        break;
      case 'progress':
        this.progress = parseInt(info.event.percent, 10);
        break;
    }
  }
  checkSelectFile(file: any) {
    if (!file) {
      return;
    }
    const isAudio = ['audio/mp3', 'audio/wav', 'audio/ogg'].includes(file.type);
    if (!isAudio) {
      this.nzMessageService.error('You can only upload Audio file ( mp3 | wav |ogg)');
      return;
    }
    const delta =  25;
    const isOverSize = (file.size / 1024 / 1024) < delta;
    if (!isOverSize) {
      this.nzMessageService.error(`audio file  must smaller than ${delta}MB!`);
      return false;
    }
    return true;
  }
liujiaxin's avatar
liujiaxin committed
190
  beforeUpload = (file: File) => {
liujiangnan's avatar
liujiangnan committed
191 192 193

    this.audioUrl = null;
    if (!this.checkSelectFile(file)) {
liujiangnan's avatar
liujiangnan committed
194
      return false;
liujiangnan's avatar
liujiangnan committed
195 196
    }
    this.isUploading = true;
liujiaxin's avatar
liujiaxin committed
197
    this.progress = 0;
liujiangnan's avatar
liujiangnan committed
198
  }
liujiaxin's avatar
liujiaxin committed
199 200 201 202
  uploadSuccess = (url) => {
    this.nzMessageService.info('Upload Success');
    this.isUploading = false;
    this.audioUrl = url;
liujiangnan's avatar
liujiangnan committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  }
  uploadFailure = (err, file) => {
    this.isUploading = false;
    if (err.name && err.name === 'cancel') {
      return;
    }
    console.log(err);
    this.nzMessageService.error('Upload Error ' + err.message);
    this.audioUploadFailure.emit(file);
  }
  doProgress = (p) => {
    if (p > 1) {
      p = 1;
    }
    if (p < 0) {
      p = 0;
    }
    // console.log(Math.floor(p * 100));
    this.progress =  Math.floor(p * 100);
  }

}

enum Type {
  RECORD = 1, UPLOAD
}