audio-recorder.component.ts 5.01 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 22 23
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;
  withRmBtn = false;

李维's avatar
李维 committed
24 25
  uploadUrl = (<any>window).courseware.uploadUrl();
  uploadData = (<any>window).courseware.uploadData();
liujiangnan's avatar
liujiangnan committed
26

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

  @Input()
  audioItem: any = null;

  @Input()
  set audioUrl(url) {
李维's avatar
李维 committed
35
    this._audioUrl = url
liujiangnan's avatar
liujiangnan committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    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
55 56
  
  constructor( private nzMessageService: NzMessageService ) { 
liujiaxin's avatar
liujiaxin committed
57

liujiangnan's avatar
liujiangnan committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  }

  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
76

liujiangnan's avatar
liujiangnan committed
77 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
    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
140
  onBtnRecord = () => { 
liujiangnan's avatar
liujiangnan committed
141 142 143
  }

  // 切换模式
李维's avatar
李维 committed
144
  onBtnSwitchType() { 
liujiangnan's avatar
liujiangnan committed
145 146 147 148 149 150 151 152 153 154
  }
  onBtnClearAudio() {
    this.audioRemoved.emit();
  }

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

李维's avatar
李维 committed
155
  handleChange(info: { type: string, file: UploadFile, event: any }): void {  
liujiangnan's avatar
liujiangnan committed
156 157 158 159 160 161 162
    switch (info.type) {
      case 'start':
        this.isUploading = true;
        this.progress = 0;
        break;
      case 'success':
        this.isUploading = false;
liujiangnan's avatar
liujiangnan committed
163
        this.uploadSuccess(info.file.response);
liujiangnan's avatar
liujiangnan committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        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
188
  beforeUpload = (file: File) => { 
liujiangnan's avatar
liujiangnan committed
189 190
    this.audioUrl = null;
    if (!this.checkSelectFile(file)) {
liujiangnan's avatar
liujiangnan committed
191
      return false;
liujiangnan's avatar
liujiangnan committed
192 193
    }
    this.isUploading = true;
李维's avatar
李维 committed
194 195 196 197 198 199 200 201 202 203
    this.progress = 0; 
  }
  uploadSuccess = (url) => {  
    this.nzMessageService.info('Upload Success'); 
    this.isUploading = false; 
    if(typeof url == "string"){
      this.audioUrl = url
    }else{
      this.audioUrl = url.url  
    }
liujiangnan's avatar
liujiangnan committed
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 229
  }
  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
}