3

Am trying to play YouTube and other videos by embedding it in the HTML source for displaying them in iOS.

Both <embed> and <object> technique works, but none of them are capable of calling callback functions when the video fails to play or if the URL passed to it is erroneous (and hence it could not play it).

I am using this HTML code with JavaScript for appropriate callbacks:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>video test</title>
<script type="text/javascript">
function errorEncountered()
{
    alert('Found error');
}
function loadingCompleted()
{
    alert("loading completed");
    //document.getElementById('youtubeObject').onerror="errorEncountered()";
    //alert(document.getElementById('youtubeObject'));
}
function clickCompleted()
{
    alert("Clicked on embedded object");
}
</script>
</head>
<body >
<div>
  <object
    id="youtubeObject"
    type="application/x-shockwave-flash"
    data="http://asdfasdf"
    width="643"
    height="400"
    onload="loadingCompleted()"
    onerror="errorEncountered()"
    onclick="clickCompleted()">
    <param name="movie" value="http://asdfasdf" />
    <param name="onError" value="errorEncountered()" />
    <!-- <param name="onload" value="loadingCompleted()" /> -->
    <param name="allowScriptAccess" value="always" />
    <param name="enablejsapi" value = "1" />
    <p>
    Couldn't load the video
    </p>
  </object>
</div>

<!-- document.getElementById('youtubeObject').onerror="errorEncountered()"; -->
<!-- document.getElementById('youtubeObject').addEventListener('onerror',function(){alert('error playing video')},true); -->

</body>
</html>

<!-- Actual Youtube URL "http://www.youtube.com/v/J_DV9b0x7v4" -->

Replacing the erroneous URL with the actual YouTube URL plays the video. But erroneous URL should trigger the JavaScript functions, that's not happening.

I have checked the documentation of <object> and the corresponding events. The <object> tag docs says that the events it responds to does not has onerror or onload. But on the other hand, the onerror event object docs state that it is supported by <img>, <object>, <script> & <style> tags.

So, I tried plugging in "onerror" event with both passing it as a <param> to the <object> tag so that the plugin would handle it and alternatively, also tried it as a attribute of <object> tag (just trying my luck). The "onclick" attribute of object does respond but not the "onerror" and "onload".

Though my question is, why doesn't the <param> passed to the plugin with onerror and onload would not trigger when a erroneous URL is passed to it?

I tried with embed tag, but there is no scope of attaching onerror and onload handlers using embed tag.

Is there a way to trigger JavaScript functions from within HTML (using embed or object technique) whenever video loading / playing fails?

PS: I have stumbled upon some links discussing this issue, but none of them end up with a solution, that's why I started this new question, and I am doing this for a iOS-specific case.

Detect <embed> tag failing to load video

http://www.webdeveloper.com/forum/showthread.php?t=590

Thanks for any suggestions & help.

Additions: The alternative content loading in the tags between <object> & </object> will display the content in iPad simulator, but not on the device itself. Even this is one of the puzzles I am finding hard to crack.

PS: I had already tried the method, for youtube specific I have tried adding the onError callback, but it never triggers! Have used the normal youtube specified way where we can plug in onError callback for youtube: https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player

Problem with the above is, we have to create a YT.Playerand set the videoID to the object. This would make it impossible to play videos from other sources. So used a slightly unconventional way:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>video test</title>
<script type="text/javascript">
function errorEncountered(event)
{
    alert('Found error');
}
function errorFound()
{
    alert('Found error');
}
</script>
<script type='text/javascript' src='http://www.sandeshkadur.com/wp-content/themes/Gaia/js/video.js?ver=1.0'></script>

</head>
<body id="mainBody">
<iframe id = "youtube_vid" src="http://player.vimeo.com/video/28723846?color=ffffff" frameborder="0" width="640" height="360"></iframe>

