I worked on an easy AVPlayerto play encrypted media HLS.
I use AVAssetResourceLoaderDelegateto process the key extraction process so that the encrypted medium can play with a valid key.
The program works fine on the simulator, but it does not work on the device at all .
Here are the codes:
- (void) playUrlByAVPlayer:(NSString *) videoUrl
{
NSURL *streamURL = [NSURL URLWithString:videoUrl];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:streamURL options:nil];
[asset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[self.playerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:self.playerLayer];
[self.player play];
}
After some debugging, I realized that the delegate method was shouldWaitForLoadingOfRequestedResourcenever called on the device.
I read other relevant questions:
AVAssetResourceLoaderDelegate will not be called
AVAssetResourceLoaderDelegate - Request only the first two bytes?
I tried to include all of the codes in the unit dispatch_async, dispatch_get_main_queuebut will not be able to solve my problem.
.
?