How to play sound when the screen is locked on Android in React Native?

I have a simple application that after rendering plays sound in a few seconds.

Everything works, except that if I lock the screen, the application will not play sound until I unlock it again.

Here is a simplified version of what I'm working with:

import React, { Component, PropTypes } from 'react';
import { View, Text } from 'react-native';

import Sound from 'react-native-sound'; //for the bell sound

class SoundTest extends Component {

  componenWillMount(){
    setTimeout(this.playSound(), 5000);
  }

  playSound() {
      let s = new Sound('bell.wav','', (e) => {
        if (e) {
          console.log('error', e);
        } else {
          console.log('duration', s.getDuration());
          s.play();
        }
      });
  }

  render() {
    return (
      <View>
        <Text>Hear some sound after a few!</Text>
      </View>
    )
  }
}

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

// Works great if the screen is on / not locked

Is there a way to play sound whether the screen is locked or not?

+4
source share

Source: https://habr.com/ru/post/1658025/


All Articles