Code coverage report for src/mediaelement.js

Statements: 4.41% (3 / 68)      Branches: 0% (0 / 29)      Functions: 0% (0 / 23)      Lines: 4.41% (3 / 68)      Ignored: none     

All files » src/ » mediaelement.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164    1   1                                                                                                                                                                                                                                                                                                                           1  
'use strict';
 
WaveSurfer.MediaElement = Object.create(WaveSurfer.WebAudio);
 
WaveSurfer.util.extend(WaveSurfer.MediaElement, {
    init: function (params) {
        this.params = params;
 
        // Dummy media to catch errors
        this.media = {
            currentTime: 0,
            duration: 0,
            paused: true,
            playbackRate: 1,
            play: function () {},
            pause: function () {}
        };
 
        this.mediaType = params.mediaType.toLowerCase();
        this.elementPosition = params.elementPosition;
    },
 
    load: function (url, container, peaks) {
        var my = this;
 
        var media = document.createElement(this.mediaType);
        media.controls = this.params.mediaControls;
        media.autoplay = false;
        media.preload = 'auto';
        media.src = url;
        media.style.width = '100%';
 
        media.addEventListener('error', function () {
            my.fireEvent('error', 'Error loading media element');
        });
 
        media.addEventListener('canplay', function () {
            my.fireEvent('canplay');
        });
 
        media.addEventListener('ended', function () {
            my.fireEvent('finish');
        });
 
        media.addEventListener('timeupdate', function () {
            my.fireEvent('audioprocess', my.getCurrentTime());
        });
 
        var prevMedia = container.querySelector(this.mediaType);
        if (prevMedia) {
            container.removeChild(prevMedia);
        }
        container.appendChild(media);
 
        this.media = media;
        this.peaks = peaks;
        this.onPlayEnd = null;
        this.buffer = null;
        this.setPlaybackRate(this.playbackRate);
    },
 
    isPaused: function () {
        return !this.media || this.media.paused;
    },
 
    getDuration: function () {
        var duration = this.media.duration;
        if (duration >= Infinity) { // streaming audio
            duration = this.media.seekable.end();
        }
        return duration;
    },
 
    getCurrentTime: function () {
        return this.media && this.media.currentTime;
    },
 
    getPlayedPercents: function () {
        return (this.getCurrentTime() / this.getDuration()) || 0;
    },
 
    /**
     * Set the audio source playback rate.
     */
    setPlaybackRate: function (value) {
        this.playbackRate = value || 1;
        this.media.playbackRate = this.playbackRate;
    },
 
    seekTo: function (start) {
        if (start != null) {
            this.media.currentTime = start;
        }
        this.clearPlayEnd();
    },
 
    /**
     * Plays the loaded audio region.
     *
     * @param {Number} start Start offset in seconds,
     * relative to the beginning of a clip.
     * @param {Number} end End offset in seconds,
     * relative to the beginning of a clip.
     */
    play: function (start, end) {
        this.seekTo(start);
        this.media.play();
        end && this.setPlayEnd(end);
        this.fireEvent('play');
    },
 
    /**
     * Pauses the loaded audio.
     */
    pause: function () {
        this.media && this.media.pause();
        this.clearPlayEnd();
        this.fireEvent('pause');
    },
 
    setPlayEnd: function (end) {
        var my = this;
        this.onPlayEnd = function (time) {
            if (time >= end) {
                my.pause();
                my.seekTo(end);
            }
        };
        this.on('audioprocess', this.onPlayEnd);
    },
 
    clearPlayEnd: function () {
        if (this.onPlayEnd) {
            this.un('audioprocess', this.onPlayEnd);
            this.onPlayEnd = null;
        }
    },
 
    getPeaks: function (length) {
        if (this.buffer) {
            return WaveSurfer.WebAudio.getPeaks.call(this, length);
        }
        return this.peaks || [];
    },
 
    getVolume: function () {
        return this.media.volume;
    },
 
    setVolume: function (val) {
        this.media.volume = val;
    },
 
    destroy: function () {
        this.pause();
        this.unAll();
        this.media && this.media.parentNode && this.media.parentNode.removeChild(this.media);
        this.media = null;
    }
});
 
//For backwards compatibility
WaveSurfer.AudioElement = WaveSurfer.MediaElement;