zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.79k stars 748 forks source link

setSpeakerphoneOn doesn't work #632

Open pappricot opened 4 years ago

pappricot commented 4 years ago

:beetle: Description setSpeakerphoneOn doesn't work

I looked up documentation for setSpeakerphoneOn at https://github.com/zmxv/react-native-sound/wiki/API, where it states that you just need to pass a boolean value into it. What I'm trying to achieve is to have a toggle button for switching on and off setSpeakerphoneOn for voicemail files :beetle: What is the observed behavior? Doesn't seem to work

:beetle: What is the expected behavior? Expected to have a toggle button for switching on and off setSpeakerphoneOn for voicemail files

:beetle: Please post your code: Posting snippets of my code:

...
  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>
}

:bulb: Does the problem have a test case?

:bulb: **Possible solution**

:bulb: Is there a workaround?

:bulb: If the bug is confirmed, would you be willing to create a pull request?

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?

brianomchugh commented 4 years ago

I just noticed the same thing testing on Android. On iOS, I previously used setPlaybackCategory to create a toggle, but that does not work on Android either.

I made sure to set the MODIFY_AUDIO_SETTINGS permission. When I press the speaker toggle, the sound does pause for a second, and the volume seems to change for a second-- but sound is playing both through the speaker and the earpiece either way.

pappricot commented 4 years ago

@brianomchugh What library did you use to get setPlaybackCategory? I checked on react-native-sound there's no such method..

brianomchugh commented 4 years ago

@pappricot Oops, I meant setCategory... the following works for me with iOS and react-native-sound@0.10.9 (old version). I am going to try an older version on Android to see if it does anything.

if (this.state.speaker) {
  this.sound.setCategory("PlayAndRecord")
} else {
  this.sound.setCategory("Playback")
}
brianomchugh commented 4 years ago

@pappricot I just noticed in your code you have true for both values of the toggle:

this.state.useEarPiece ? this.sound.setSpeakerphoneOn(true) : this.sound.setSpeakerphoneOn(true)
tuanbm commented 4 years ago

@pappricot any update on this?

avetisyan66 commented 1 year ago

any updates? I can't change the speakers output of on android!