使用AVPlayer在ios8中无法播放视频

时间:2014-10-13 07:38:58

标签: objective-c xcode video ios8 avplayer

如果我在documents中运行应用程序,则directory ios8中存储的视频无法播放,但如果我在ios7中运行

,则效果很好

我正在使用documentsdirectory AFNetworking下载视频。以下是代码。

NSURLRequest *request = [NSURLRequest requestWithURL:urlVideo];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *path = [dataPath stringByAppendingPathComponent:[urlVideo lastPathComponent]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                self.isDownloadingComplete = YES;
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Video Downloading Failed");
            }];
            [operation start];

我正在播放使用video下载AVPlayer的内容。这是代码。

NSURL* playbackURL = [[NSURL alloc] initFileURLWithPath:strPath isDirectory:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[_pl currentItem]];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[_pl currentItem]];


AVAsset *asset = [AVURLAsset URLAssetWithURL:playbackURL options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
_pl = [AVPlayer playerWithPlayerItem:anItem];
_pl.volume = 1.0;
_pl.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_pl];
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
layer.frame = CGRectMake(0, 0, 305, 306);
[self.contentView.layer addSublayer: layer];
[_pl play];

我认为问题在于文档目录,因为我运行应用程序,文档路径每次都在变化,但我每次都发送正确的路径。以下是参考:http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/

1 个答案:

答案 0 :(得分:0)

我终于通过以下代码

解决了这个问题

我刚刚更换了

[self.contentView.layer addSublayer: layer];

[self.layer addSublayer:layer];

它工作正常,但我仍然没有得到[self.contentView.layer addSublayer: layer];

的问题

如果有人有强有力的证据,请告诉我。

由于