Advertisement
ganryu

Untitled

Jan 16th, 2021
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mediaStatus = {
  2.   playing: 'playing',
  3.   paused: 'paused',
  4.   ended: 'ended'
  5. }
  6.  
  7. const congratsSound = {
  8.   file: 'Niveles.mp3',
  9.   media: null,
  10.   started: null,
  11.   playFor: 0,
  12.   status: mediaStatus.paused,
  13.   create() {
  14.     this.media = createAudio(this.file);
  15.   },
  16.   play(seconds) {
  17.     this.media.play();
  18.     this.status = mediaStatus.playing;
  19.     this.started = millis();
  20.     this.playFor += seconds * 1000;
  21.   },
  22.   reset() {
  23.     this.media.stop();
  24.     this.status = mediaStatus.paused;
  25.     this.started = null;
  26.   },
  27.   update() {
  28.     if (this.status === mediaStatus.paused || this.started === null) {
  29.       return;
  30.     }
  31.    
  32.     const duration = this.media.duration() * 1000;
  33.    
  34.     if (millis() >= this.started + duration) {
  35.       this.status = mediaStatus.ended;
  36.       this.started = null;
  37.       return;
  38.     }
  39.    
  40.     if (millis() >= this.started + this.playFor) {
  41.       this.media.stop();
  42.       this.status = mediaStatus.paused;
  43.       this.started = null;      
  44.     }
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement