我必须从服务器在AVPlayer中播放视频文件,但我还必须使用基本身份验证来播放此文件。这是我的代码

    NSMutableDictionary * headers = [NSMutableDictionary dictionary];
NSData *basicAuthCredentials = [[NSString stringWithFormat:@"username:password"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64AuthCredentials = [basicAuthCredentials base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
[headers setValue:[NSString stringWithFormat:@"Basic %@", base64AuthCredentials] forKey:@"Authorization"];
NSURL *videoURL = [NSURL URLWithString:fileUrlString];
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:videoURL options:headers];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];


AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
playerViewController.delegate = (id)self;
[player play];
[self presentViewController:playerViewController animated:YES completion:nil];

最佳答案

@ ankit-jain你快到了。将HTTP header 添加到AVURLAsset的正确方法如下所示:
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
当您这样做时,您的 header 将与请求一起使用。
请查看此答案以获取更多详细信息:https://stackoverflow.com/a/23713028/1306884

关于ios9 - 如何使用AVPlayer进行基本身份验证以播放来自服务器的视频文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38567347/

10-12 15:36