【问题标题】:How to play youtube videos in a native player and also how to download youtube videos如何在本机播放器中播放 youtube 视频以及如何下载 youtube 视频
【发布时间】:2014-08-27 20:14:46
【问题描述】:

我正在创建一个应用程序,因为我需要在本机播放器中播放 youtube 视频,并将 youtube 视频下载到应用程序中。请在这方面帮助我

【问题讨论】:

    标签: ios objective-c youtube-api


    【解决方案1】:
    NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:\'100%%\', height:'339px', videoId:\'%@\', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
    
    
    
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId];
    
    videoView = [[UIWebView alloc] initWithFrame:CGRectMake(12.0f, 247.0f, 743.0f, 339.0f)];
    videoView.backgroundColor = [UIColor clearColor];
    videoView.opaque = NO;
    videoView.scrollView.scrollEnabled=NO;
    
    // videoView.delegate = self;
    [self.view addSubview:videoView];
    
    videoView.mediaPlaybackRequiresUserAction = NO;
    
    [videoView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
    

    【讨论】:

      【解决方案2】:

      要获取网址/下载视频帮助,应在此处获得:Save Youtube video to iPhone in the app。

      如果您有直接视频网址,您可以使用原生 MPMoviePlayerController 进行流式传输。

      MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:youTubeDirectURL];
      
      [mp setControlStyle:MPMovieControlStyleFullscreen];
      [mp setMovieSourceType:MPMovieSourceTypeStreaming];
      [mp setFullscreen:YES];
      
      [self.view addSubview:mp.view];
      
      [mp prepareToPlay];
      [mp play];
      

      Apple MoviePlayer example

      【讨论】:

        猜你喜欢
        • 2014-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多