【问题标题】:How can I play a Youtube video in an iPhone如何在 iPhone 中播放 Youtube 视频
【发布时间】:2011-04-28 12:29:32
【问题描述】:

我想从应用程序中在 iPhone 中播放 Youtube 视频。我尝试使用 MPMoviePlayerController,但没有加载视频。它正在播放我从捆绑包中加载的视频,而不是你的管视频。我还尝试通过将 Youtube 视频嵌入到 UIWebView 中来播放它,但这也是徒劳的。任何人都可以建议我一个我应该采取的方法。我在 IOS 4.2 中测试它

这是我使用的代码。

mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

  if ([mp respondsToSelector:@selector(loadState)]) 
  {
    // Set movie player layout
    [mp setControlStyle:MPMovieControlStyleFullscreen];
    [mp setFullscreen:YES];

        // May help to reduce latency
        [mp prepareToPlay];

        // Register that the load state changed (movie is ready)
        [[NSNotificationCenter defaultCenter] addObserver:self 
                       selector:@selector(moviePlayerLoadStateChanged:) 
                       name:MPMoviePlayerLoadStateDidChangeNotification 
                       object:nil];
    }  
  else
  {
    // Register to receive a notification when the movie is in memory and ready to play.
    [[NSNotificationCenter defaultCenter] addObserver:self 
                         selector:@selector(moviePreloadDidFinish:) 
                         name:MPMoviePlayerContentPreloadDidFinishNotification 
                         object:nil];
  }

  // Register to receive a notification when the movie has finished playing. 
  [[NSNotificationCenter defaultCenter] addObserver:self 
                        selector:@selector(moviePlayBackDidFinish:) 
                        name:MPMoviePlayerPlaybackDidFinishNotification 
                        object:nil];

【问题讨论】:

    标签: ios ios4 uiwebview media-player


    【解决方案1】:

    在 iOS 上播放 youtube 视频的唯一方法是使用默认的 youtube 播放器播放它们。您应该将视频嵌入到 uiwebview 中,当您点击/单击它时,默认播放器将打开,请参阅此代码以在 uiwebview 中嵌入视频-

    - (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
     NSString* embedHTML = @"\
        <html><head>\
     <style type=\"text/css\">\
     body {\
     background-color: transparent;\
     color: white;\
     }\
     </style>\
     </head><body style=\"margin:0\">\
        <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
     width=\"%0.0f\" height=\"%0.0f\"></embed>\
        </body></html>";
     NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
     if(videoView == nil) {
        videoView = [[UIWebView alloc] initWithFrame:frame];
        [self.view addSubview:videoView];
     }
     [videoView loadHTMLString:html baseURL:nil];
    }
    

    【讨论】:

    • 嗨,我试过这个方法。当我在 4.0 之前的版本上尝试它时,它正在工作。现在我在 Simulator 4.2 上尝试了这种方法,但没有用。它是否适用于 4.0 以上的任何 SDK,
    • 这种方法不适用于模拟器.. 它只能在 iphone 设备上使用
    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2012-08-25
    • 2011-05-29
    • 2011-11-29
    • 2011-04-25
    • 2013-07-28
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多