问题描述
我一直在使用简单的 AVPlayer
来播放加密的 HLS
媒体。
I have been working on a simple AVPlayer
to play encrypted HLS
media.
我使用 AVAssetResourceLoaderDelegate
来处理密钥检索过程,以便可以使用有效密钥播放加密媒体。
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.
这里是代码:
- (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];
}
经过一些调试后,我意识到委托方法在设备上从未调用过shouldWaitForLoadingOfRequestedResource
。
After some debugging, I realized that the delegate method shouldWaitForLoadingOfRequestedResource
was never called on device.
我已阅读其他相关问题:
I have read other relevant questions:
我尝试将所有代码包含在 dispatch_async
, dispatch_get_main_queue
块中,但没有运气解决我的问题。
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.
关于问题?
推荐答案
如果你看看Apple的例子在显示bipbop.m3u8 HLS播放的代码中,您将看到他们正在使用掩码进行真实的http请求:http:/host/bipbop.m3u8=>custom_scheme:/host/bipbop.m3u8
相同应该使用播放列表子资源制作技巧。
If you take a look on Apple example code where they show bipbop.m3u8 HLS playback you will see that they are using masks for real http requests: "http:/host/bipbop.m3u8" => "custom_scheme:/host/bipbop.m3u8"Same trick should be made with playlist subresources.
否则avplayer会忽略AVAssetResourceLoaderDelegate并直接加载数据。
Otherwise avplayer ignores AVAssetResourceLoaderDelegate and load data directly.
你需要实现某种映射:
NSString* videoUrl = @"fake_scheme://host/video.m3u8";
NSURL *streamURL = [NSURL URLWithString:videoUrl];
这篇关于AVAssetResourceLoaderDelegate方法不在设备上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!