react-native ios app webview 在屏幕关闭时静音播放音频

react-native ios app webview plays audio muted when screen turrned off

我构建了一个 react-native webview 应用程序,它在屏幕关闭时有音频,音频在 android 中继续播放,而在 ios 中关闭。 有什么解决办法吗?

请确保 Xcode 项目的 Background Modes 应启用。

如果未启用,请转到 Xcode 项目 -> Select 目标 -> 功能部分 -> 背景模式 -> 打开它。

然后 select Audio, AirPlay, and Picture in Picture.

代码:

import React, { Component } from 'react';
import {StyleSheet, View, Button} from 'react-native';
import SoundPlayer from 'react-native-sound-player';

export default class Touchables extends Component {
  constructor(props) {
    super(props);
  }

  playTrack() {
    try {
      SoundPlayer.playUrl('http://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3')
    } catch (e) {
      alert('Cannot play the file')
      console.log('cannot play the song file', e)
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <Button title="play me" onPress={this.playTrack} />
      </View>
   );
  }
}

const styles = StyleSheet.create({
  container: {
    marginTop: 100,
  },
});