audio-recorder.component.ts 5.41 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;

25 26
  uploadUrl;
  uploadData;
liujiangnan's avatar
liujiangnan committed
27

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

  @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 ) {
58 59
    this.uploadUrl = (<any> window).courseware.uploadUrl();
    this.uploadData = (<any> window).courseware.uploadData();
liujiangnan's avatar
liujiangnan committed
60

61 62 63 64
    window['air'].getUploadCallback = (url, data) => {
      this.uploadUrl = url;
      this.uploadData = data;
    };
limingzhe's avatar
limingzhe committed
65 66

    this.setUploadUrl();
liujiangnan's avatar
liujiangnan committed
67 68
  }

limingzhe's avatar
limingzhe committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  setUploadUrl() {

    if (!this.uploadUrl) {

      this.uploadUrl = (<any> window).courseware.uploadUrl();
      this.uploadData = (<any> window).courseware.uploadData();

      setTimeout(() => {
        this.setUploadUrl();
      }, 500);
    }
  }


liujiangnan's avatar
liujiangnan committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  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
100

liujiangnan's avatar
liujiangnan committed
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    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
164
  onBtnRecord = () => {
liujiangnan's avatar
liujiangnan committed
165 166 167
  }

  // 切换模式
liujiaxin's avatar
liujiaxin committed
168
  onBtnSwitchType() {
liujiangnan's avatar
liujiangnan committed
169 170
  }
  onBtnClearAudio() {
171
    this.audioUrl = null;
liujiangnan's avatar
liujiangnan committed
172 173 174 175 176
    this.audioRemoved.emit();
  }

  onBtnDeleteAudio() {
    this.audioUrl = null;
limingzhe's avatar
limingzhe committed
177
    this.audioUploaded.emit({});
liujiangnan's avatar
liujiangnan committed
178 179 180
    this.audioRemoved.emit();
  }

liujiaxin's avatar
liujiaxin committed
181
  handleChange(info: { type: string, file: UploadFile, event: any }): void {
liujiangnan's avatar
liujiangnan committed
182 183 184 185 186 187 188
    switch (info.type) {
      case 'start':
        this.isUploading = true;
        this.progress = 0;
        break;
      case 'success':
        this.isUploading = false;
liujiangnan's avatar
liujiangnan committed
189
        this.uploadSuccess(info.file.response);
liujiangnan's avatar
liujiangnan committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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
214
  beforeUpload = (file: File) => {
liujiangnan's avatar
liujiangnan committed
215 216 217

    this.audioUrl = null;
    if (!this.checkSelectFile(file)) {
liujiangnan's avatar
liujiangnan committed
218
      return false;
liujiangnan's avatar
liujiangnan committed
219 220
    }
    this.isUploading = true;
liujiaxin's avatar
liujiaxin committed
221
    this.progress = 0;
liujiangnan's avatar
liujiangnan committed
222
  }
liujiaxin's avatar
liujiaxin committed
223 224 225 226
  uploadSuccess = (url) => {
    this.nzMessageService.info('Upload Success');
    this.isUploading = false;
    this.audioUrl = url;
liujiangnan's avatar
liujiangnan committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  }
  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
}