在开发环境中正常工作时,产品上的音频无法正常工作(Angular 7)。
产品配置(VPS):
音频服务:
export class AudioService {
audio = new Audio();
constructor() { }
isPlaying() {
return this.audio.currentTime > 0 && !this.audio.paused && !this.audio.ended && this.audio.readyState > 2;
}
play(name: string): void {
this.audio.src = `assets/audio/${name}`;
this.audio.crossOrigin = 'anonymous';
this.audio.load();
if (!this.isPlaying()) {
this.audio.play();
}
}
pause(): void {
if (this.isPlaying()) {
this.audio.pause();
}
}
}
在Nodejs端启用了CORS(使用Nestjs)。 main.ts :
app.enableCors();
Chrome浏览器日志:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
Firefox日志:
NotSupportedError: The media resource indicated by the src attribute or assigned media provider object was not suitable.
查看网络控制台,我们可以看到myaudio.wav与:
Status Code: 206 Partial Content
注意:加载图像效果很好!
编辑:
Nginx配置 / etc / nginx / sites-available / mywebsite :
# Redirection
server {
# if ($host = mywebsite.com) {
# return 301 https://$host$request_uri;
# } # managed by Certbot
listen 80;
listen [::]:80;
server_name mywebsite.com www.mywebsite.com;
return 301 https://$host$request_uri;
#return 404; # managed by Certbot
}
# Config
server {
server_name mywebsite.com www.mywebsite.com;
root /home/foo/mywebsite/gui;
index index.html index.htm;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://my.ip:3000/;
# Websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
if ($host = 'www.mywebsite.com') {
return 301 https://mywebsite.com$request_uri;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
}
在开发环境上localhost:4200 / assets / audio / myaudio.wav→正常运行
在生产环境中https://mywebsite.com/assets/audio/myaudio.wav→返回主页
虽然https://mywebsite.com/assets/image.jpg→正常
仅音频无效。
最佳答案
关于javascript - Nginx音频文件(wav/ogg/mp3)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576313/