如何在iPhone应用程序中播放视频的URL从服务器(How to play a video url

2019-09-18 09:19发布

其实,我用从服务器获取一个url XMLParser 。 我想在我的应用程序来播放此视频网址。
现在我用'的MPMoviePlayerController,但它不工作。 请帮忙!

XMLParser.m

-(id) loadXMLByURL:(NSString *)urlString
{
videoNames      =[[NSMutableArray alloc]init];

videos          = [[NSMutableArray alloc] init];
NSURL *url      = [NSURL URLWithString:urlString];
NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
parser          = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:   (NSDictionary *)attributeDict
 {
 if ([elementname isEqualToString:@"items"]) 
{

}


 if ([elementname isEqualToString:@"video"]) 
 {
     NSLog(@"video");
 currentVideo = [Video alloc];
  }   
}


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

if ([elementname isEqualToString:@"video"]) 
{

   currentNodeContent2= [[currentNodeContent2 stringByReplacingOccurrencesOfString:@"+"    withString:@" "]
                               stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[videos addObject:currentVideo];
currentVideo.content2 = currentNodeContent2;


}
   if ([elementname isEqualToString:@"name"]) 
{
     currentNodeContent2= [[currentNodeContent2 stringByReplacingOccurrencesOfString:@"+" withString:@" "]
                          stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [videoNames addObject:currentVideo];
    currentVideo.content2 = currentNodeContent2;
    NSLog(@"%@",currentNodeContent2);
   }
  }


// in my class.m

 -(void)playButtonPressed1:(UIButton*)sender
 {
  UIButton *btn1 = (UIButton *)sender;

Video *currentVideo= [[xmlParservideo videos] objectAtIndex:btn1.tag];
   NSString *urlString=currentVideo.content;

NSURL *playURL=[[NSURL alloc]initWithString:urlString];

self.player =[[MPMoviePlayerController alloc]initWithContentURL
              :playURL];
      player.view.frame = CGRectMake(0,80,340,360);
    [self.view addSubview:player.view];

self.player.movieSourceType = MPMovieSourceTypeUnknown;

[self.player prepareToPlay];

[self.player play];
 }

我的XML

  <items>
    <item>
      <pastor> Master Jojimon</pastor>  
       <video>
       http3A2F2Fvideo.google.com2Fgoogleplayer.swfFdocidD-675885207870876013326hl%3Den 
      </video>
      <name>Message by Master Jojimon</name>
        <lan>english</lan>

Answer 1:

它不工作之前视频字符串不是一个有效的URL。 履行其指向一个SWF Flash文件是不是视频。 和B在URL中的Safari只是打字表明它不是一个有效的地址。

如果你要处理的加载错误,我相信它会说,它不是一个有效的URL。



文章来源: How to play a video url from server in an iPhone app