2012-01-31 308 views
0

我给我的整个下面的代码是否有帮助呢.....播放视频在iPhone模拟器与媒体播放器框架

#import "ContentViewController.h" 
#import "ContentViewController.h" 
#import "MediaPlayerViewController.h" 
#import "TwitterViewController.h" 
#import "YoutubeViewController.h" 

@implementation ContentViewController 

@synthesize imageView; 
@synthesize imageView1; 
@synthesize tableView; 
@synthesize navigationController; 
@synthesize toolBar; 
@synthesize item; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    } 
return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - XMLParser Delegate 

-(void)parseXMLFileAtURL:(NSString *)URL{ 

NSURL *xmlURL = [NSURL URLWithString:URL]; 
rssParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlURL]; 
[rssParser setDelegate:self]; 
[rssParser setShouldProcessNamespaces:NO]; 
[rssParser setShouldReportNamespacePrefixes:NO]; 
[rssParser setShouldResolveExternalEntities:NO]; 
[rssParser parse]; 
NSLog(@"Parsed"); 
} 

-(void)parserDidStartDocument:(NSXMLParser *)parser{ 

NSLog(@"Found file and started parsing"); 
} 

-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ 

NSString *errorString = [NSString stringWithFormat:@"Unable to download feed from website (Error Code %i)", [parseError code]]; 
NSLog(@"Error parsing xml: %@", errorString); 
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[errorAlert show]; 
} 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 

    currentElement = [elementName copy]; 
    if ([elementName isEqualToString:@"channel"]) { 

    rssElement  = [[NSMutableDictionary alloc]init]; 

    title   = [[NSMutableString alloc]init]; 
    link   = [[NSMutableString alloc]init]; 
    description  = [[NSMutableString alloc]init]; 
    copyright  = [[NSMutableString alloc]init]; 
    } 
} 


-(void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 

    if ([elementName isEqualToString:@"channel"]) { 

    [rssElement setObject:title     forKey:@"title"]; 
    [rssElement setObject:link     forKey:@"link"]; 
    [rssElement setObject:description   forKey:@"description"]; 
    [rssElement setObject:copyright    forKey:@"copyright"]; 

    [item addObject:[rssElement copy]]; 
    NSLog(@"adding stories %@", title); 
    } 
} 


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 

    if ([currentElement isEqualToString:@"title"]) { 
    [title appendString:string]; 
    }else if ([currentElement isEqualToString:@"link"]) { 
    [link appendString:string]; 
    }else if ([currentElement isEqualToString:@"description"]) { 
    [description appendString:string]; 
    }else if ([currentElement isEqualToString:@"copyright"]) { 
    [copyright appendString:string]; 
    } 
} 

-(void)parserDidEndDocument:(NSXMLParser *)parser{ 

NSLog(@"all done"); 
NSLog(@"item array has %d items", [item count]); 
[tableView reloadData]; 
NSLog(@"Finished Parsing"); 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

/* Add Segmented Controller */ 

if (segmentedControl == nil) { 
    segmentedControl = [[UISegmentedControl alloc] initWithItems: 
         [NSArray arrayWithObjects:@"Video", @"Images", @"Audio", nil]]; 
} 


[segmentedControl setFrame:CGRectMake(55.0, 47.0, 180.0, 31.0)]; 
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
[segmentedControl setWidth:70.0 forSegmentAtIndex:0]; 
[segmentedControl setWidth:70.0 forSegmentAtIndex:1]; 
[segmentedControl setWidth:70.0 forSegmentAtIndex:2]; 
[segmentedControl addTarget:self 
        action:@selector(segmentClick:) 
      forControlEvents:UIControlEventValueChanged]; 
[segmentedControl setMomentary:YES]; 

// [[[segmentedControl subviews]objectAtIndex:0]setTintColor:[UIColor blackColor]]; 

[self.view addSubview:segmentedControl]; 

/* Add Image View */ 

imageView1.image = [UIImage imageNamed:@"Harry.png"]; 

/* Add Page Control */ 

pageControl = [[UIPageControl alloc] init]; 
pageControl.frame = CGRectMake(120.0, 250.0, 100.0 ,10.0); 
pageControl.numberOfPages = 5; 
pageControl.currentPage = 0; 

[self.view addSubview:pageControl]; 

/* Customize Table View */ 

tableView.backgroundColor = [UIColor clearColor]; 
imageView.image = [UIImage imageNamed:@"gradientBackground.png"]; 

item = [[NSMutableArray alloc]init]; 

if ([item count] == 0) { 

    path = @"http://172.19.58.172/Android/GetApprovedList.php?ip=172.19.58.172&type=Video"; 
    [self parseXMLFileAtURL:path]; 
    [tableView reloadData]; 
    NSLog(@"Returned %@", item); 
} 

headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 270.0, 300.0, 14.0)]; 
headerLabel.text = @"Latest Videos"; 
headerLabel.textColor = [UIColor whiteColor]; 
headerLabel.backgroundColor = [UIColor clearColor]; 

[self.view addSubview:headerLabel]; 
} 

/* Assign control to Segment Controller */ 

