在reactjs中播放声音

编程入门 行业动态 更新时间:2024-10-25 09:29:13
本文介绍了在reactjs中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 import React, { Component } from 'react' import { Button, Input, Icon,Dropdown,Card} from 'semantic-ui-react' import { Link } from 'react-router-dom' import $ from 'jquery' import styles from './Home.scss' import Modal from './Modal.jsx' import MakeChannelModal from './MakeChannelModal.jsx' class Music extends React.Component { constructor(props) { super(props); this.state = { play: false, pause: true }; this.url = "streaming.tdiradio:8000/house.mp3"; this.audio = new Audio(this.url); } play(){ this.setState({ play: true, pause: false }); console.log(this.audio); this.audio.play(); } pause(){ this.setState({ play: false, pause: true }); this.audio.pause(); } render() { return ( <div> <button onClick={this.play}>Play</button> <button onClick={this.pause}>Pause</button> </div> ); } } export default Music

这是我用来在我的反应应用程序中用url(this.url)播放声音的代码。当我按下播放按钮时,它会给我一个错误

This is the code that I am using to play the sound with url (this.url) in my react app. When I press the play button, it gives me an error

未捕获的TypeError:无法读取未定义的属性'setState'

我不确定为什么这是开心的,因为我没有看到任何未定义的状态。一个;;国家已被宣布。

I am not sure why this is happpening since I don't see any undefined states. A;; states have been declared.

我是新来的,所以我可能会遗漏一些非常重要的事情。

I am new to react so I might be missing something very important.

请帮助!

推荐答案

你需要绑定播放和暂停构造函数中的方法,以便在这些方法中使用的 this 引用组件的实例。

You need to bind both play and pause methods in your constructor so that the this used within either of these methods refers to the instance of the component.

在退出构造函数之前添加这两行:

Add these two lines just before exiting your constructor:

this.play = this.play.bind(this); this.pause = this.pause.bind(this);

此外,这不是你的问题,你可以真正缩短你的代码,有很多那里的冗余。类似于:

Also, but this is not really your question, you could really shorten your code, there's a lot of redundancy in there. Something like:

编辑:更新为使用ES6类属性语法

class Music extends React.Component { state = { play: false } audio = new Audio(this.props.url) togglePlay = () => { this.setState({ play: !this.state.play }, () => { this.state.play ? this.audio.play() : this.audio.pause(); }); } render() { return ( <div> <button onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button> </div> ); } } export default Music;

Hooks版本(React 16.8 +):

import React, { useState, useEffect } from "react"; const useAudio = url => { const [audio] = useState(new Audio(url)); const [playing, setPlaying] = useState(false); const toggle = () => setPlaying(!playing); useEffect( () => { playing ? audio.play() : audio.pause(); }, [playing] ); return [playing, toggle]; }; const Player = ({ url }) => { const [playing, toggle] = useAudio(url); return ( <div> <button onClick={toggle}>{playing ? "Pause" : "Play"}</button> </div> ); }; export default Player;

更多推荐

在reactjs中播放声音

本文发布于:2023-07-11 12:08:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   reactjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!