如何知道youtube视频何时开始播放以及在ios的webview中按下mpmovieplayer的完成按钮的时间

时间:2014-09-05 12:57:00

标签: ios uiwebview mpmovieplayercontroller

我正在使用以下方法播放youtube的视频。当用户按下视频时,它会在默认的ios mpmovieplayer中播放视频。我需要知道视频何时开始播放以及按下mpmovieplayer完成按钮的时间。任何形式的帮助将不胜感激。

特定视频 videoInfo =(specialVideo )responseObj;

    self.videoTitleLabel.text = videoInfo.videoTitle;



    //NSString *vidUrl = [videoInfo.videoURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    CGSize scrSize = [[UIScreen mainScreen] bounds].size;//appDel.window.frame.size;

    float imgSclToX = scrSize.width/16.0;

    int imgWidth = (int) (scrSize.width -18);
    int imgHeight = (int) ((float)(9.0)*imgSclToX);

    //        DebugLog(@"Yo-----> Window: %@ Width: %i Height: %i", NSStringFromCGSize(scrSize), imgWidth, imgHeight);

    NSString *htmlStr = @"";
    //        htmlStr = [htmlStr stringByAppendingFormat:@"<iframe width=\"%i\" height=\"%i\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>", imgWidth, imgHeight, vidUrl];

    NSString *tStr = videoInfo.videoURL;
    tStr = [tStr substringFromIndex:([tStr rangeOfString:@"v="].location+2)];

    htmlStr = [NSString stringWithFormat:@"<iframe  type=\"application/x-shockwave-flash\" width=\"%i\" height=\"%i\" src=\"https://www.youtube.com/embed/%@\" frameborder=\"0\" allowfullscreen></iframe>", imgWidth, imgHeight, tStr];

    NSString *embedHTML = [NSString stringWithFormat:@"<html><head></head><body>%@<p><b>Artist:</b> %@<br><b>Composer:</b>%@<p>%@</p></body></html>", htmlStr, videoInfo.videoArtist, videoInfo.videoCompositor, videoInfo.videoDescription];
    [self.myWebview loadHTMLString: embedHTML baseURL: nil];

1 个答案:

答案 0 :(得分:0)

试试这个......我在我的项目中使用了这个......

它会告诉您已经缓冲了足够的数据以供播放以继续不间断。

 -(void)movieLoadStateDidChange:(id)sender{
        if(MPMovieLoadStatePlaythroughOK ) {
            NSLog(@"State is Playable OK");
            NSLog(@"Enough data has been buffered for playback to continue uninterrupted..");


    }

您需要使用 NSNotificationCenter

- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"VIEW DID LOAD");
    // Register to receive a notification that the movie is now in memory and ready to play
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieLoadStateDidChange:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];

    self.movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
    [[ self.movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view addSubview: [ self.movieplayer view]];
    [ self.movieplayer setShouldAutoplay:YES];
    [ self.movieplayer prepareToPlay];
    [ self.movieplayer play];
    [self.view insertSubview:self. self.movieplayer.view belowSubview:self.indicator];



}