我希望能够点击tableview
项目并播放 video.so 我为视频播放器编写了一些代码。我正在使用MPMediaplayer
框架。我正在使用json parser
。使用json parser
从网络服务器中检索数据。我的问题是,如果我点击tableview
项,我会收到以下异常:< / p>
&#39; NSRangeException&#39;,原因:&#39; * - [__ NSArrayM objectAtIndex:]:索引2超出空数组的范围&#39;
*** First throw call stack:
(0x1ba7012 0x15f6e7e 0x1b490b4 0x1c203a8 0x1ae59 0x813285 0x8134ed 0x121d5b3 0x1b66376 0x1b65e06 0x1b4da82 0x1b4cf44 0x1b4ce1b 0x249e7e3 0x249e668 0x763ffc 0xd66d 0x3a4770d 0x1)
libc++abi.dylib: terminate called throwing an exception
这是我的代码。
-(void)viewDidLoad
{
urlsArray=[NSMutableArray alloc]init];
}
-(void)loadData
{
url=[NSURL URLWithString:@"urls”];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&jsonError];
if ([jsonObject isKindOfClass:[NSArray class]])
{
}
else if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSArray *array=[[NSArray alloc]init];
array=[jsonDictionary objectForKey:@"video-urls"];
dataDictionary=[array objectAtIndex:0];
NSLog(@"%@",dataDictionary);
}
[urlsArray addObject:[dataDictionary objectForKey:@“Key1”]];
[urlsArray addObject:[dataDictionary objectForKey:@“Key2”]];
[urlsArray addObject:[dataDictionary objectForKey:@“Key3”]];
[urlsArray addObject:[dataDictionary objectForKey:@“Key4”]];
[urlsArray addObject:[dataDictionary objectForKey:@“Key6”]];
NSLog(@"%@",urlsArray);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// self.selectedIndex=indexPath.row;
currentUrlsArr=[urlsArray[indexPath.row]lastPathComponent];
NSString *documentDirectoryPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath=[documentDirectoryPath stringByAppendingPathComponent:currentUrlsArr];
NSURL *videoURL =[NSURL fileURLWithPath:filePath];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.fullscreen=TRUE;
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}