我正在使用Xcode 7并且具有快速查看功能我遇到了此错误。它说无法加载" fileURL"这是我的代码 //这里因为下面的行@file被附加到我的fileURL的末尾,就像这样 -
@"http:/example.com/some_clips/SHAINA%20NC%20NEWS%20X%20180516%201657PM.mp4 -- file:///" see here something file:/// in the end so because of this i am unable to play video.
如果我使用initWithString并直接提供URL,那么它没有给出错误请建议我在我创建的下面设置我的播放器的URL,请在代码中阅读评论。有没有人对此有所了解。
NSString *localfilepath=[NSString stringWithFormat:@"%@",[managedObject valueForKey:@"clip_path"]]; //here i am getting my URL path upto .mp4 but after below line word -- file gets attached to it because of fileURLWithPath method don't know how to convert that
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath]; //suggest me this line of code so that i can pass URL to the player here this method is not working
// create a player
player = [AVPlayer playerWithURL:fileURL]; //here this fileURL which is going to player is going with that file:/// so giving error and no video runs
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
[player pause];
答案 0 :(得分:0)
您应该使用:
NSURL *fileURL = [NSURL URLWithString:localfilepath];
而不是:
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath];
fileURLWithPath:方法用于创建本地文件(位于设备本身的文件)网址。
答案 1 :(得分:0)
我已经成功了:)我找到答案:)这是将视频播放到服务器上的AVPlayer的完整代码
//这里我使用核心数据管理对象获取从服务器下载的所有URL
NSString *localfilepath=[NSString stringWithFormat:@"%@",[managedObject valueForKey:@"clip_path"]];
//然后在下面的行我将它编码为NSUTF8StringEncoding格式 不要忘记这样做,否则视频将无法播放,因为它不是我的情况
NSURL *url = [NSURL URLWithString:[localfilepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//及以下我已经为玩家分配了网址并创建了播放器
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
//player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];