audio-recorder.component.ts 5.1 KB
Newer Older
李维's avatar
李维 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';
李维's avatar
李维 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']
李维's avatar
李维 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;
李维's avatar
李维 committed
22
  @Input()
liujiangnan's avatar
liujiangnan committed
23 24
  withRmBtn = false;

李维's avatar
李维 committed
25 26
  uploadUrl;
  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) {
李维's avatar
李维 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;

李维's avatar
李维 committed
56 57

  constructor( private nzMessageService: NzMessageService ) {
李维's avatar
李维 committed
58 59
    this.uploadUrl = (<any> window).courseware.uploadUrl();
    this.uploadData = (<any> window).courseware.uploadData();
liujiaxin's avatar
liujiaxin committed
60

李维's avatar
李维 committed
61 62 63 64
    window['air'].getUploadCallback = (url, data) => {
      this.uploadUrl = url;
      this.uploadData = data;
    };
liujiangnan's avatar
liujiangnan committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }

  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
83

liujiangnan's avatar
liujiangnan committed
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 141 142 143 144 145 146
    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();
    }
  }

  // 开始录音
李维's avatar
李维 committed
147
  onBtnRecord = () => {
liujiangnan's avatar
liujiangnan committed
148 149 150
  }

  // 切换模式
李维's avatar
李维 committed
151
  onBtnSwitchType() {
liujiangnan's avatar
liujiangnan committed
152 153
  }
  onBtnClearAudio() {
李维's avatar
李维 committed
154
    this.audioUrl = null;
liujiangnan's avatar
liujiangnan committed
155 156 157 158 159 160 161 162
    this.audioRemoved.emit();
  }

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

李维's avatar
李维 committed
163
  handleChange(info: { type: string, file: UploadFile, event: any }): void {
liujiangnan's avatar
liujiangnan committed
164 165 166 167 168 169 170
    switch (info.type) {
      case 'start':
        this.isUploading = true;
        this.progress = 0;
        break;
      case 'success':
        this.isUploading = false;
liujiangnan's avatar
liujiangnan committed
171
        this.uploadSuccess(info.file.response);
liujiangnan's avatar
liujiangnan committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        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;
  }
李维's avatar
李维 committed
196 197
  beforeUpload = (file: File) => {

liujiangnan's avatar
liujiangnan committed
198 199
    this.audioUrl = null;
    if (!this.checkSelectFile(file)) {
liujiangnan's avatar
liujiangnan committed
200
      return false;
liujiangnan's avatar
liujiangnan committed
201 202
    }
    this.isUploading = true;
李维's avatar
李维 committed
203 204 205 206 207 208
    this.progress = 0;
  }
  uploadSuccess = (url) => {
    this.nzMessageService.info('Upload Success');
    this.isUploading = false;
    this.audioUrl = url;
liujiangnan's avatar
liujiangnan committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  }
  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
}