Ignore:
Timestamp:
2021-10-18T15:57:18+13:00 (3 years ago)
Author:
cstephen
Message:

Improve audio loading flow.
Load audio on word editor click.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/js/AudioPlaybackModule.js

    r35502 r35631  
    3434        }
    3535
    36         // setTimeout(poll, 33); // Slightly more than 30hz
    3736        requestAnimationFrame(poll);
    3837    })();
     
    4140/**
    4241 * Loads an audio file.
    43  * @param {Audio} player The audio player.
     42 * @param {HTMLAudioElement} player The audio player.
    4443 * @param {TranscriptionViewModel} transcription The name of the requested audio file.
    4544 * @param {LoadedAudio} current The currently loaded audio.
     
    7069        this.player = new Audio();
    7170        this.loadedAudio = new LoadedAudio(null, null);
     71        this.requestedPlaybackTime = 0;
    7272
    7373        pollAudioTime(this.player, store);
     
    7878    }
    7979
     80    static onCanPlayThrough() {
     81        AudioPlayback.player.removeEventListener("canplaythrough", AudioPlayback.onCanPlayThrough);
     82
     83        AudioPlayback.player.currentTime = AudioPlayback.requestedPlaybackTime;
     84        AudioPlayback.store.commit("playbackStateSetLength", { id: AudioPlayback.loadedAudio.id, time: AudioPlayback.player.duration });
     85    }
     86
    8087    /**
    81      * Plays a transcription's audio file.
    82      * @param {String} id The ID of the transcription to play audio for.
    83      * @param {Number} startTime The time into the audio at which to start playing. Leave negative to resume playback if paused.
     88     * Loads a transcription's audio file.
     89     * @param {String} id The ID of the transcription to load the audio of.
     90     * @param {Number} startTime The time into the audio to set the current playback time to. Leave negative to select the last value, or zero.
    8491     */
    85     static async play(id, startTime = -1) {
     92    static async load(id, startTime = -1) {
     93        this.player.addEventListener("canplaythrough", this.onCanPlayThrough);
     94
    8695        const playbackTimes = this.store.state.playbackState.playbackTimes;
    8796
    88         let playbackTime = startTime;
     97        this.requestedPlaybackTime = startTime;
    8998        if (playbackTimes.has(id) && startTime < 0) {
    90             playbackTime = playbackTimes.get(id);
     99            this.requestedPlaybackTime = playbackTimes.get(id);
    91100        }
    92101
     
    97106        }
    98107
    99         this.player.currentTime = playbackTime;
     108        this.player.currentTime = AudioPlayback.requestedPlaybackTime;
     109        this.store.commit("playbackStateSetLength", { id: id, time: this.player.duration });
     110    }
     111
     112    /**
     113     * Plays a transcription's audio file.
     114     * @param {String} id The ID of the transcription to play audio for.
     115     * @param {Number} startTime The time into the audio at which to start playing. Leave negative to resume playback if paused.
     116     */
     117    static async play(id, startTime = -1) {
     118        await this.load(id, startTime);
    100119        await this.player.play();
    101 
    102         this.store.commit("playbackStateSetLength", { id: id, time: this.player.duration });
    103120        this.store.commit("playbackStateSetIsPlaying", true);
    104121    }
Note: See TracChangeset for help on using the changeset viewer.