Commit 41d806ef authored by liujiangnan's avatar liujiangnan

fix: 音频不播放的问题

parent 08dec26f
......@@ -12,6 +12,7 @@ import {debounceTime} from 'rxjs/operators';
import TWEEN from '@tweenjs/tween.js';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { settings } from 'cluster';
......@@ -41,8 +42,7 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges, AfterViewIni
saveKey = 'ww_audio_image';
isPlaying = false;
audio = new Audio();
audio = null;
fntSizeRect: any;
@ViewChild('pic')
domPic: ElementRef;
......@@ -55,6 +55,8 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges, AfterViewIni
private changeDetectorRef: ChangeDetectorRef) {
}
canPlayAudio = false;
ngOnInit() {
this.data = {
contentObj: {
......@@ -65,19 +67,20 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges, AfterViewIni
}
};
window['courseware'].onEvent('course-in-screen', () => {
setTimeout(() => {
this.canPlayAudio = true;
}, 1500);
});
// 获取数据
const getData = ( window as any).courseware.getData;
getData((data) => {
if (data && typeof data === 'object') {
this.data = data;
}
this.audio.pause();
this.audio.currentTime = 0;
this.isPlaying = false;
if (this.data.contentObj && this.data.contentObj.audio_id ) {
this.audio.src = this.data.contentObj.audio_id;
this.audio.load();
console.log('Setting audio source:', this.data.contentObj.audio_id);
}
this.showQuestion = false;
this.changeDetectorRef.markForCheck();
......@@ -86,17 +89,10 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges, AfterViewIni
if(window["air"]) {
window["air"].hideAirClassLoading(this.saveKey, this.data);
}
}, this.saveKey);
this.audio.onended = () => {
this.isPlaying = false;
};
}
ngOnDestroy() {
this.audio.pause();
this.audio = null;
}
ngOnChanges(data) {
......@@ -125,16 +121,26 @@ export class PlayComponent implements OnInit, OnDestroy, OnChanges, AfterViewIni
}
playAudio(ev) {
async playAudio(ev) {
if (!this.canPlayAudio) {
return;
}
ev.preventDefault();
console.log('Trying to play audio...', window['air'].isCourseInScreen);
if (this.data.contentObj && this.data.contentObj.audio_id) {
if (!this.audio.paused) {
if (this.audio && !this.audio.paused) {
console.log('Pausing audio...');
this.audio.pause();
this.isPlaying = false;
} else {
this.audio.play();
this.isPlaying = true;
this.changeDetectorRef.markForCheck();
return;
}
this.audio = new Audio();
this.audio.src = this.data.contentObj.audio_id;
this.audio.play();
this.changeDetectorRef.markForCheck();
} else {
console.warn('No audio source available');
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment