我希望播放音乐不间断(重播)

时间:2018-12-02 06:44:32

标签: react-native

我想在React Native中实现一个“婴儿睡眠”应用程序,并且我在其中使用了短歌。

想多次播放一首歌曲而不会中断,但是一首歌曲会短暂播放

我的完整代码是:

  

构造函数(道具){           超级(道具);           this.state = {               1号,               selectMusic:1,               playMusic:1,           };       }

play(namePlayer1,namePlayer2){
    if(statePlay === true && nowPlay === true){
        musicPlayer1= new SoundPlayer(namePlayer1, SoundPlayer.MAIN_BUNDLE);
        musicPlayer2= new SoundPlayer(namePlayer2, SoundPlayer.MAIN_BUNDLE, (error) => {
            this.playM2();
        });}
}

playM1() {
    if(statePlay === true && nowPlay === true){
        musicPlayer1.play();
        let timeOutTimeInMiliSec = (musicPlayer1.getDuration() -1) * 1000;
        setTimeout(() => {
            this.playM2();
            musicPlayer2.stop();
        }, timeOutTimeInMiliSec);
    }
}

playM2() {
    if(statePlay === true && nowPlay === true){
        musicPlayer2.play();
        let timeOutTimeInMiliSec = (musicPlayer2.getDuration() -1) * 1000;
        setTimeout(() => {
            this.playM1();
            musicPlayer1.stop();
        }, timeOutTimeInMiliSec);
    }}


playNow(){
    this.setState({
        number: this.state.selectMusic
    });
    if(nowPlay === false)
    {
        statePlay=true;
        nowPlay=true;
        const namePlayer1='s_'+this.state.selectMusic+'.ogg';
        const namePlayer2='s_'+this.state.selectMusic+'_s.ogg';
        this.setState({number: this.state.selectMusic});
        this.play(namePlayer1,namePlayer2);

    } else {
        if(musicPlayer1) {
            musicPlayer1.pause();
            musicPlayer1.stop();
        }
        if(musicPlayer2) {
            musicPlayer2.pause();
            musicPlayer2.stop();
        }
        this.setState({number: this.state.selectMusic});
        if(this.state.number === this.state.selectMusic){
            statePlay=false;
            nowPlay=false;

        }else{
            statePlay=true;
            nowPlay=true;
            const namePlayer1='s_'+this.state.selectMusic+'.ogg';
            const namePlayer2='s_'+this.state.selectMusic+'_s.ogg';
            this.play(namePlayer1,namePlayer2);

        }
    }
}

感谢您的帮助

0 个答案:

没有答案