@@ -541,23 +541,49 @@ export default {
541
541
this .recorder .onerror = event => reject (event .name )
542
542
})
543
543
544
- stopped .then (() => {
544
+ stopped .then (async () => {
545
545
stream .getTracks ().forEach (track => track .stop ())
546
546
547
- const recordedBlob = new Blob (this .recordedChunks , {
547
+ const blob = new Blob (this .recordedChunks , {
548
548
type: ' audio/ogg; codecs="opus"'
549
549
})
550
550
551
+ const duration = await this .getBlobDuration (blob)
552
+
551
553
this .file = {
552
- blob: recordedBlob ,
554
+ blob: blob ,
553
555
name: ' audio' ,
554
- size: recordedBlob .size ,
555
- type: recordedBlob .type ,
556
+ size: blob .size ,
557
+ duration: duration,
558
+ type: blob .type ,
556
559
audio: true ,
557
- localUrl: URL .createObjectURL (recordedBlob )
560
+ localUrl: URL .createObjectURL (blob )
558
561
}
559
562
})
560
563
},
564
+ getBlobDuration (blob ) {
565
+ const tempVideoEl = document .createElement (' video' )
566
+
567
+ const durationP = new Promise (resolve =>
568
+ tempVideoEl .addEventListener (' loadedmetadata' , () => {
569
+ if (tempVideoEl .duration === Infinity ) {
570
+ tempVideoEl .currentTime = Number .MAX_SAFE_INTEGER
571
+ tempVideoEl .ontimeupdate = () => {
572
+ tempVideoEl .ontimeupdate = null
573
+ resolve (tempVideoEl .duration )
574
+ tempVideoEl .currentTime = 0
575
+ }
576
+ } else resolve (tempVideoEl .duration )
577
+ })
578
+ )
579
+
580
+ tempVideoEl .src =
581
+ typeof blob === ' string' || blob instanceof String
582
+ ? blob
583
+ : window .URL .createObjectURL (blob)
584
+
585
+ return durationP
586
+ },
561
587
addNewMessage (message ) {
562
588
this .newMessages .push (message)
563
589
},
0 commit comments