Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've looked around for a while and this is what I came up with: It involves adding a youtube.html file to your project with the following code:</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;style&gt;body{margin:0px 0px 0px 0px;}&lt;/style&gt;&lt;/head&gt; &lt;body&gt; &lt;!-- 1. The &lt;iframe&gt; (and video player) will replace this &lt;div&gt; tag. --&gt; &lt;div id="player"&gt;&lt;/div&gt; &lt;script&gt; // 2. This code loads the IFrame Player API code asynchronously. var tag = document.createElement('script'); tag.src = "http://www.youtube.com/player_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // 3. This function creates an &lt;iframe&gt; (and YouTube player) // after the API code downloads. var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width: '640', height: '360', videoId: '%@', playerVars: {'autoplay' : 1, 'controls' : 0 , 'vq' : 'hd720', 'playsinline' : 1, 'showinfo' : 0, 'rel' : 0, 'enablejsapi' : 1, 'modestbranding' : 1}, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } // 4. The API will call this function when the video player is ready. function onPlayerReady(event) { event.target.playVideo(); } // 5. The API calls this function when the player's state changes. // The function indicates that when playing a video (state=1), // the player should play for six seconds and then stop. var done = false; function onPlayerStateChange(event) { if (event.data == YT.PlayerState.ENDED) { window.location = "callback:anything"; }; } function stopVideo() { player.stopVideo(); window.location = "callback:anything"; } function getTime() { return player.getCurrentTime(); } function getDuration() { return player.getDuration(); } function pause() { player.pauseVideo(); } function play() { player.playVideo(); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Also you have to create a UiWebView subclass that is also a delegate for UIWebViewDelegate</p> <pre><code>- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self == nil) return nil; self.mediaPlaybackRequiresUserAction = NO; self.delegate = self; self.allowsInlineMediaPlayback = TRUE; self.userInteractionEnabled = FALSE; return self; } - (void)loadVideo:(NSString *)videoId { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"YouTube" ofType:@"html"]; // if (filePath == nil) NSError *error; NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&amp;error]; // TODO: error check string = [NSString stringWithFormat:string, videoId]; NSData *htmlData = [string dataUsingEncoding:NSUTF8StringEncoding]; // if (htmlData == nil) NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube2.html"]; [htmlData writeToFile:targetPath atomically:YES]; [self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]]; File = 0; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ([[[request URL] scheme] isEqualToString:@"callback"]) { Playing = FALSE; NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube2.html"]; NSError *error; [[NSFileManager defaultManager] removeItemAtPath:targetPath error:&amp;error]; } return YES; } </code></pre> <p>Basically it creates a UIWebView and loads the code in the youtube.html file. Because the youtube.html is static and I need to load a certain id I create a copy on the fly in the documents folder youtube2.html in which I add the string id.</p> <p>The whole thing is that [self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]]; works but loading a NSUrlRequest from a string doesn't.</p> <p>The Javascript functions you see in the html file are for controlling the video. If you need access to time or full duration you can get them like this:</p> <pre><code>float VideoDuration = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getDuration()"] floatValue]; float VideoTime = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getTime()"] floatValue]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload