为什么部署在Heroku上的React应用无法正常播放声音?
问题背景
基于Node.js+Express开发的React应用,使用封装了howler.js的第三方Hook库use-sound实现环境音与游戏音效播放。应用本地开发阶段运行完全正常,部署到线上后出现异常。
预期表现
- 站点加载完成后,左上角的音频图标支持用户手动控制开启环境音
- 进入游戏页面后,对应交互音效可同步正常启用
实际表现
- 站点初始加载完成时,音频图标默认显示为未静音状态
- 点击图标按钮后,图标状态切换、音频设置菜单均可正常显示,但全程没有任何声音输出
- 浏览器控制台未输出任何和音频加载、播放相关的报错信息
项目基本信息
- 本地运行环境:4台团队成员设备上,所有声音功能无异常
- 线上运行环境:托管在Heroku平台,使用Node.js运行时、适配子目录项目结构的Heroku构建包完成打包部署
- 技术依赖:音频能力基于
howler.js底层实现,上层通过use-soundHook封装调用
核心相关代码实现
全局音频管理组件 SoundSuite
import React, { useState, useContext, createContext } from 'react'; import useSound from 'use-sound'; import soundEnums from '../../../game/SpriteEnums.js'; import SfxOptions from '../SfxOptions/index'; import ButtonGroup from 'react-bootstrap/ButtonGroup'; import './style.css' import SfxButton from '../SfxButton/index.js'; function SoundSuite({ children }) { const sfx = useProvideSfx(); return ( <> <sfxContext.Provider value={sfx}> <SfxButton /> {children} </sfxContext.Provider> </> ) }; // 全局获取音频管理状态的Hook export function useSfx() { return useContext(sfxContext); } export default SoundSuite; // 全局音频状态上下文,跨页面同步声音状态 const sfxContext = createContext(); // 全局音频状态与方法实现 function useProvideSfx() { const [soundEnabled, setSoundEnabled] = useState(false); const [playbackRate, setPlaybackRate] = useState(1); const [masterVol, setMasterVol] = useState(1.0); const [nextFile, setNextFile] = useState(''); const [ambientFile, setAmbientFile] = useState('Sound_background_0'); const [sfxFile, setSfxFile] = useState('Sound_pop_0'); const [ambientVol, setAmbientVol] = useState(0.5); const [sfxVol, setSfxVol] = useState(0.5); const [auto, setAuto] = useState(false); const [sfxAuto, setSFXAuto] = useState(false); // 环境音播放实例 const [play, { pause, stop, isPlaying } ] = useSound( soundEnums[ambientFile].src, { autoplay: auto, preload: true, volume: masterVol * ambientVol, loop: true, playbackRate, id: ambientFile }, ); // 交互音效播放实例 const [playSfx, { sound }] = useSound( soundEnums[sfxFile].src, { autoplay: sfxAuto, preload: true, loop: false, volume: masterVol * sfxVol, id: sfxFile, soundEnabled }, ); // 静音/取消静音切换 const mute = (enable) => { if(enable) { setSFXAuto(false); setAuto(true); setSoundEnabled(true); play(ambientFile); }else{ setAuto(false); setSFXAuto(false); setSoundEnabled(false); pause(); }; }; // 播放速度调节,支持0.5x ~ 4x倍率 const playbackSpeed = (rate) => setPlaybackRate(rate); // 主音量调节,范围0(静音)~1(最大) const masterVolume = (vol) => setMasterVol(vol); // 环境音切换队列 const playNext = (file) => { if(isPlaying) setNextFile(file); }; // 强制切换环境音文件 const ambientSound = (file) => { if(ambientFile !== file) { setAmbientFile(file); if(!soundEnabled) return; setAuto(true); pause(); }else{ if(!soundEnabled) return; setAuto(true); setAmbientFile(file); stop(); play(); } }; // 强制切换交互音效文件 const sfxSound = (file) => { if(!soundEnabled) return; setSFXAuto(true); // 避免初始静音状态下,按钮首次点击触发音效 setAuto(true); // 更换不同音效时,卸载原有的音效实例 if(sfxFile !== file) sound.unload(sfxFile); setSfxFile(file, nextOne(file)); }; // 环境音独立音量调节 const ambientVolume = (vol) => setAmbientVol(vol); // 音效独立音量调节 const sfxVolume = (vol) => setSfxVol(vol); const nextOne = (file) => playSfx(file); return { mute, playbackSpeed, masterVolume, playNext, ambientSound, sfxSound, ambientVolume, sfxVolume, soundEnabled, playbackRate, masterVol, nextFile, ambientFile, sfxFile, ambientVol, sfxVol }; };
音频控制按钮组件 SfxButton
import React from 'react'; import SfxOptions from '../SfxOptions/index'; import ButtonGroup from 'react-bootstrap/ButtonGroup'; import { useSfx } from "../../components/SoundSuite/index"; import "./style.css" export default function SfxButton() { const sfx = useSfx(); const toggle = (event) => { event.preventDefault(); event.stopPropagation(); if(event.target.id === 'play'){ sfx.mute(true); }else if(event.target.id === 'mute'){ sfx.mute(false); } }; return ( <div className="sound-bar" value={sfx}> {!sfx.soundEnabled ? ( <div className="row align-items-center phone-row"> <div className="col-sm-2 soundwave"> <button id='play' onClick={toggle}> <svg width="1.5em" height="1.5em" viewBox="0 0 16 16" class="bi bi-soundwave sound-icon" fill="white" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M8.5 2a.5.5 0 0 1 .5.5v11a.5.5 0 0 1-1 0v-11a.5.5 0 0 1 .5-.5zm-2 2a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zm4 0a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zm-6 1.5A.5.5 0 0 1 5 6v4a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm8 0a.5.5 0 0 1 .5.5v4a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm-10 1A.5.5 0 0 1 3 7v2a.5.5 0 0 1-1 0V7a.5.5 0 0 1 .5-.5zm12 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0V7a.5.5 0 0 1 .5-.5z"/> </svg> </button> </div> <div className="col-sm-10 game-title-phone"> AsheN Void </div> </div> ) : ( <ButtonGroup className="mr-2" aria-label="First group"> <button id='mute' onClick={toggle}> <svg width="1.5em" height="1.5em" viewBox="0 0 16 16" class="bi bi-soundwave sound-icon" fill="white" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M8.5 2a.5.5 0 0 1 .5.5v11a.5.5 0 0 1-1 0v-11a.5.5 0 0 1 .5-.5zm-2 2a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zm4 0a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zm-6 1.5A.5.5 0 0 1 5 6v4a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm8 0a.5.5 0 0 1 .5.5v4a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm-10 1A.5.5 0 0 1 3 7v2a.5.5 0 0 1-1 0V7a.5.5 0 0 1 .5-.5zm12 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0V7a.5.5 0 0 1 .5-.5z"/> </svg> </button> <SfxOptions /> </ButtonGroup> )} </div> ) }
根应用组件 App
import React from "react"; import { Router, Route, Switch} from "react-router-dom"; import UserAuth from "./userInterface/components/UserAuth/index" import PrivateRoute from "./userInterface/components/PrivateRoute/index" import PreventReverse from "./userInterface/components/PreventReverse/index"; import history from "./utils/history"; import Nav from "./userInterface/components/Nav"; import Wrapper from "./userInterface/components/Wrapper"; import Rules from "./userInterface/pages/Rules"; import SignUp from "./userInterface/pages/SignUp"; import LogIn from "./userInterface/pages/LogIn"; import GamePage from "./userInterface/pages/GamePage"; import ContentWrapper from "./userInterface/components/Wrapper/ContentWrapper"; import GameContainer from "./userInterface/components/GameContainer/index" import HomePage from "./userInterface/pages/HomePage"; import LoadingPage from "./userInterface/pages/LoadingPage" import SoundSuite from "./userInterface/components/SoundSuite/index" function App() { return ( <UserAuth> <SoundSuite> <Router history={history}> <div> <Wrapper> <Switch> <PreventReverse path="/" exact> <ContentWrapper> <HomePage /> </ContentWrapper> </PreventReverse> <PreventReverse path="/rules"> <ContentWrapper> <Rules /> </ContentWrapper> </PreventReverse> <PreventReverse path="/login"> <ContentWrapper> <LogIn /> </ContentWrapper> </PreventReverse> <PreventReverse path="/signup"> <ContentWrapper> <SignUp /> </ContentWrapper> </PreventReverse> <PrivateRoute path="/game"> <GamePage /> </PrivateRoute> <PreventReverse path="/loading"> <LoadingPage /> </PreventReverse> </Switch> </Wrapper> </div> </Router> </SoundSuite> </UserAuth> ); } export default App;
内容的提问来源于stack exchange,提问作者LazarusOfSuburbia
相关产品推荐
相关产品推荐

