-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#272 Co-authored-by: Aliang <nxl1996@gmail.com>
- Loading branch information
Showing
7 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<button mat-icon-button aria-label="播放音频" (click)="onClicked()"> | ||
<mat-icon *ngIf="state === PlayerStateEnum.Idle">play_arrow</mat-icon> | ||
<mat-icon *ngIf="state === PlayerStateEnum.Loading">more_horiz</mat-icon> | ||
<mat-icon *ngIf="state === PlayerStateEnum.Playing">stop</mat-icon> | ||
</button> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {async, ComponentFixture, TestBed} from '@angular/core/testing'; | ||
|
||
import {AudioPlayerComponent} from './audio-player.component'; | ||
|
||
describe('AudioPlayerComponent', () => { | ||
let component: AudioPlayerComponent; | ||
let fixture: ComponentFixture<AudioPlayerComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({declarations: [AudioPlayerComponent]}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(AudioPlayerComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {Component, Input, OnInit} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-audio-player', | ||
templateUrl: './audio-player.component.html', | ||
styleUrls: ['./audio-player.component.scss'] | ||
}) | ||
export class AudioPlayerComponent implements OnInit { | ||
public PlayerStateEnum = PlayerState; | ||
|
||
@Input('audioUrl') audioUrl: string; | ||
state: PlayerState = PlayerState.Idle; | ||
private currentAudio: HTMLAudioElement = null; | ||
|
||
constructor() {} | ||
|
||
ngOnInit(): void {} | ||
|
||
onClicked() { | ||
switch (this.state) { | ||
case PlayerState.Idle: | ||
console.log('playing + ' + this.audioUrl); | ||
this.currentAudio = new Audio(this.audioUrl); | ||
this.state = PlayerState.Loading; | ||
this.currentAudio.onended = () => { | ||
this.state = PlayerState.Idle; | ||
this.currentAudio = null; | ||
}; | ||
this.currentAudio.oncanplaythrough = () => { | ||
this.state = PlayerState.Playing; | ||
this.currentAudio.play(); | ||
}; | ||
this.currentAudio.onerror = (e) => { | ||
let mediaError = this.currentAudio.error; | ||
this.currentAudio = null; | ||
if (mediaError.message.indexOf('404') >= 0) { | ||
console.log('404 encountered. Disabling audio player for ' + this.audioUrl); | ||
this.state = PlayerState.Disabled; | ||
} else { | ||
this.state = PlayerState.Idle; | ||
} | ||
}; | ||
break; | ||
case PlayerState.Playing: | ||
if (this.currentAudio != null) { | ||
} | ||
break; | ||
case PlayerState.Loading: | ||
case PlayerState.Disabled: | ||
console.log('Do nothing'); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
enum PlayerState { | ||
// Initial State. | ||
Idle, | ||
Loading, | ||
Playing, | ||
// When a 404 is encountered. Prevents further attempts. | ||
Disabled | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters