I want to be able to click on a tableview
item and play the video.so I wrote some of the code for video player.I am using MPMediaplayer
framework.I am using json parser
.Using json parser
to retrieve the data from webserver .My problem is if i click the tableview
item I recieve the following exception:
'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds for empty array'
*** 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
This is my code.
-(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];
}