AVQueuePlayer
アプリで動画のリストを再生するために使用しています。AVQueuePlayer
ビデオを毎回ダウンロードする必要がないように、再生されるビデオをキャッシュしようとしています。
そのため、ビデオの再生が終了したら、AVPlayerItem をディスクに保存して、次回はローカル URL で初期化しようとします。
私は2つのアプローチでこれを達成しようとしました:
- AVAssetExportSession を使用する
- AVAssetReader と AVAssetWriter を使用します。
1) AVAssetExportSession アプローチ
このアプローチは次のように機能します。
AVPlayerItem
が を使用して演奏を終了するのを観察しAVPlayerItemDidPlayToEndTimeNotification
ます。- 上記の通知を受け取ると (ビデオの再生が終了したため、完全にダウンロードされます)、
AVAssetExportSession
そのビデオをディスク内のファイルにエクスポートするために使用します。
コード:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
それから
- (void)videoDidFinishPlaying:(NSNotification*)note
{
AVPlayerItem *itemToSave = [note object];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:itemToSave.asset presetName:AVAssetExportPresetHighestQuality];
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [NSURL fileURLWithPath:@"/path/to/Documents/video.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch(exportSession.status){
case AVAssetExportSessionStatusExporting:
NSLog(@"Exporting...");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export completed, wohooo!!");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"Waiting...");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Failed with error: %@", exportSession.error);
break;
}
}
そのコードのコンソールでの結果は次のとおりです。
Failed with error: Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x98916a0 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x99ddd10 "The operation couldn’t be completed. (OSStatus error -12109.)", NSLocalizedFailureReason=An unknown error occurred (-12109)}
2) AVAssetReader、AVAssetWriter アプローチ
コード:
- (void)savePlayerItem:(AVPlayerItem*)item
{
NSError *assetReaderError = nil;
AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:assetToCache error:&assetReaderError];
//(algorithm continues)
}
AVAssetReader
次の情報を使用して割り当て/初期化しようとすると、そのコードは例外をスローします。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReader initWithAsset:error:] Cannot initialize an instance of AVAssetReader with an asset at non-local URL 'https://someserver.com/video1.mp4''
***
どんな助けでも大歓迎です。