简体   繁体   English

如何解决audio.play()的问题?

[英]How to fix issue with audio.play()?

I am trying to use the JavaScript Audio object. 我正在尝试使用JavaScript Audio对象。 I am following this guide very closely: https://medium.com/@bryanjenningz/how-to-record-and-play-audio-in-javascript-faa1b2b3e49b . 我正在密切关注本指南: https : //medium.com/@bryanjenningz/how-to-record-and-play-audio-in-javascript-faa1b2b3e49b

In part 6, I am supposed to call play() on an Audio object, but I am receiving an error that says "Uncaught TypeError: audio.play is not a function" and I am unsure what I am doing incorrectly. 在第6部分中,我应该在Audio对象上调用play(),但是我收到一条错误消息,提示“未捕获的TypeError:audio.play不是函数”,而且我不确定自己做错了什么。 Any help? 有什么帮助吗?

Edit: Code so far: 编辑:到目前为止的代码:

import React, { Component } from 'react'

class Audio extends Component {

  constructor() {
    super();
    this.state = {
      showButton: true,
    };
  }

  recordAudio = () => {
    this.setState({ showButton: false })

    navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
      this.setState({ mediaRecorder: new MediaRecorder(stream) })
      this.state.mediaRecorder.start();

      const audioChunks = [];

      this.state.mediaRecorder.addEventListener("dataavailable", event => {
        audioChunks.push(event.data);
      });

      this.state.mediaRecorder.addEventListener("stop", () => {
        const audioBlob = new Blob(audioChunks);
        const audioUrl = URL.createObjectURL(audioBlob);
        const audio = new Audio(audioUrl);
        console.log(audio);
        audio.play();
      });
    });
  }

  stopRecording = () => {
    this.setState({ showButton: true })
    this.state.mediaRecorder.stop();
  }

  render() {
    return (
      <div>

      {/* This is a ternary operator that changes the button that is shown  */}
      {
        this.state.showButton ?
        <button type="button" onClick={this.recordAudio}> Start Recording </button> :
        <button type="button" onClick={this.stopRecording}> Stop Recording </button>
      }

      </div>

    )
  }

}

export default Audio;

Edit: console.log(audio) output: 编辑:console.log(音频)输出:

Audio {props: undefined, context: undefined, refs: {…}, updater: {…}, recordAudio: ƒ, …}
    context:undefined
    props:undefined
    recordAudio:ƒ ()
    refs:{}
    state:{showButton: true}
    stopRecording:ƒ ()
    updater:{isMounted: ƒ, enqueueForceUpdate: ƒ, enqueueReplaceState: ƒ, enqueueSetState: ƒ}
    isMounted:(...)
    replaceState:(...)
    __proto__:ReactComponent

Kaiido's comment solved the issue for me. Kaiido的评论为我解决了这个问题。 Since I had named my class "Audio" and was calling the "Audio" constructor (when I thought I was calling window.Audio), it was not creating the appropriate object. 因为我已经将类命名为“ Audio”并调用了“ Audio”构造函数(当我以为我正在调用window.Audio时),所以它没有创建适当的对象。

As per their solution, either rename the class, call window.Audio, or call the default window.Audio functionality as detailed in their comment. 根据他们的解决方案,重命名该类,调用window.Audio或调用默认window.Audio功能,如其注释中所述。

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

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