gpt4 book ai didi

ios - 当应用程序在后台时,AVAssetReader 将无法正确初始化,因此无法开始阅读

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:13 28 4
gpt4 key购买 nike

在初始化 AVAssetReader 时,我注意到大约 20% 的时间它无法开始阅读。下面是代码片段,它在代码停止 20% 的地方结束。 'startReading' 调用返回 NO。

有人知道为什么会这样吗?我在这里错过了什么吗?我还应该注意到这段代码经常被执行。它可能是大约 10 个视频,一个接一个地按顺序处理。因此,大约 2 或 3 个视频将无法处理,因为“startReading”调用将返回 NO。


更新:通过更多测试,我发现当应用程序进入后台时它总是失败。因此,当应用程序处于后台时,AVAssetReader 无法“开始阅读”。有什么方法可以让它在应用程序处于后台时运行吗??

NSError *error = nil;
NSURL *srcVideo = [NSURL fileURLWithPath:[video getVideoLocation]];

NSDictionary *opts =
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:yes]
forKey:@"AVURLAssetPreferPreciseDurationAndTimingKey"];

AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:srcVideo options:opts];

// Get the video track but first check for existence of track
if( [[avAsset tracksWithMediaType:AVMediaTypeVideo] count] == 0 ) {
DLog(@"Error - no video tracks in asset");
return;
}

AVAssetTrack *videoTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

// Set up the reading of the video track
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset
error:&error];
NSDictionary *videoOptions =
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];

AVAssetReaderTrackOutput *assetReaderVideoOutput =
[[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack
outputSettings:videoOptions];

[reader addOutput:assetReaderVideoOutput];

if( ![reader startReading] ) {
DLog(@"Error - failed to start reading video");

DLog(@"Happens about 20% of time...");

return;
}

最佳答案

您可以请求时间在后台运行。 (已在 iOS8 上确认,也可能适用于更早的 iOS。)

UIBackgroundTaskIdentifier 添加一个属性。

@property (nonatomic) UIBackgroundTaskIdentifier bgIdentifier;

当您的应用进入后台时,开始后台任务。

self.bgIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^ {
NSLog(@"background task expired.");
[[UIApplication sharedApplication] endBackgroundTask:self.bgIdentifier];
self.bgIdentifier = UIBackgroundTaskInvalid;
}];

做一些工作。 (您的 Assets 阅读器现在应该在后台阅读)。

完成后结束任务,以便系统可以暂停您的应用。

[[UIApplication sharedApplication] endBackgroundTask:self.bgIdentifier];
self.bgIdentifier = UIBackgroundTaskInvalid;

请记住,使用此方法只会花几分钟的时间。

关于ios - 当应用程序在后台时,AVAssetReader 将无法正确初始化,因此无法开始阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17553624/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com