长期听众,第一次来电。
我试图在我的react-native应用中使用背景音乐(例如Spotify),但是当我导航到一个使用react-native-video的屏幕时,它经常会停止播放。在安卓上,不需要进行任何更改即可正常工作。我使用的是react-native(非expo)并且目前正在测试testflight。
如果这是重复的问题,请告诉我,但是我只能找到许多关于如何使应用程序在后台播放的文章(我的问题相反),而且我可能不知道IOS用于控制此类操作的术语。需要注意的是,该组件的其中一个实例包含音频,但是大多数呈现的视频没有音频。
我已经尝试过:
- 按照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
...
}
- 这似乎没有任何效果。
- 使用
ignoreSilentSwitch="ignore"
和obey
Obey
成功允许背景音频继续播放,但会静音那些带有音频的视频。- 查看源代码,ignoreSilentSwitch=obey 执行的操作与我添加到 AppDelegate.m 文件中的操作相同,如果您使用 isPaused 属性,也会被否定。
- 尝试了许多其他涉及音频的属性,希望能得到幸运的结果。
在 Xcode 中的我的应用目标上添加 "Audio、Airplay 和 Picture in Picture" 背景模式 = "音频" 能力。
略微哭泣并质疑我的人生选择。
我使用 react-native-video 和 react-native-video-controls 实现了它,但据我所知,它是具有附加功能的相同模块。下面是实现方式:
<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>
任何帮助、建议、反馈或反驳都非常感激!