简体   繁体   English

使用 audio.play() 函数时反应错误

[英]React error when using audio.play() function

I'm trying to play a sound by triggering the function with onClick event in React and I'm getting the following error:我正在尝试通过在 React 中使用 onClick 事件触发函数来播放声音,但出现以下错误:

Uncaught Error: The error you provided does not contain a stack trace.未捕获的错误:您提供的错误不包含堆栈跟踪。

at B (index.js:1582)在 B (index.js:1582)

at G (index.js:1899)在 G (index.js:1899)

at eval (index.js:1914)在 eval (index.js:1914)

at eval (index.js:1933)在 eval (index.js:1933)

at eval (index.js:1414)在 eval (index.js:1414)



import React from "react";
import firebase from "../../firebase";
import classes from "./Sidenav.module.sass";

const Sidenav = props => {
  const logout = () => {
    firebase.auth().signOut();
    window.location.href = "..";
  };

  const playAudio = () => {
    let audio = new Audio("../../assets/bellNotification.mp3");
    audio.play();
  };

  return (
    <div style={props.styles.sideNavDiv} className={classes.sidenavDiv}>
      <i />
      <div style={props.styles.iconDiv} className={classes.iconDiv}>
        <i className={"material-icons " + classes.navIcon}>account_box</i>
        <p className={classes.iconText}>Account</p>
      </div>
      <div style={props.styles.iconDiv} className={classes.iconDiv}>
        <i className={"material-icons " + classes.navIcon}>settings</i>
        <p className={classes.iconText}>Settings</p>
      </div>
      <div style={props.styles.iconDiv} className={classes.iconDiv}>
        <i className={"material-icons " + classes.navIcon}>trending_up</i>
        <p className={classes.iconText}>Check Progress</p>
      </div>
      <div style={props.styles.iconDiv} className={classes.iconDiv}>
        <i className={"material-icons " + classes.navIcon}>looks_one</i>
        <p className={classes.iconText}>1RM calculator</p>
      </div>
      <div
        onClick={props.toggleModal}
        style={props.styles.iconDiv}
        className={classes.iconDiv}
      >
        <i className={"material-icons " + classes.navIcon}>alarm</i>
        <p className={classes.iconText}>Edit timers</p>
      </div>
      <div
        onClick={playAudio}
        style={props.styles.iconDiv}
        className={classes.iconDiv}
      >
        <i className={"material-icons " + classes.navIcon}>help</i>
        <p className={classes.iconText}>Help</p>
      </div>
      <div
        onClick={logout}
        style={props.styles.iconDiv}
        className={classes.iconDiv}
      >
        <i className={"material-icons " + classes.navIcon}>
          power_settings_new
        </i>
        <p className={classes.iconText}>Logout</p>
      </div>
    </div>
  );
};

export default Sidenav;

The same thing happened to me also because of the chrome changes in player promise.同样的事情发生在我身上也是因为玩家承诺中的 chrome 变化。 just replace your player.play() with只需将您的player.play()替换为

const playPromise = player.play();

      if (playPromise !== undefined) {
        playPromise
          .then(_ => {
            // Automatic playback started!
            // Show playing UI.
            console.log("audio played auto");
          })
          .catch(error => {
            // Auto-play was prevented
            // Show paused UI.
            console.log("playback prevented");
          });
      }

You can read more about it here on chrome blogs about player promise changes您可以在 Chrome 博客上阅读有关播放器承诺更改的更多信息

使用import audio from '../assets/audio.mp3'//your map3 path there然后创建一个对象let audio = new Audio(audio)并在所需的函数audio.play()播放声音

Okay, I managed to fix my problem.好的,我设法解决了我的问题。 This error is caused because the new Audio(url).play() points to the index.html in the public folder and for some reason it doesn't seem to work in the root div in my case.这个错误是因为new Audio(url).play()指向公共文件夹中的 index.html 并且由于某种原因它在我的情况下似乎在根 div 中不起作用。 I created the audio element in the index.html file and moved the mp3 file to the public folder.我在 index.html 文件中创建了音频元素并将 mp3 文件移动到公共文件夹。 <audio id="audio"><source src="bellNotification.mp3" type="audio/mpeg"></source></audio>

I changed the function like this我改变了这样的功能

const playAudio = () => { document.getElementById("audio").play(); };

It works for me but I hope that someone can fix this problem with pure js without accessing the html file.它对我有用,但我希望有人可以在不访问 html 文件的情况下用纯 js 解决这个问题。

For me I started getting this really vague error when the audio files had been renamed.对我来说,当音频文件被重命名时,我开始收到这个非常模糊的错误。

index.js:1 Uncaught Error: The error you provided does not contain a stack trace. index.js:1 未捕获的错误:您提供的错误不包含堆栈跟踪。

Please check that your audio files are matching the path and name in your code before you try different methods to play them.在尝试不同的播放方法之前,请检查您的音频文件是否与代码中的路径和名称匹配。

Import your audio file like this像这样导入您的音频文件

import audio from './filepath'

just replace filepath with your audio file's location Then create an audio and add its source in this way只需用您的音频文件的位置替换文件路径然后创建一个音频并以这种方式添加其源

const audioElement = new Audio()
audioElement.src = audio

The just play the audio只播放音频

audioElement.play()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM