【问题标题】:using json parser how to play video player in ios [closed]使用json解析器如何在ios中播放视频播放器[关闭]
【发布时间】:2015-01-11 12:40:36
【问题描述】:

我正在使用 MPMediaPlayer 框架在 iOS 中开发视频播放器。我正在使用 JSON 响应器从服务器端检索数据。我的问题是我想在加载视图时播放视频,下面是我的代码

请帮帮我。

- (void)viewDidLoad  {

    videoURL=[NSURL URLWithString:@"http://GetLiveVideoUrl"];
    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:videoURL];
    [urlRequest addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];

    videoWebData=[[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
    mp = [[MPMoviePlayerController alloc]initWithContentURL:strURL];
    mp.view.frame = self.view.bounds; //Set the size
    self.view.backgroundColor=[UIColor clearColor];
    mp.controlStyle = MPMovieControlStyleNone;
    mp.view.userInteractionEnabled = YES;
    mp.movieSourceType = MPMovieSourceTypeStreaming;
    [self.view addSubview:mp.view]; //Show the view
    mp.scalingMode = MPMovieScalingModeFill;
    [mp setFullscreen:YES animated:YES];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSError *error1;
    NSString *stringResponse=[[NSString alloc]initWithData:responseDataVideo encoding:NSUTF8StringEncoding];
    NSLog(@"String Resonse is :%@",stringResponse);

    videoDict=[NSJSONSerialization JSONObjectWithData:responseDataVideo options:NSJSONReadingAllowFragments error:&error1];
    NSLog(@"Dictionary is :%@",videoDict);

    strVideoUrl=[NSString stringWithFormat:@"%@",[videoDict valueForKey:@"GetLiveVideoUrlResult"]];
    NSLog(@"String URL is :%@",strVideoUrl);

    strURL=[NSURL URLWithString:strVideoUrl];
    NSLog(@"string url at player :%@",strURL);
}

【问题讨论】:

  • 请帮助我任何身体

标签: ios objective-c iphone json mpmovieplayercontroller


【解决方案1】:

编写代码viewDidAppear方法

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
}

【讨论】:

  • Suresh 请告诉我如何编写代码..请给我任何想法..
  • @MurthyChimalakonda 您想播放带有 URL 的视频或捆绑包中包含的视频?
  • 我想播放视频网址兄弟
【解决方案2】:

你有没有得到url,如果有,那么试试这样:

- (void)viewDidLoad {
    [super viewDidLoad];

    videoURL=[NSURL URLWithString:@"http://services.pawankalyan.tv/Service1.svc/GetLiveVideoUrl"];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:videoURL];        
    [urlRequest addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];        
    videoWebData=[[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    if (connection==videoWebData) {            
        responseDataVideo=[[NSMutableData alloc]init];            
        [responseDataVideo setLength:0];
    }
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection==videoWebData) {
        [responseDataVideo appendData:data];            
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSError *error1;
    NSString *stringResponse=[[NSString alloc]initWithData:responseDataVideo encoding:NSUTF8StringEncoding];        
    NSLog(@"String Resonse is :%@",stringResponse);

    videoDict=[NSJSONSerialization JSONObjectWithData:responseDataVideo options:
               NSJSONReadingMutableContainers error:&error1];
    NSLog(@"Dictionary is :%@",videoDict);

    strVideoUrl=[NSString stringWithFormat:@"%@",[videoDict valueForKey:@"GetLiveVideoUrlResult"]];
    NSLog(@"String URL is :%@",strVideoUrl);

    strURL=[NSURL URLWithString:strVideoUrl];
    NSLog(@"string url at player :%@",strURL);

    [self mediaplayermethod];
}

-(void)mediaplayermethod {

   // strURL make  gobal 
    mp = [[MPMoviePlayerController alloc]initWithContentURL: strURL];
    mp.view.frame = self.view.bounds; //Set the size
    self.view.backgroundColor=[UIColor clearColor];
    mp.controlStyle = MPMovieControlStyleDefault;
    [self.view addSubview:mp.view]; //Show the view
    mp.movieSourceType = MPMovieSourceTypeStreaming;
    mp.scalingMode = MPMovieScalingModeFill;
    [mp setFullscreen:YES animated:YES];
    mp.controlStyle=MPMovieControlStyleNone;
    [mp play];
}

【讨论】:

  • 运动它对我不起作用..请给我解决方案
  • 出现什么错误
  • 你有没有得到视频地址,先看看这个
  • 运动我没有收到任何错误。但是视频播放器不应该播放兄弟..我得到了网址
  • 你得到视频网址了吗,显示你的网址
【解决方案3】:

@MurthyChimalakonda 请按照以下代码进行操作

导入媒体播放器框架

#import <MediaPlayer/MediaPlayer.h>

获取 .xib 中的按钮,将以下代码写入 .h 文件中的 Play Button Action 并将操作赋予按钮

- (IBAction)playMovie:(id)sender;

在 .m 文件中为播放按钮操作编写以下代码

-(IBAction)playMovie:(id)sender
{
   NSURL *url = [NSURL URLWithString:
      URL];//write your URL

    _moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

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

    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:YES];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
      removeObserver:self
      name:MPMoviePlayerPlaybackDidFinishNotification
      object:player];

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

【讨论】:

  • 兄弟请检查我上面的代码。我正在使用 json 解析器..
  • 好的,我会在你的代码中做 strURL 是什么?
  • 那是什么?是要播放的完整视频 URL 还是您的应用正在直播?
  • 视频直播
猜你喜欢
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2015-01-31
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多