1 安装
yarn add react-native-video
react-native link react-native-video
2 iOS后台使用
1 RN端
设置属性:
ignoreSilentSwitch={'ignore'}
playInBackground={true}
①#import <AVFoundation/AVFoundation.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
//注册远程控制事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; // allow
...
}
②创建一个MainViewController替代原来的root
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
MainViewController *rootViewController = [MainViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
③添加进入后台的方法
-(void)applicationDidEnterBackground:(UIApplication *)application{
[_window.rootViewController becomeFirstResponder];
}
④MainViewController.m中
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
#warning 这里只做演示--当RN端开始播放的时候发送事件到原生再调用此处的更新锁屏信息
[self updateLockedScreenMusic];
}
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
NSLog(@"暂停播放");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"下一曲");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"上一曲");
break;
default:
break;
}
}
}
- (BOOL)canBecomeFirstResponder{
return YES;
}
- (void)updateLockedScreenMusic{
// 1.播放信息中心
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
// 2.初始化播放信息
NSMutableDictionary *info = [NSMutableDictionary dictionary];
// 专辑名称
info[MPMediaItemPropertyAlbumTitle] = @"专辑名称";
// 歌手
info[MPMediaItemPropertyArtist] = @"歌手";
// 歌曲名称
info[MPMediaItemPropertyTitle] = @"歌曲名称";
// 设置持续时间(歌曲的总时间)
info[MPMediaItemPropertyPlaybackDuration] = @(360.0f);
// 设置当前播放进度
info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(100.0f);
// 3.切换播放信息
center.nowPlayingInfo = info;
}
我并没有做深入测试 这种方式的可行性,可能有其他的什么问题,这里只是提供这种可能的解决方案