当屏幕扭动时,react-native ios app webview播放音频静音

时间:2018-11-10 09:42:42

标签: android ios react-native react-native-ios

我构建了一个本机的webview应用程序,当屏幕关闭时,该应用程序会播放音频,而在ios中关闭时,音频会继续在android中播放。 有什么解决办法吗?

1 个答案:

答案 0 :(得分:1)

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

如果未启用,请转到Xcode项目->选择目标->功能部分->后台模式->开启它。

然后选择Audio, AirPlay, and Picture in Picture

enter image description here

代码:

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,
  },
});