Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

ios - AVAssetResourceLoaderDelegate methods not working on device

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Otherwise avplayer ignores AVAssetResourceLoaderDelegate and load data directly.

You need to implement some kind of mapping:

NSString* videoUrl = @"fake_scheme://host/video.m3u8";
NSURL *streamURL = [NSURL URLWithString:videoUrl];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...