Newer
Older
<template>
<div class="cw-block cw-block-audio">
<courseware-default-block
:block="block"
:canEdit="canEdit"
:isTeacher="isTeacher"
:preview="true"
@storeEdit="storeBlock"
@closeEdit="initCurrentData"
>
<template #content>
<div v-if="currentTitle !== ''" class="cw-block-title">{{ currentTitle }}</div>
<audio
:src="currentURL"
class="cw-audio-player"
ref="audio"
@timeupdate="onTimeUpdateListener"
@loadeddata="setDuration"
@ended="onEndedListener"
/>
<div v-if="!emptyAudio" class="cw-audio-container">
<div class="cw-audio-current-track">
<p>{{ activeTrackName }}</p>
</div>
<div class="cw-audio-controls">
<input
class="cw-audio-range"
ref="range"
type="range"
:value="currentSeconds"
min="0"
:max="Math.round(durationSeconds)"
@input="rangeAction"
/>
<span class="cw-audio-time">{{ currentTime }} {{ durationTime ? '/ ' + durationTime : '' }}</span>
<button v-if="hasPlaylist" class="cw-audio-button cw-audio-prevbutton" :title="$gettext('Zurück')" @click="prevAudio" />
<button v-if="!playing" class="cw-audio-button cw-audio-playbutton" :title="$gettext('Abspielen')" @click="playAudio" />
<button v-if="playing" class="cw-audio-button cw-audio-pausebutton" :title="$gettext('Pause')" @click="pauseAudio" />
<button v-if="hasPlaylist" class="cw-audio-button cw-audio-nextbutton" :title="$gettext('Weiter')" @click="nextAudio" />
<button class="cw-audio-button cw-audio-stopbutton" :title="$gettext('Anhalten')" @click="stopAudio" />
<div class="cw-audio-playlist-wrapper">
<ul v-show="hasPlaylist" class="cw-audio-playlist">
<li
v-for="(file, index) in files"
:key="file.id"
:class="{
'is-playing': index === currentPlaylistItem && playing,
'current-item': index === currentPlaylistItem,
}"
class="cw-playlist-item"
@click="setCurrentPlaylistItem(index)"
>
{{ file.name }}
</li>
</ul>
<div v-if="emptyAudio" class="cw-audio-empty">
<p><translate>Es ist keine Audio-Datei verfügbar</translate></p>
</div>
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
<div v-if="showRecorder && canGetMediaDevices" class="cw-audio-playlist-recorder">
<button
v-show="!userRecorderEnabled"
class="button"
@click="enableRecorder"
>
<translate>Aufnahme aktivieren</translate>
</button>
<button
v-show="userRecorderEnabled && !isRecording && !newRecording"
class="button"
@click="startRecording"
>
<translate>Aufnahme starten</translate>
</button>
<button
v-show="newRecording && !isRecording"
class="button"
@click="startRecording"
>
<translate>Aufnahme wiederholen</translate>
</button>
<button
v-show="isRecording"
class="button"
@click="stopRecording"
>
<translate>Aufnahme beenden</translate>
</button>
<button
v-show="newRecording && !isRecording"
class="button"
@click="resetRecorder"
>
<translate>Aufnahme löschen</translate>
</button>
<button
v-show="newRecording && !isRecording"
class="button"
@click="storeRecording"
>
<translate>Aufnahme speichern</translate>
</button>
<span v-show="isRecording">
<translate>Aufnahme läuft</translate>: {{seconds2time(timer)}}
</span>
</div>
</div>
</template>
<template v-if="canEdit" #edit>
<form class="default" @submit.prevent="">
<label>
<translate>Überschrift</translate>
<input type="text" v-model="currentTitle" />
</label>
<label>
<translate>Quelle</translate>
<select v-model="currentSource">
<option value="studip_file"><translate>Dateibereich Datei</translate></option>
<option value="studip_folder"><translate>Dateibereich Ordner</translate></option>
<option value="web"><translate>Web-Adresse</translate></option>
</select>
</label>
<label v-show="currentSource === 'web'">
<translate>URL</translate>
<input type="text" v-model="currentWebUrl" />
</label>
<label v-show="currentSource === 'studip_file'">
<translate>Datei</translate>
<courseware-file-chooser
v-model="currentFileId"
:isAudio="true"
@selectFile="updateCurrentFile"
/>
</label>
<label v-show="currentSource === 'studip_folder'">
<translate>Ordner</translate>
<courseware-folder-chooser v-model="currentFolderId" allowUserFolders />
</label>
<label v-show="currentSource === 'studip_folder'">
<translate>Audio Aufnahmen zulassen</translate>
<select v-model="currentRecorderEnabled">
<option :value="true"><translate>Ja</translate></option>
<option :value="false"><translate>Nein</translate></option>
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
230
231
232
</select>
</label>
</form>
</template>
<template #info>
<p><translate>Informationen zum Audio-Block</translate></p>
</template>
</courseware-default-block>
</div>
</template>
<script>
import CoursewareDefaultBlock from './CoursewareDefaultBlock.vue';
import CoursewareFileChooser from './CoursewareFileChooser.vue';
import CoursewareFolderChooser from './CoursewareFolderChooser.vue';
import { mapActions, mapGetters } from 'vuex';
export default {
name: 'courseware-audio-block',
components: {
CoursewareDefaultBlock,
CoursewareFileChooser,
CoursewareFolderChooser,
},
props: {
block: Object,
canEdit: Boolean,
isTeacher: Boolean,
},
data() {
return {
currentTitle: '',
currentSource: '',
currentFileId: '',
currentFolderId: '',
currentWebUrl: '',
currentFile: {},
currentSeconds: 0,
durationSeconds: 0,
playing: false,
currentPlaylistItem: 0,
currentRecorderEnabled: false,
userRecorderEnabled: false,
recorder: null,
chunks: [],
timer: 0,
isRecording: false,
newRecording: false,
};
},
computed: {
...mapGetters({
fileRefById: 'file-refs/byId',
relatedFileRefs: 'file-refs/related',
urlHelper: 'urlHelper',
userId: 'userId',
usersById: 'users/byId',
relatedTermOfUse: 'terms-of-use/related'
}),
files() {
const files =
this.relatedFileRefs({
parent: { type: 'folders', id: this.currentFolderId },
relationship: 'file-refs'
}) ?? [];
return files
.filter((file) => {
if (this.relatedTermOfUse({parent: file, relationship: 'terms-of-use'}).attributes['download-condition'] !== 0) {
return false;
}
if (! file.attributes['mime-type'].includes('audio')) {
return false;
}
return true;
})
.map(({ id, attributes }) => {
return {
id,
name: attributes.name,
download_url: this.urlHelper.getURL(
'sendfile.php/',
{ type: 0, file_id: id, file_name: attributes.name },
true
),
};
});
},
currentTime() {
return this.seconds2time(this.currentSeconds);
},
durationTime() {
if (this.durationSeconds > 0) {
return this.seconds2time(this.durationSeconds);
}
return false;
},
title() {
return this.block?.attributes?.payload?.title;
},
source() {
return this.block?.attributes?.payload?.source;
},
fileId() {
return this.block?.attributes?.payload?.file_id;
},
folderId() {
return this.block?.attributes?.payload?.folder_id;
},
webUrl() {
return this.block?.attributes?.payload?.web_url;
},
recorderEnabled() {
return this.block?.attributes?.payload?.recorder_enabled;
},
showRecorder() {
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
},
hasPlaylist() {
return this.files.length > 0 && this.currentSource === 'studip_folder';
},
canGetMediaDevices() {
return navigator.mediaDevices !== undefined;
},
currentURL() {
if (this.currentSource === 'studip_file') {
return this.currentFile.download_url;
}
if (this.currentSource === 'studip_folder') {
if (this.files.length > 0) {
return this.files[this.currentPlaylistItem].download_url;
} else {
return '';
}
}
if (this.currentSource === 'web') {
return this.currentWebUrl;
}
return '';
},
activeTrackName() {
if (this.currentSource === 'studip_file') {
return this.currentFile.name;
}
if (this.currentSource === 'studip_folder') {
if (this.files.length > 0) {
return this.files[this.currentPlaylistItem].name;
} else {
return '';
}
}
if (this.currentSource === 'web') {
return this.currentWebUrl;
}
return '';
},
emptyAudio() {
if (this.currentSource === 'studip_folder' && this.currentFolderId !== '' && this.files.length > 0) {
return false;
}
if (this.currentSource === 'studip_file' && this.currentFileId !== '') {
return false;
}
if (this.currentSource === 'web' && this.currentWebUrl !== '') {
return false;
}
return true;
}
},
mounted() {
this.initCurrentData();
},
methods: {
...mapActions({
loadFileRef: 'file-refs/loadById',
loadRelatedFileRefs: 'file-refs/loadRelated',
updateBlock: 'updateBlockInContainer',
companionWarning: 'companionWarning',
companionSuccess: 'companionSuccess',
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
createFile: 'createFile',
}),
initCurrentData() {
this.currentTitle = this.title;
this.currentSource = this.source;
this.currentFileId = this.fileId;
this.currentWebUrl = this.webUrl;
if (this.currentFileId !== '') {
this.loadFile();
}
this.currentFolderId = this.folderId;
this.currentRecorderEnabled = this.recorderEnabled;
},
updateCurrentFile(file) {
this.currentFile = file;
this.currentFileId = file.id;
},
getFolderFiles() {
return this.loadRelatedFileRefs({
parent: { type: 'folders', id: this.currentFolderId },
relationship: 'file-refs',
options: { include: 'terms-of-use' }
});
},
storeBlock() {
let attributes = {};
attributes.payload = {};
attributes.payload.title = this.currentTitle;
attributes.payload.source = this.currentSource;
attributes.payload.file_id = '';
attributes.payload.web_url = '';
attributes.payload.folder_id = '';
if (this.currentSource === 'studip_file') {
if (this.currentFileId === '') {
this.companionWarning({
info: this.$gettext('Bitte wählen Sie eine Datei aus.')
});
return false;
}
attributes.payload.file_id = this.currentFileId;
} else if (this.currentSource === 'web') {
attributes.payload.web_url = this.currentWebUrl;
} else if (this.currentSource === 'studip_folder') {
if (this.currentFolderId === '') {
this.companionWarning({
info: this.$gettext('Bitte wählen Sie einen Ordner aus.')
});
return false;
}
attributes.payload.folder_id = this.currentFolderId;
attributes.payload.recorder_enabled = this.currentRecorderEnabled;
} else {
return false;
}
this.updateBlock({
attributes: attributes,
blockId: this.block.id,
containerId: this.block.relationships.container.data.id,
});
},
rangeAction() {
if (this.$refs.range.value !== this.currentSeconds) {
this.$refs.audio.currentTime = this.$refs.range.value;
}
},
setDuration() {
let duration = this.$refs.audio.duration
if (!isNaN(duration) && isFinite(duration)) {
this.durationSeconds = duration;
} else {
this.durationSeconds = 0;
}
const audio = this.$refs.audio;
let isSupported = 'unknown';
if (this.currentSource === 'studip_file') {
isSupported = audio.canPlayType(this.currentFile.mime_type);
}
if (this.currentSource === 'studip_folder') {
isSupported = audio.canPlayType(this.files[this.currentPlaylistItem].mime_type);
}
if (isSupported !== '') {
audio.play();
this.playing = true;
} else {
this.companionError({
info: this.$gettext('Ihr Browser unterstützt dieses Audioformat leider nicht.')
});
if(this.hasPlaylist) {
this.nextAudio();
}
}
},
pauseAudio() {
this.$refs.audio.pause();
this.playing = false;
},
stopAudio() {
this.pauseAudio();
this.$refs.audio.currentTime = 0;
},
onTimeUpdateListener() {
this.currentSeconds = this.$refs.audio.currentTime;
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
},
onEndedListener() {
this.stopAudio();
if(this.hasPlaylist) {
this.nextAudio();
}
},
seconds2time(seconds) {
seconds = Math.round(seconds);
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor((seconds - hours * 3600) / 60);
let time = '';
seconds = seconds - hours * 3600 - minutes * 60;
if (hours !== 0) {
time = hours + ':';
}
if (minutes !== 0 || time !== '') {
minutes = minutes < 10 && time !== '' ? '0' + minutes : String(minutes);
time += minutes + ':';
}
if (time === '') {
time = seconds < 10 ? '0:0' + seconds : '0:' + seconds;
} else {
time += seconds < 10 ? '0' + seconds : String(seconds);
}
return time;
},
setCurrentPlaylistItem(index) {
if (this.currentPlaylistItem === index) {
if (this.playing) {
this.pauseAudio();
} else {
this.playAudio();
}
} else {
this.currentPlaylistItem = index;
this.$nextTick(()=> {
this.playAudio();
});
}
},
prevAudio() {
this.stopAudio();
if (this.currentPlaylistItem !== 0) {
this.currentPlaylistItem = this.currentPlaylistItem - 1;
} else {
this.currentPlaylistItem = this.files.length - 1;
}
this.$nextTick(()=> {
this.playAudio();
});
},
nextAudio() {
this.stopAudio();
if (this.currentPlaylistItem < this.files.length - 1) {
this.currentPlaylistItem = this.currentPlaylistItem + 1;
}
},
async loadFile() {
const id = this.currentFileId;
await this.loadFileRef({ id });
const fileRef = this.fileRefById({ id });
if (fileRef) {
this.updateCurrentFile({
id: fileRef.id,
name: fileRef.attributes.name,
download_url: this.urlHelper.getURL(
'sendfile.php',
{ type: 0, file_id: fileRef.id, file_name: fileRef.attributes.name },
true
),
});
}
},
enableRecorder() {
let view = this;
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function(stream) {
view.recorder = new MediaRecorder(stream, {type: 'audio/webm; codecs:vp9' });
view.userRecorderEnabled = true;
view.recorder.ondataavailable = e => {
view.chunks.push(e.data);
};
})
.catch(() => {
view.companionWarning({
info: view.$gettext('Sie müssen ein Mikrofon freigeben, um eine Aufnahme starten zu können.')
});
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
});
},
startRecording() {
let view = this;
this.chunks = [];
this.timer = 0;
this.recorder.start();
this.isRecording = true;
setTimeout(function(){ view.setTimer(); }, 1000);
},
stopRecording() {
this.isRecording = false;
this.newRecording = true;
this.recorder.stop();
},
setTimer() {
let view = this;
if (this.recorder.state === 'recording') {
this.timer++;
setTimeout(function(){ view.setTimer(); }, 1000);
}
},
async storeRecording() {
let view = this;
let user = this.usersById({id: this.userId});
let file = {};
let blob = new Blob(view.chunks, {type: 'audio/webm; codecs:vp9' });
file.attributes.name = (user.attributes["formatted-name"]).replace(/\s+/g, '_') + '.webm';
let fileObj = false;
try {
fileObj = await this.createFile({
file: file,
folder: {id: this.currentFolderId}
});
}
catch(e) {
this.companionError({
info: this.$gettext('Es ist ein Fehler aufgetreten! Die Aufnahme konnte nicht gespeichert werden.')
});
console.debug(e);
}
if(fileObj && fileObj.type === 'file-refs') {
this.companionSuccess({
info: this.$gettext('Die Aufnahme wurde erfolgreich im Dateibereich abgelegt.')
});
}
this.newRecording = false;
this.getFolderFiles();
},
resetRecorder() {
this.newRecording = false;
this.chunks = [];
this.timer = 0;
this.blob = null;
},
},
watch: {
currentFolderId() {
this.getFolderFiles();
},
},
};
</script>