Ajo*_*uve 6 objective-c ios airplay react-native
我正在构建一个 React Native 应用程序,以便能够将我的手机内容流式传输到本地 NodeJS 服务器。
在 android 上,它很好用,MediaProjectionManager但在 iOS 上,这更复杂。
我试着用RPScreenRecorder,这是我的代码
#import "ScreenShare.h"
@implementation ScreenShare
RCT_EXPORT_MODULE();
- (NSString *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
CIContext *temporaryContext = [CIContext contextWithOptions:nil];
CGImageRef videoImage = [temporaryContext
createCGImage:ciImage
fromRect:CGRectMake(0, 0,
CVPixelBufferGetWidth(imageBuffer),
CVPixelBufferGetHeight(imageBuffer))];
UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
NSString *base64String = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
CGImageRelease(videoImage);
return (base64String);
}
- (NSArray<NSString *> *)supportedEvents
{
return @[@"ImageCaptured"];
}
RCT_REMAP_METHOD(start,
startWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
[result setObject:@true forKey:@"success"];
if (@available(iOS 11.0, *)) {
if([RPScreenRecorder.sharedRecorder isRecording]) {
return resolve(result);
}
[RPScreenRecorder.sharedRecorder startCaptureWithHandler:^(CMSampleBufferRef _Nonnull sampleBuffer, RPSampleBufferType bufferType, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{
if(bufferType == RPSampleBufferTypeVideo) {
NSString *strEncoded = [self imageFromSampleBuffer:sampleBuffer];
[self sendEventWithName:@"ImageCaptured" body:@{@"image": strEncoded}];
}
});
} completionHandler:^(NSError * _Nullable error) {
if(error == NULL) return resolve(result);
// The user declined application recording
if([error code] == -5801) {
return reject(@"401", [error localizedDescription], error);
}
reject([NSString stringWithFormat:@"%ld", [error code]], [error localizedDescription], error);
}];
} else {
NSError * error = [NSError errorWithDomain:@"com.xxx.ConnectApp" code:426 userInfo:nil];
reject([NSString stringWithFormat:@"%ld", [error code]], @"Failed to start screen capture", error);
};
}
RCT_REMAP_METHOD(stop,
stopWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
[result setObject:@true forKey:@"success"];
if (@available(iOS 11.0, *)) {
[RPScreenRecorder.sharedRecorder stopCaptureWithHandler:^(NSError * _Nullable error) {
if(error == NULL) return resolve(result);
reject([NSString stringWithFormat:@"%ld", [error code]], [error localizedDescription], error);
}];
} else {
NSError * error = [NSError errorWithDomain:@"com.xxx.ConnectApp" code:426 userInfo:nil];
reject([NSString stringWithFormat:@"%ld", [error code]], @"Failed to stop screen capture", error);
}
}
@end
Run Code Online (Sandbox Code Playgroud)
视频质量不是很好,当我离开应用程序时它会停止。该应用程序的目标是能够在应用程序之外而非应用程序之外进行流式传输。
我研究了另一种在我的 NodeJS 本地服务器上创建 airplay 服务器的解决方案,但我找不到任何文档,我得到的唯一文档或模块很旧并且无法工作。
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |