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

Posted

技术标签:

【中文标题】如何知道 youtube 视频何时开始播放以及何时在 ios 的 web 视图中按下 mpmovieplayer 的完成按钮【英文标题】:how to know when the youtube video starts playing and when the done button pressed of mpmovieplayer in a webview in ios 【发布时间】:2014-09-05 12:57:00 【问题描述】:

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

particularVideo videoInfo = (particularVideo) 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】:

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

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

 -(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];




【讨论】:

以上是关于如何知道 youtube 视频何时开始播放以及何时在 ios 的 web 视图中按下 mpmovieplayer 的完成按钮的主要内容,如果未能解决你的问题,请参考以下文章

Youtube API如何在视频结束时从0秒开始播放视频?框架,Javascript

如何通过 youtube API 判断 youtube 视频的内​​容何时发生变化?

如何检测 VideoView 何时开始播放(Android)?

检测 webview 视频何时在 ios8 上变为全屏

使用 Youtube 播放器库关闭视频

如何检测 AVPlayer 视频何时结束播放?