<!-- From https://groups.google.com/forum/#!msg/youtube-api-gdata/myLe-SdMZ6w/tziw7W2LBv4J -->
<!-- <iframe  id="youtube_vid" src="asdfasdf"></iframe> -->
<script>
var player = document.getElementById("youtube_vid");
new YT.Player(player, {
            events: {
                'onStateChange': function(){console.log(arguments)},
                'onError': errorEncountered
            }
        });
</script>
</body>
</html>

This though it plays the video properly, it dows not get error callbacks for Youtube links atleast!


Is it is mandatory to store both entities data when storing in core data?

I am creating a Core Data application where:

category entity has the following attributes

catid:int

catname:string

randomrelation:to-many rel to brandom entity

brandom is another entity having attributes

cid:category entity

no:int

arr:int

My code is the following

category *c=[NSEntityDescription insertNewObjectForEntityForName:@"category" inManagedObjectContext:context];

for (int i=0; i<[arrayofnumbers count]; i++) {
                
    brandom *r=[NSEntityDescription insertNewObjectForEntityForName:@"brandom" inManagedObjectContext:context];
    c.catid=[NSNumber numberWithInt:i];
    r.cid=c;
    r.no=[NSNumber numberWithInt:i+1];
    int objectatindex=[[arrayofnumbers objectAtIndex:i] intValue];
    NSLog(@"object at index:%i",objectatindex);
    r.arr=[NSNumber numberWithInt:objectatindex];
    [set addObject:r];
}

c.randomrelation=r;
NSLog(@"set element count=%i",[set count]);
if (![context save:&error]) {
    NSLog(@"%@",[error localizedDescription]);
}
4

2 回答 2

1

使用新的<iframe>嵌入代码:

<iframe width="420" height="315" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>

YouTube 将检测用户正在使用的平台并提供适当的代码,即适用于桌面用户的 Flash、<video>适用于 iOS 等的代码。

您可以通过单击“共享”按钮,然后单击“嵌入”按钮来找到此嵌入代码(在任何 YouTube 视频页面中)。


更新:不会找到可以在 iOS 设备上播放来自任何提供商的任何视频的通用解决方案。提供者必须专门对视频进行编码,以便可以由<video>Safari 中的元素播放。

我建议:

  • 查找您支持的每个视频网站的嵌入代码(不是 FLV 源 URL,即该网站提供给用户的嵌入代码)。

  • 如果该站点使用<iframe>,那么它们可能支持在 iOS 上播放。逐字使用此代码。

  • 如果该站点使用 Flash 播放器,请使用SWFObject来嵌入视频而不是其<embed>代码。如果 SWFObject 检测到浏览器不支持 Flash,它可以显示一些替代内容(例如“抱歉,您需要 Flash 才能观看此视频”之类的消息)。

  • 使用一个巨大的switch声明:

    switch (videoType):
    case 'YouTube':
        // use YouTube iframe code
        break;
    
    case 'Vimeo':
        // use Vimeo iframe code
        break;
    
    // ...on and on...
    
    default:
        // use SWFObject to embed
    }
    
于 2012-06-20T04:38:02.540 回答
1

您正在定义type="application/x-shockwave-flash"object tag但您在哪里定义iOS. 如果您尝试iOS专门针对目标,那么为什么不使用video标签。http://websitehelpers.com/video/html.html中提供的文档将帮助您在其中播放视频iOS因为iOS您需要定义video tag否则它无法理解,或者您可以使用任何一个flowplayer具有后备支持的Flash 播放器iOS. 您可以参考http://flash.flowplayer.org/plugins/javascript/ipad.html中的文档。尝试让我知道这是否可以解决您的问题

更新 :

试试下面的<iframe id="frame" title="Youtube Video" width="320" height="194" scrolling="no" src='http://www.youtube.com/embed/J_DV9b0x7v4' frameborder="1"></iframe>。这desktopipad. J_DV9b0x7v4是您要播放的视频。检查来自http://jsfiddle.net/AAUgG/的演示iOS。这应该可以解决您想要的目的。

于 2012-06-14T12:09:47.480 回答