React-Native-Sound:setSpeakerphoneOn不起作用

时间:2019-11-21 22:09:54

标签: react-native-sound

我在https://github.com/zmxv/react-native-sound/wiki/API查找了setSpeakerphoneOn的文档,其中指出您只需要向其中传递一个布尔值。我要实现的目标是有一个切换按钮,用于打开和关闭语音邮件文件的setSpeakerphoneOn。

这是我的代码段:

...
  play = async () => {
    if (this.sound) {
      this.sound.play(this.playComplete)
      this.setState({playState: 'playing'})
    } else {
      const { id, userCredential, contentUrl } = this.props
      const voiceMailURL = `${contentUrl}/content/voicemail/get?msgId=${id}&token=${userCredential.access_token}`
      this.sound = new Sound(voiceMailURL, '', (error) => {
        if (error) {
          Alert.alert('Notice', 'audio file error. (Error code : 1)')
          this.setState({playState: 'paused'})
        } else {
          this.setState({playState: 'playing', duration: this.sound.getDuration()})
          this.state.useEarPiece ? this.sound.setSpeakerphoneOn(true) : this.sound.setSpeakerphoneOn(true)
          this.sound.play(this.playComplete)
        }
      })
    }
  }
  playComplete = (success) => {
    if (this.sound) {
      if (success) {
      } else {
        Alert.alert('Notice', 'audio file error. (Error code : 2)')
      }
      this.setState({playState: 'paused', playSeconds: 0})
      this.sound.setCurrentTime(0)
    }
  }
 toggleUseEarPiece = () => {
    this.setState({
      useEarPiece: !this.state.useEarPiece
    })
  }
render () {
...
  <View style={{flexDirection: 'row', justifyContent: 'center'}}>
                        {this.state.playState === 'playing' &&
                        <TouchableOpacity style={{marginHorizontal: 20}} onPress={this.pause}>
                          <Icon color='#000' name='ios-pause' size={20} type='ionicon' />
                        </TouchableOpacity>}
                        {this.state.playState === 'paused' &&
                        <TouchableOpacity style={{marginHorizontal: 20}} onPress={this.play}>
                          <Icon color='#000' name='md-play' size={20} type='ionicon' />
                        </TouchableOpacity>}
                      </View>
...
<TouchableOpacity onPress={this.toggleUseEarPiece}>{speaker}</TouchableOpacity>
}

1 个答案:

答案 0 :(得分:0)

您要咯咯地笑,但是代码中的这一行可能有错:

this.state.useEarPiece ? this.sound.setSpeakerphoneOn(true) : this.sound.setSpeakerphoneOn(true);
相关问题