Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Tiny Record RTC - audio recorder configuration.
18
 *
19
 * @module      tiny_recordrtc/audio_recorder
20
 * @copyright   2022 Stevani Andolo <stevani@hotmail.com.au>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import BaseClass from './base_recorder';
25
import Modal from './modal';
26
import {component} from 'tiny_recordrtc/common';
1441 ariadna 27
import {convertMp3} from './convert_to_mp3';
28
import {add as addToast} from 'core/toast';
1 efrain 29
 
30
export default class Audio extends BaseClass {
1441 ariadna 31
 
32
    // A mapping of MIME types to their corresponding file extensions.
33
    fileExtensions = {
34
        'audio/ogg': 'ogg',
35
        'audio/mp4': 'mp4',
36
        'audio/webm': 'webm',
37
    };
38
 
1 efrain 39
    configurePlayer() {
40
        return this.modalRoot.querySelector('audio');
41
    }
42
 
43
    getSupportedTypes() {
44
        return [
45
            // Firefox supports webm and ogg but Chrome only supports ogg.
46
            // So we use ogg to maximize the compatibility.
47
            'audio/ogg;codecs=opus',
48
 
49
            // Safari supports mp4.
50
            'audio/mp4;codecs=opus',
51
            'audio/mp4;codecs=wav',
52
            'audio/mp4;codecs=mp3',
1441 ariadna 53
 
54
            // Set webm as a fallback.
55
            'audio/webm;codecs=opus',
1 efrain 56
        ];
57
    }
58
 
59
    getRecordingOptions() {
60
        return {
61
            audioBitsPerSecond: parseInt(this.config.audiobitrate),
1441 ariadna 62
            audioBitsPerSecondInKb: parseInt(this.config.audiobitrate / 1000),
1 efrain 63
        };
64
    }
65
 
66
    getMediaConstraints() {
67
        return {
68
            audio: true,
69
        };
70
    }
71
 
72
    getRecordingType() {
73
        return 'audio';
74
    }
75
 
76
    getTimeLimit() {
77
        return this.config.audiotimelimit;
78
    }
79
 
80
    getEmbedTemplateName() {
81
        return 'tiny_recordrtc/embed_audio';
82
    }
83
 
84
    getFileName(prefix) {
85
        return `${prefix}-audio.${this.getFileExtension()}`;
86
    }
87
 
88
    getFileExtension() {
1441 ariadna 89
        if (this.config.audiortcformat === 1) {
90
            return 'mp3';
1 efrain 91
        }
92
 
1441 ariadna 93
        const options = super.getParsedRecordingOptions(); // Call parent method.
94
        if (options?.mimeType) {
95
            const mimeType = options.mimeType.split(';')[0];
96
            return this.fileExtensions[mimeType];
97
        }
98
 
1 efrain 99
        window.console.warn(`Unknown file type for MediaRecorder API`);
100
        return '';
101
    }
102
 
103
    static getModalClass() {
104
        return class extends Modal {
105
            static TYPE = `${component}/audio_recorder`;
106
            static TEMPLATE = `${component}/audio_recorder`;
107
        };
108
    }
1441 ariadna 109
 
110
    async uploadRecording() {
111
        if (this.getFileExtension() === "mp3") {
112
            try {
113
                const options = this.getRecordingOptions();
114
                this.blob = await convertMp3(this.player.src, options.audioBitsPerSecondInKb);
115
                this.player.src = URL.createObjectURL(this.blob);
116
            } catch (error) {
117
                // Display a user-friendly error message
118
                const message = `MP3 conversion failed: ${error.message || 'Unknown error'}. Please try again.`;
119
                addToast(message, {type: 'error', delay: 6000});
120
 
121
                // Disable the upload button.
122
                this.setUploadButtonState(false);
123
 
124
                return;
125
            }
126
        }
127
 
128
        super.uploadRecording();
129
    }
1 efrain 130
}