2011-02-25 57 views
27

嵌入YouTube視頻,我通過一個片段,我在網上找到的嵌入來自YouTube的視頻,在這裏是我使用的代碼:

@interface FirstViewController (Private) 
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame; 
@end 


@implementation FirstViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self embedYouTube:@"http://www.youtube.com/watch?v=l3Iwh5hqbyE" frame:CGRectMake(20, 20, 100, 100)]; 
} 


- (void)embedYouTube:(NSString *)urlString 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, urlString, frame.size.width, frame.size.height]; 
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; 
    [videoView loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:videoView]; 
    [videoView release]; 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

它正確編譯,當我打開它,我看到一個不正確的白色框,代碼創建。我有一個關於這兩個問題:

  1. 我怎麼知道,如果視頻會玩,這只是一個純白色的盒子,從YouTube確實模擬器播放視頻嗎?

  2. 我該如何擺放這個盒子?

+0

代碼真棒位,乾杯! – Shawson 2012-06-10 12:00:06

+0

此代碼是否適用於iOS 6? – Dee 2013-03-07 11:18:45

回答

13

剛剛測試過你的代碼,它在iPhone上工作正常,Youtube視頻雖然不支持在iOS模擬器上,所以你需要一個真正的設備進行測試。

我該如何擺放這個盒子?

您已經傳遞箱的X(20),Y(20),寬度(100)和高度(100)在該行:

[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)]; 

要改變的位置事後查看,修改其物業:

videoView.center = CGPointMake(200, 100); 
+0

只是一個側面說明這一點,如果你這樣做的模式來看,視頻結束後,我的應用程序行爲異常,關閉模式的看法和UIButtons是反應遲鈍。一旦我將其從模式視圖中拉出,它就可以正常工作。 – steve 2012-04-14 02:52:08

+0

您好我有同樣的問題,現在我成功了在設備上播放視頻,但存在這樣的視頻在IOS版本5.1.1只打了另外一個問題,如果我想在下面的ios版本5.1.1播放視頻什麼,我應該怎麼辦? – Birju 2012-09-18 07:32:10

+0

嗨,我正在使用上面的代碼播放視頻。它適用於播放視頻,但當我暫停視頻回到應用程序,然後它崩潰。給EXC_BAD_ACESS。對此有任何想法。 – 2013-09-30 06:04:06

0

對於使用MonoTouch的和C#的,你可以將該類添加到您的項目,並用它來查看您的視頻:

public class YouTubeViewer : UIWebView 
{ 
    public YouTubeViewer(string url, RectangleF frame) 
    { 
     string youTubeVideoHTML = @"<object width=""{1}"" height=""{2}""><param name=""movie"" 
          value=""{0}""></param><embed 
          src=""{0}"" type=""application/x-shockwave-flash"" 
          width=""{1}"" height=""{2}""</embed></object>"; 

     string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height); 
     this.LoadHtmlString(html, null); 
     this.Frame = frame; 
    } 
} 

確保您傳遞的URL是您點擊Share按鈕時從Youtube提供的嵌入代碼的URL。 (並檢查複選框舊代碼)

+0

問題是針對iOS – NSCoder 2015-02-08 09:33:03

+2

@NSCoder,這個答案是爲iOS,MonoTouch的是一個名爲Xamarin由公司提供的軟件,可以讓原生iOS應用程序開發C#,而不是現在不推薦使用的Objective-C。 (Swift) – TChadwick 2015-02-10 18:43:22

5

下面是在iOS 6中,並用新的YouTube嵌入代碼兼容版本:

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame { 
    NSString *html = [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color: transparent;color: white;}</style></head><body style='margin:0'><iframe width='%f' height='%f' src='%@' frameborder='0' allowfullscreen></iframe></body></html>", frame.size.width, frame.size.height, urlString]; 
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; 
    [videoView loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:videoView]; 
} 
+0

你可以自動播放電影加載到webview後? – 2013-03-27 12:20:46

+0

我不再使用此代碼,所以我不確定。你試過了嗎?如果沒有自動播放,我不會感到驚訝。我只是現在加載一個外部的MP4視頻,它的效果很好。 – adriandz 2013-03-28 01:03:41

7

下面是示例代碼,其在iOS工作正常5和更高版本,這是Youtube API提供的新實現,myWeb在這裏是一個UIWebView。 showinfo=0在URL中將刪除videoView中的頂部標題。

NSString *html = [NSString stringWithFormat:@"<html><body><iframe class=\"youtube-player\" type=\"text/html\" width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/q1091sWVCMI?HD=1;rel=0;showinfo=0\" allowfullscreen frameborder=\"0\" rel=nofollow></iframe></body></html>",frame.size.width,frame.size.height]; 
[myWeb loadHTMLString:html baseURL:nil]; 

希望這有助於你:)

+0

只有這個答案適合我。 – 2013-09-24 06:17:37

0

迅速實施:

let webView = UIWebView(frame: self.view.frame) // Position your player frame here 

self.view.addSubview(webView) 
self.view.bringSubviewToFront(webView) 

// Playback options: play in-line and start playing immediately 
webView.allowsInlineMediaPlayback = true 
webView.mediaPlaybackRequiresUserAction = false 

// Set the id of the video you want to play 
let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg 

// Set up your player 
let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>" 

// Load the HTML into your UIWebView 
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL) 

另外,YouTube爲開發者建立在他們的iOS版的YouTube播放其樹立UIWebView爲一個偉大的helper class您。

0

迅速4

func embedYouTube(_ urlString: String, frame: CGRect) {

let html = "<html><head><style type='text/css'>body {background-color: transparent;color: white;}</style></head><body style='margin:0'><iframe width='\(frame.size.width)' height='\(frame.size.height)' src='\(urlString)' frameborder='0' allowfullscreen></iframe></body></html>" 
    let videoView = UIWebView(frame: frame) 
    videoView.loadHTMLString(html, baseURL: nil) 
    view.addSubview(videoView) 
}