-(void)segmentClick:(UISegmentedControl *)segmentControl { 

if (segmentControl.selectedSegmentIndex == 1){ 


    if ([item count] == 0) { 

     path = @"http://172.19.58.172/Android/GetApprovedList.php?ip=172.19.58.172&type=Image"; 
     [self parseXMLFileAtURL:path]; 
     [tableView reloadData]; 
     headerLabel.text = @"Latest Images"; 
     NSLog(@"Returned %@",item); 
    } 
} 
else if (segmentedControl.selectedSegmentIndex == 2){ 
    if ([item count] == 0) { 

     path = @"http://172.19.58.172/Android/GetApprovedList.php?ip=172.19.58.172&type=Audio"; 
     [self parseXMLFileAtURL:path]; 
     [tableView reloadData]; 
     headerLabel.text = @"Latest Audios"; 
     NSLog(@"Returned no items"); 
     //   [[[segmentedControl subviews]objectAtIndex:2]setTintColor:[UIColor blackColor]]; 
    } 
} 
else { 
    if ([item count] == 0) { 

     path = @"http://172.19.58.172/Android/GetApprovedList.php?ip=172.19.58.172&type=Video"; 
     [self parseXMLFileAtURL:path]; 
     [tableView reloadData]; 
     headerLabel.text = @"Latest Videos"; 
     NSLog(@"Returned %@", item); 
    }   
    //  [[[segmentedControl subviews]objectAtIndex:0]setTintColor:[UIColor blackColor]]; 
    } 
} 

#pragma mark Table View Data Source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"this is returned %@", item); 
return item.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 

int feed = [indexPath indexAtPosition:[indexPath length] - 1]; 
cell.textLabel.text = [[item objectAtIndex:feed]objectForKey:@"title"]; 
cell.detailTextLabel.text = [[item objectAtIndex:feed]objectForKey:@"description"]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

NSString *moviePath = [self.item objectAtIndex:indexPath.row]; 
NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; 
NSLog(@"Item has %@", movieURL); 
playerController = [[MPMoviePlayerController alloc]initWithContentURL:movieURL]; 
[playerController play]; 

// MediaPlayerViewController *mediaView = [[MediaPlayerViewController alloc]initWithNibName:@"MediaPlayerViewController" bundle:nil]; 
// [self presentModalViewController:mediaView animated:YES]; 

} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

    if (buttonIndex == 0) { 
    shareAlert = [[UIActionSheet alloc]initWithTitle:@"Share to" delegate:self cancelButtonTitle:@"Cancel"  
destructiveButtonTitle:@"Facebook" otherButtonTitles:@"Twitter", nil]; 
    [shareAlert showInView:self.view]; 
    } 
    else if (buttonIndex == 1){ 
    NSLog(@"button 2"); 
    } 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (buttonIndex == 1) { 
    TwitterViewController *twitterController = [[TwitterViewController alloc]initWithNibName:@"TwitterViewController" bundle:nil]; 
    [self presentModalViewController:twitterController animated:YES]; 

    } 
} 

-(void)moviePlayBackDidFinish:(NSNotification *)notification{ 
MPMoviePlayerController *moviePlayerController = [notification object]; 
[moviePlayerController.view removeFromSuperview]; 
[moviePlayerController release]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

更改didSelectRow方法类似以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

moviePath = [[item objectAtIndex:indexPath.row]objectForKey:@"link"]; 
NSLog(@"moviepath has %@", moviePath); 
movieURL = [NSURL URLWithString:moviePath]; 
NSLog(@"movieURL has %@", movieURL); 
viewController = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL]; 
[self presentMoviePlayerViewControllerAnimated:viewController]; 

viewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; 
[playerController play]; 
viewController = nil; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayBackDidFinish:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:viewController]; 

} 

- (void) moviePlayBackDidFinish:(NSNotification*)notification { 

viewController = [notification object]; 

[[NSNotificationCenter defaultCenter] removeObserver:self  
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:viewController]; 

if ([viewController respondsToSelector:@selector(setFullscreen:animated:)]) 
{ 
    [viewController.view removeFromSuperview]; 
} 

} 

现在,在选择任何行时会出现空白屏幕,并且应用程序停留在此处。视频不播放

在这方面的任何帮助表示赞赏。

在此先感谢。

+0

找到了与视频问题.....从PHP解析视频的名字是不正确....感谢所有为他们的帮助...... – 2012-02-02 05:38:25

回答

0

通过看你的代码我可以告诉你的是,你不必重新分配你的物品对象。

如果你在课堂上的某个地方分配你的item对象和存储在其中的值,那么,你在这里做不再次重新分配它:

item = [[NSMutableArray alloc]init]; 
NSString *moviePath = [item objectAtIndex:indexPath.row]; 

通过重新分配它,你被分配一个新的内存吧并且在新的记忆中不存在任何物体。

+0

我确实有同样的问题,即使不把这条线用于重新分配 – 2012-01-31 12:07:05

+0

你有没有在课程中的任何地方分配'item'对象,或者你只是在那个函数中做的 – 2012-01-31 12:08:51

+0

不.......我用它在不同的方法所以先前分配它......如果你需要整个代码我可以发布它......但它太长了 – 2012-01-31 12:15:50

0

您正在为您的阵列分配新内存:item = [[NSMutableArray alloc]init];。这意味着现在有NO个对象。并且您正试图通过NSString *moviePath = [item objectAtIndex:indexPath.row];访问它,在indexPath.row处没有对象,这就是为什么它崩溃。

+0

嗯,即使我没有在那里分配阵列,我面临同样的问题... – 2012-01-31 12:59:24

+0

@DipanjanDutta:它只是意味着数组超出界限! – Maulik 2012-01-31 13:03:40