如何阻止音乐在后台播放?(反应天然声)

Sin*_*met 5 android react-native react-native-android

我正在使用react-native-sound模块但是当我启动声音并按下Home按钮退出应用程序时,声音仍然在播放.

当我重新加载应用程序时,它会再次启动声音并且我无法使用.stop()它来销毁它,因为它只会破坏加载到第一个上面的第二个.

我该怎么办呢?

class Home extends Component {
  constructor(props) {
    super(props);
    this.props.actions.fetchScores();
  }

  componentWillReceiveProps(nextProps){
   nextProps.music.backgroundMusic.stop()

   setInterval( () => { 
    nextProps.music.backgroundMusic.play()
    nextProps.music.backgroundMusic.setNumberOfLoops(-1)
   }, 1000);
  }
}

export default connect(store => ({
    music: store.music
  })
)(Home);
Run Code Online (Sandbox Code Playgroud)

cod*_*ker 7

我认为您应该使用AppState在后台停止音乐并在应用程序状态再次更改时启用。

componentDidMount: function() {
  AppState.addEventListener('change', this._handleAppStateChange);
},
componentWillUnmount: function() {
  AppState.removeEventListener('change', this._handleAppStateChange);
},
_handleAppStateChange: function(currentAppState) {
  if(currentAppState == "background") {
      stop();
  } 
  if(currentAppState == "active") {
      resume();
  }
},
Run Code Online (Sandbox Code Playgroud)