我必须从服务器在 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
关于swift - 如何使用 AVPlayer 进行基本身份验证来播放服务器上的视频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38567347/