简体   繁体   English

反应音频无法设置未定义的属性“音量”

[英]React audio Cannot set property 'volume' of undefined

I am trying to play an mp3 using the Audio element but whenever the player renders an error occurs:- Cannot set property 'volume' of undefined.我正在尝试使用 Audio 元素播放 mp3,但每当播放器呈现错误时:- 无法设置未定义的属性“音量”。

Play prop is just a boolean value. Play prop 只是一个布尔值。

The useRef() current property shows me the mp3 file when I console.log it. useRef() current 属性在我 console.log 时向我显示 mp3 文件。

I removed the volume property but then it displays the same error for audio.play().我删除了 volume 属性,但随后它显示了与 audio.play() 相同的错误。

why is the audio undefined?为什么音频未定义?

import React, { useState, useRef } from "react";
import "../../Static/player.css";
import Nowplaying from "./Nowplaying";
import SongInfo from "./SongInfo";
import Slider from "./Slider";
import Duration from "./Duration";
import song from "../../Static/assets/song.mp3";

const Player = (props) => {

  const { Play } = props;

  const [percentage, setPercentage] = useState(0)
  const [duration, setDuration] = useState(0)
  const [currentTime, setCurrentTime] = useState(0)

  const audioRef = useRef()


  const onChange = (e) => {
    const audio = audioRef.current
    audio.currentTime = (audio.duration / 100) * e.target.value
    setPercentage(e.target.value)
  }


  const play = () => {
    const audio = audioRef.current
    audio.volume = 0.1

    if (!Play) {
      audio.play()
    }

    if (Play) {
      audio.pause()
    }
  }

  const getCurrDuration = (e) => {
    const percent = ((e.currentTarget.currentTime / e.currentTarget.duration) * 100).toFixed(2)
    const time = e.currentTarget.currentTime

    setPercentage(+percent)
    setCurrentTime(time.toFixed(2))
  }

  if (Play) {
    play();
  } else {
    play();
  }

  return (
    <div className="player-screen">
      <div className="play-screen">
        <div className="navbar">
          <Nowplaying />
        </div>
        <div className="song-info">
          <SongInfo />
        </div>
        <div className="player-controls">
          <Slider percentage={percentage} onChange={onChange} />
          <Duration
            duration={duration}
            currentTime={currentTime}
          />
          <audio
            ref={audioRef}
            onTimeUpdate={getCurrDuration}
            onLoadedData={(e) => {
              setDuration(e.currentTarget.duration.toFixed(2));
            }}
            src={song}
          ></audio>
        </div>
      </div>
    </div>
  );
};

export default Player;

what wrong am I doing?我做错了什么?

This part of code:这部分代码:

  if (Play) {
    play();
  } else {
    play();
  }

gets immediately called, before React even has the chance to set audioRef.current to the Audio element.在 React 甚至有机audioRef.current设置为 Audio 元素之前,它会立即被调用。 Your function hasn't even finished rendering yet, React doesn't even know where audioRef is used.你的函数还没有完成渲染,React 甚至不知道在哪里使用了audioRef

Move that piece of code into a useEffect , or better yet, replace your audioRef with a callback function (which can still store the Audio element in another ref or state variable), as shown here .动议一段代码成useEffect ,或更好的是,取代您audioRef用的回调函数(其仍可以存储在另一个ref或状态变量的音频组件),如图所示在这里

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

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