保持其他应用程序的背景音频活跃,react-native-video IOS

Nat*_*sen 9 react-native react-native-ios react-native-video

长期倾听者第一次来电。

到目前为止,当我导航到使用react-native-video的屏幕时,我还没有成功地从Spotify之类的应用程序中获取背景音乐,以便在我的react-native应用程序中保持活力。在 Android 上,无需进行任何更改即可运行。我正在使用react-native(非expo),目前正在testflight上进行测试。

如果这是重复的,请告诉我,但我只找到了很多关于如何让你的应用程序在后台播放的文章(我的问题是相反的),而且我可能不只知道 IOS 用于此类控制的术语。值得注意的是,该组件的一个实例确实包含音频,但大多数呈现的视频都没有音频。

我试过了:

  1. 使用react-native-video 文档中的说明更新AppDelgate.m:

AppDelegate.m

    #import <AVFoundation/AVFoundation.h>  // import
    
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      ...
      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];  // allow
      ...
    }

Run Code Online (Sandbox Code Playgroud)
  • 这似乎没有效果
  1. 使用ignoreSilentSwitch="ignore"obey
  • Obey成功允许背景音频继续,但将有音频的视频静音。
  • 查看源代码,ignoreSilentSwitch=obey 的作用与我添加到 AppDelegate.m 文件中的作用相同,并且如果您使用 isPaused 属性,它也会被否定。
  • 尝试了许多其他涉及音频的道具,希望能幸运。
  1. 在 xcode 中的我的应用程序目标上添加了背景模式 =“音频、Airplay 和画中画”功能。

  2. 哭了一会儿,质疑我的人生选择。

我使用react-native-video-controls实现了react-native-video,但据我了解,它是具有附加功能的同一模块。这是实现:

<VideoContainer>
    <VideoPlayer
      source={currentVideo}
      navigator={null}
      ref={videoRef}
      resizeMode={'cover'}
      controlTimeout={1500}
      paused={isPaused}
      repeat={true}
      muted={true}
      tapAnywhereToPause={true}
      disableBack={true}
      disableTimer={true}
      disableVolume={true}
      disableFullscreen={true}
      showOnStart={true}
      hideShutterView={true}
    />
 </VideoContainer>
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助、建议、反馈或反驳!

小智 -1

我发现您缺少启用背景音频的道具。引用文档:

playInBackground 确定应用程序在后台时媒体是否应继续播放。这允许客户继续收听音频。

false(默认)- 不继续播放媒体

true - 继续播放媒体要在 iOS 上使用此功能,您必须:

在 Xcode 项目中启用背景音频 将ignoreSilentSwitch 属性设置为“忽略”平台:Android ExoPlayer、Android MediaPlayer、iOS