zmxv / react-native-sound

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

Preventing audio pausing upon unplugging headphones #619

Open humiston opened 4 years ago

humiston commented 4 years ago

:beetle: Description

Attempting to allow audio to resume when unplugging headphones

:beetle: What have you tried? Looking at the documentation, I was unable to find instructions related to controlling the functionality of audio playback upon unplugging headphones. There are release notes for version v0.10.5 that state "Pause/resume when I pull out the headphones or receive an incoming call.", but there appears to be no sort of config/api call that allows the developer to determine which of these functionalities (pause or resume) should be used.

:beetle: Please post your code:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

const Sound = require('react-native-sound');

Sound.setCategory('Playback');

var whoosh = new Sound('test.wav', Sound.MAIN_BUNDLE, (error) => {
  if (error) {
    console.log('failed to load the sound', error);
    return;
  }
  // loaded successfully
  console.log('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());

  // Play the sound with an onEnd callback
  // Note that if unplugging in the middle of play invocation, the callback will never be invoked due to audio pausing 
  whoosh.play((success) => {
    if (success) {
      console.log('successfully finished playing');
    } else {
      console.log('playback failed due to audio decoding errors');
    }
  });
  console.log('hello!');
});

const App: () => React$Node = () => {
  return (
    <View style={styles.topContainer}>
      <View style={styles.parentContainer}>
        <Text>{'Hello World!'}</Text>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  topContainer: {width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center'},
  parentContainer: { width: 250, height: 250, backgroundColor: 'pink'}
});

export default App;

:bulb: Possible solution

A possible solution would be to allow the user to determine this functionality either via configuration or through some sort of api call.

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?

schumannd commented 4 years ago

have you found a way to at least detect when audio is paused in this way?