如何在本机播放器中播放YouTube视频以及如何下载YouTube视频

问题描述 投票:0回答:2

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

ios objective-c youtube-api
2个回答
0
投票
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]];

0
投票

要获取网址/下载,应在此处提供视频帮助: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

© www.soinside.com 2019 - 2024. All rights reserved.