浏览代码

Merge pull request #9158 from jocravio/master

Add current time property to sound
Raanan Weber 4 年之前
父节点
当前提交
8ec3c87d4f
共有 2 个文件被更改,包括 17 次插入0 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 16 0
      src/Audio/sound.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -246,6 +246,7 @@
 ### Audio
 
 - Added support of `metadata` in `Sound` class. ([julien-moreau](https://github.com/julien-moreau))
+- Added `currentTime` property to the `Sound` class. ([jocravio](https://github.com/jocravio))
 
 ### Build
 

+ 16 - 0
src/Audio/sound.ts

@@ -150,6 +150,21 @@ export class Sound {
      */
     public onEndedObservable = new Observable<Sound>();
 
+    /**
+     * Gets the current time for the sound.
+     */
+    public get currentTime(): number {
+        if (this._htmlAudioElement) {
+            return this._htmlAudioElement.currentTime;
+        }
+
+        let currentTime: number = this._startOffset;
+        if (this.isPlaying && Engine.audioEngine.audioContext) {
+            currentTime += Engine.audioEngine.audioContext.currentTime - this._startTime;
+        }
+        return currentTime;
+    }
+
     private _panningModel: string = "equalpower";
     private _playbackRate: number = 1;
     private _streaming: boolean = false;
@@ -835,6 +850,7 @@ export class Sound {
 
     private _onended() {
         this.isPlaying = false;
+        this._startOffset = 0;
         if (this.onended) {
             this.onended();
         }