我正在尝试从服务器下载视频,将其存储,然后使用AVPlayer
. 到目前为止,我所拥有的是:
- 我
AFNetworking
用来下载视频数据并将其存储NSData
在我NSDictionary
的资源中 - 我创建了我
TargetOverlayView
的显示资源内容(图像、文本、播放音频和...视频 - 希望很快) 当谈到显示视频时,我写了方法:
(void)playVideo:(NSData*)video{ NSLog(@"TargetOverlayView: playVideo"); if (!video) { NSLog(@"TargetOV: nothing to play"); [vPlayerLayer removeFromSuperlayer]; vPlayerLayer = nil; vPlayer = nil; return; } //save nsdata into file NSLog(@"{SAVE} saving video data into file..."); NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"video.3gp"]]; [fileManager createFileAtPath:filePath contents:video attributes:nil]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]]; vPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];//[AVPlayer playerWithPlayerItem:playerItem]; vPlayerLayer = [[AVPlayerLayer alloc] init];//[AVPlayerLayer playerLayerWithPlayer:vPlayer]; vPlayerLayer.player = vPlayer; [vPlayerLayer setFrame:self.frame]; [vPlayerLayer setBackgroundColor:[[UIColor purpleColor] CGColor]]; [self.layer addSublayer:vPlayerLayer]; [vPlayer addObserver:self forKeyPath:@"status" options:0 context:NULL]; }
我还添加了观察者,所以我知道什么时候播放它:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
if (vPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"video ready!");
[vPlayer play];
} else if (vPlayer.status == AVPlayerStatusFailed) {
NSLog(@"vide faield: %@", [vPlayer.error localizedDescription]);
}
}
}
我希望我可以从服务器下载 mp4 和 3gp 视频并播放它们,但只有当我给出这个:http ://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 Apple 给出的链接它工作......我无法播放下载的 3gp 数据filepath
(是否可以播放 3gp 或其他格式?)。有没有办法播放下载的视频?我不需要流媒体(至少现在不需要)。非常感谢每一个帮助。
编辑:我将有关制作文件的内容更改为:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"video.mp4"];
[video writeToFile:filePath atomically:YES];
仍然无法正常工作...
EDIT2:我在服务器上输入了此文件的链接 - 然后它工作(仅适用于 mp4),但我需要下载数据并从中播放视频,而不仅仅是链接到资源......