首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ipad React可同时播放2种声音

Ipad React可同时播放2种声音
EN

Stack Overflow用户
提问于 2017-09-13 21:59:02
回答 1查看 109关注 0票数 1

我有以下内容:(https://www.npmjs.com/package/react-sound)

代码语言:javascript
复制
    return (
        <div className = { "wrapper " + currentState.bgColor}>
            <div className="wrapper-inner">
                <CustomSound soundOn = { this.state.soundOn} />
                <CorrectSound soundOn = { this.state.correct} />
                <IncorrectSound soundOn = { this.state.incorrect} />
                <h1 className = { isH1Visible}><span>the big</span> reveal</h1>
                {form}
                {cat}
                {grid}
                {timeUp}
                {correct}
                {incorrect}
            </div>
        </div>
    )

在"getAnswer“函数中设置正确和不正确的soundOn属性:

代码语言:javascript
复制
import React, { Component } from 'react';

class Action1 extends Component {
    constructor(props) {
        super(props);

        this.state = {
            answer1: "",
            answer2: "",
            answer3: "",
            answer4: "",
            answerDisabled: false
        }
    }

    updateStateProperty(el, val) {
        var s = {};
        s[el] = val;
        this.setState(s);
    }

    getAnswer = (e) => {
        let answer = e.target.getAttribute('data-id');
        this.props.checkAnswer(e);

        if(e.target.getAttribute('data-question') == ("option_" + this.props.question.correct_option)){
            this.updateStateProperty(answer, 'correct');
            this.props.setCorrectOn(true);
        }

        else {
            this.updateStateProperty(answer, 'wrong');  
            this.props.setIncorrectOn(true);    
        }

        this.setState({
            answerDisabled: true
        })

        setTimeout(() => {
            this.props.setIncorrectOn(false);
            this.props.setCorrectOn(false);
            this.updateStateProperty(answer, '');
        }, 1000);

        setTimeout(() => {
            this.setState({
                answerDisabled: false
            })
        }, 1500)
    }

    render() {

        return(
            <div className="action">

                    <div className={"action-question " + this.props.catBgColor}>
                        <h3>{this.props.question.question}</h3>
                    </div>

                    <div className={"action-answers " + this.props.catBgColor}>
                        <p className={this.state.answer1 + " " + (this.state.answerDisabled ? 'disable-link' : "")} data-id="answer1" data-question="option_1" onClick={this.getAnswer.bind(this)}>{this.props.question.option_1}</p>
                        <p className={this.state.answer2 + " " + (this.state.answerDisabled ? 'disable-link' : "")} data-id="answer2" data-question="option_2" onClick={this.getAnswer.bind(this)}>{this.props.question.option_2}</p>
                        <p className={this.state.answer3 + " " + (this.state.answerDisabled ? 'disable-link' : "")} data-id="answer3" data-question="option_3" onClick={this.getAnswer.bind(this)}>{this.props.question.option_3}</p>
                        <p className={this.state.answer4 + " " + (this.state.answerDisabled ? 'disable-link' : "")} data-id="answer4" data-question="option_4" onClick={this.getAnswer.bind(this)}>{this.props.question.option_4}</p>
                    </div>

            </div>
        );
    }
}

export default Action1;

每个声音组件具有以下结构(具有不同的声音文件):

代码语言:javascript
复制
import React from 'react';
import Sound from 'react-sound';

class CustomSound extends React.Component {
    render() {
        return (
            <Sound
              url="./assets/sound.mp3"
              playStatus={this.props.soundOn ? Sound.status.PLAYING : Sound.status.STOPPED}
              playFromPosition={0 /* in milliseconds */}
            />
        );
    }
}

export default CustomSound;

当两个中的任何一个被触发时:

代码语言:javascript
复制
this.props.setCorrectOn(true); or this.props.setIncorrectOn(true);

声音来自:

代码语言:javascript
复制
<CustomSound soundOn = { this.state.soundOn} />

停止并重新开始。理想情况下,这是一个配乐,我想继续播放,并在游戏的后期停止它。

这个问题似乎只发生在iPad上。在桌面的chrome上运行是完全没问题的。

EN

回答 1

Stack Overflow用户

发布于 2017-09-13 23:59:48

解决方案位于:http://www.schillmania.com/projects/soundmanager2/doc/

代码语言:javascript
复制
soundManager.setup({ ignoreMobileRestrictions: true });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46199561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档