2017-08-02 9 views
3

私はネイティブ・リアクト・ネイティブ・サウンドに反応します。を使用してオーディオ・プレーヤーを作成していますが、今は問題が発生しています。私の問題は、現在の演奏曲を止めて次の曲を演奏したいと思うことです。これをどうやって行うのか分かりません。私のコードは以下の通りです:現在の再生曲を停止して次の曲を再生するにはどうしたらいいですか?

私はReact、React NativeおよびReact Native Soundを使用しています。

export default class PlaySound extends Component { 

    constructor(props) { 
    super(props); 

    this.state = { 
     stop: false, 
     playing: false 
    }; 

    this.stopTrack =() => { 
    if(!this.state.playing){ 
     return; 
    } 
     this.setState({ stop: false }); 
     this.state.playing.stop(); 
    } 
    } 

    renderTrackButton(track){ 
    if(this.state.stop){ 
     return (
     <Button danger onPress={() => this.stopTrack()}> 
      <Text>Stop</Text> 
     </Button> 
    ); 
    }else{ 
     return (
     <Button onPress={() => this.playTrack(track)}> 
      <Text>Play</Text> 
     </Button> 
    ); 
    } 
    } 


    playTrack = (track) => { 
    const callback =(error, sound) => { 
     if(error) throw error; 
     this.setState({ stop: true, playing: sound }); 
     sound.play(); 
    } 
    const sound = new Sound(track.url, error => callback(error, sound)); 
    } 

    render() { 
    console.log(this.state); 
    return (
     <Container> 
     <Header> 
      <Text style={{ color:'white', fontWeight:'bold', marginTop:15 }}>Play Sound Demo</Text> 
      <Right> 
      <Icon name="heart" /> 
      </Right> 
     </Header> 
     <Content> 
      {tracks.map((track) => (
      <Card key={track.chinese}> 
       <CardItem> 
       <Body> 
        <Text>{track.pinyin}</Text> 
        <Text>{track.chinese}</Text> 
       </Body> 
       <Right> 
        {this.renderTrackButton(track)} 
       </Right> 
       </CardItem> 
      </Card> 
     ))} 
     </Content> 
     </Container> 
    ); 
    } 
} 

AppRegistry.registerComponent('PlaySound',() => PlaySound); 

答えて

0

ここでは、バックグラウンドサポートとメディアコントロールを使用して、オーディオストリームを再生するネイティブの反応モジュールの完全な実例を見つけることができます。何かを学ぶ最善の方法は、良い例です。https://github.com/tlenclos/react-native-audio-streaming

+0

また、https://github.com/luminos-software/react-native-play-soundを試すこともできます。 –

関連する問題