I have been working on a simple AVPlayer
to play encrypted HLS
media.
I am using the AVAssetResourceLoaderDelegate
to handle the key retrieving process so the encrypted media can be played with a valid key.
The program works perfectly on simulator, but it doesn't work at all on device.
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 shouldWaitForLoadingOfRequestedResource
was never called on device.
I have read other relevant questions:
AVAssetResourceLoaderDelegate not being called
AVAssetResourceLoaderDelegate - Only requests first two bytes?
and I tried enclosing all codes within a dispatch_async
, dispatch_get_main_queue
block but there's no luck on solving mine.
Currently my codes above are not enclosed by any dispatch queue blocks.
Any thoughts on the problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…