简体   繁体   English

使用 react-native-camera 在 React Native 中的 iOS 中录制视频时无法播放音频

[英]Unable to play audio while recording a video in iOS in React Native using react-native-camera

I want to play an audio that is being played through a url using react-native-sound , at the same time I want to record a video so that I can capture the audio being played through the speakers of the phone in the video.我想使用react-native-sound播放通过 url 播放的音频,同时我想录制一个视频,以便我可以捕获视频中通过手机扬声器播放的音频。 I'm able to achieve this in android, but iOS is giving me a hard time.我能够在 android 中实现这一点,但 iOS 给我带来了困难。

I've already asked this on the official react-native-camera github repo, but no response yet.我已经在官方 react-native-camera github repo 上问过这个问题,但还没有回应。

To Do First先做

  • [ YES ] Did you try latest release? [ 是 ] 您是否尝试过最新版本?
  • [ YES ] Did you try master? [是] 你试过大师吗?
  • [ YES ] Did you look for existing matching issues? [ 是 ] 您是否在寻找现有的匹配问题?

Platforms平台

No issue faced in Android在 Android 中没有遇到任何问题

It happens only in iOS.它只发生在 iOS 中。

Versions版本

  • Android: Used different versions of android, no issues faced Android:使用不同版本的 android,没有遇到任何问题
  • iOS: IOS:
  • react-native-camera: 3.44.3反应原生相机:3.44.3
  • react-native: 0.64.2反应原生:0.64.2
  • react: 17.0.1反应:17.0.1

Description/Current Behaviour描述/当前行为

This happens with iOS only, it's working fine in Android.这只发生在 iOS 上,它在 Android 上运行良好。 I want to play an audio while recording a video so that the audio playing on the phone's speaker can be captured through the microphone that is being used by the camera.我想在录制视频时播放音频,以便可以通过相机正在使用的麦克风捕获手机扬声器上播放的音频。 I play the audio and as soon as I call the recordAsync() function, the audio stops and the recording starts, at first I though that it's impossible to do this in iOS due to different permission policies in iOS as compared to Android, but I was provided with an native iOS version of the app by the client that is doing the same thing, so permissions is not an issue.我播放音频,一旦我调用 recordAsync() 函数,音频就会停止并开始录制,起初我认为由于与 Android 相比,iOS 中的权限策略不同,因此在 iOS 中无法执行此操作,但是我由执行相同操作的客户端提供了该应用程序的本机 iOS 版本,因此权限不是问题。

Expected Behavior Expected to allow the audio to keep playing while I record the video预期行为允许在我录制视频时继续播放音频

Steps to Reproduce重现步骤

Try playing any audio from a file or a URL using react-native-sound and call recordAsync() from react-native-camera,尝试使用react-native-sound播放文件或 URL 中的任何音频,并从 react-native-camera 调用 recordAsync(),

Additionals附加费

I'm using react-native-sound for playing the audio, as the audio is played through a URL, This is my RNCamera Component with props:我正在使用react-native-sound来播放音频,因为音频是通过 URL 播放的,这是我的带有道具的 RNCamera 组件:

    <RNCamera
          style={styles.preview}
          type={RNCamera.Constants.Type.back}
          flashMode={RNCamera.Constants.FlashMode.on}
          captureAudio={true}
          androidCameraPermissionOptions={{
            title: 'Permission to use camera',
            message: 'We need your permission to use your camera',
            buttonPositive: 'Ok',
            buttonNegative: 'Cancel',
          }}
          androidRecordAudioPermissionOptions={{
            title: 'Permission to use audio recording',
            message: 'We need your permission to use your audio',
            buttonPositive: 'Ok',
            buttonNegative: 'Cancel',
          }}>
          {({camera, status, recordAudioPermissionStatus}) => {
            if (showFilenameModal) {
             // show a modal to save the video file with a user entered name
            }
            if (status !== 'READY')
              return (
                <View
                  style={{
                    flex: 1,
                    width: globalStyles.WIDTH,
                    backgroundColor: 'black',
                    justifyContent: 'center',
                    alignItems: 'center',
                  }}>
                  <Text>Waiting</Text>
                </View>
              );
            return recording || processing ? (
              <IconButton
                title={'Stop'}
                icon={'stop'}
                disabled={processing}
                onPress={() => {
                  console.log('Stop Recording');
                  camera.stopRecording();
                }}
              />
            ) : (
              <View
                style={{
                  display: 'flex',
                  flexDirection: 'row',
                  justifyContent: 'space-around',
                  marginBottom: 10,
                }}>
                  {/* Button to record video without playing the audio */}
                <IconButton
                  title={'Record'}
                  icon="record"
                  onPress={() => this.startRecording(camera, false)}
                />
                  {/* Button to record video while playing the audio */}
                <IconButton
                  title={'Record with Audio'}
                  icon="record"
                  showSound={true}
                  onPress={() => this.startRecording(camera)}
                /> 
              </View>
            );
          }}
        </RNCamera>

This is how I start the recording:这是我开始录制的方式:

startRecording = async function (camera, withAudio = true) {
    const {audioPlayer, setVideoStatus} = this.props;
    audioPlayer.stopAudio();
    setVideoStatus({
      recording: true,
      processing: false,
      uri: null,
    });

    if (withAudio) audioPlayer.playAudio();

    const video = await camera.recordAsync({
      quality: RNCamera.Constants.VideoQuality['720p'],
      // I've tried this with different flag combination i.e:
      // captureAudio: true / false,
      // keepAudioSession: true / false,
    });

    console.log(video.uri.substr(7, video.uri.length), video.codec);

    audioPlayer.stopAudio();
    setVideoStatus({
      recording: false,
      processing: true,
    });

    this.setState({
      showFilenameModal: true,
      uri: video.uri,
    });
  };

This is my info.plist这是我的 info.plist

  <key>NSCameraUsageDescription</key>
  <string>Allow app to open camera to record video</string>
  <key>NSPhotoLibraryAddUsageDescription</key>
  <string>Allow app to save videos to library</string>
  <key>NSAppleMusicUsageDescription</key>
  <string>Allow app to use media library for audio</string>
  <key>NSLocationAlwaysUsageDescription</key>
  <string>Allow app to use location for getting current state</string>
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>Allow app to use location for getting current state</string>
  <key>NSMicrophoneUsageDescription</key>
  <string>Allow app to use microphone to record video with audio</string>

Just in case these are the permissions in my AndoroidManifest.xml:以防万一这些是我的 AndoroidManifest.xml 中的权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The other app that I'm talking about, is not based on react native, I think it's native iOS.我正在谈论的另一个应用程序不是基于 react native 的,我认为它是原生的 iOS。

Thanks to the react-native-camera community, I was able to solve this issue:感谢 react-native-camera 社区,我能够解决这个问题:

https://github.com/react-native-camera/react-native-camera/issues/3276 https://github.com/react-native-camera/react-native-camera/issues/3276

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM