This probably caused by https://groups.google.com/group/youtube-api-gdata/browse_thread/thread/cba53817e843603d#
That being said, the way you're using the <iframe> API isn't
technically correct. You seem to be using a mashup of the approach
where an <iframe> is dynamically inserted on the page and the approach
where there's already an <iframe> element that. That's why you need to
do suspect things like set the onStateChange event listener in two
places.
If you already have an <iframe> element on the page, you can just
call new YT.Player('video-iframe') and not pass in anything as the
second parameter, and it will return a YT.Player object that is
associated with your existing <iframe> element. You could then listen
to events on that YT.Player, etc.
Cheers,
-Jeff Posnick, YouTube API Team
groups.google.com/group/youtube-api-gdata | apiblog.youtube.com |
@YouTubeDev
On Jan 19, 4:44 pm, nateguy <nat...@reflexionsdata.com> wrote:
> I'm trying to track the number of play clicks on an iframe embedded youtube
> video. This was going fine, until I tried to add a new video and discovered
> that it wasn't working at all... for any videos. Did something in the API
> change? Here's the code:
>
> var player;
> var hasTracked = false;
> var vframe = $('#video-iframe');
>
> if(typeof(YT) != 'undefined') {
> initiatePlayer();
> }
> else {
> var tag = document.createElement('script');
> tag.src = "https://www.youtube.com/player_api";
> var firstScriptTag = document.getElementsByTagName('script')[0];
> firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
> }
>
> function onYouTubePlayerAPIReady() {
> initiatePlayer();
> }
>
> function initiatePlayer() {
> console.log('player initiating')
> player = new YT.Player('video-iframe', {
> videoId: vframe.attr('videoid'),
> events: {
> 'onStateChange': onPlayerStateChange
> }
> });
> player.addEventListener('onStateChange', 'onPlayerStateChange');
> }
>
> function onPlayerStateChange(event) {
> console.log('it worked!')
> if(event.data == YT.PlayerState.PLAYING && !hasTracked) {
> hasTracked = true;
> }
> }
>
> And the iframe looks like this:
>
> <iframe width="295" height="230" id="video-iframe" videoid="u8MM2qq1GIV"
> trackinfo="click_goingback_play_3" src=
> "https://www.youtube.com/embed/u8MM2qq1GIV?rel=0&enablejsapi=1"
> frameborder="0"></iframe>
>
> The video plays, there are no visible errors, I get the 'player initiating'
> message, but I never get the 'it worked!' message when I hit play. I swear
> this used to work! Does anybody have any idea what's going on? Did the
> Youtube API change? It doesn't work in any browsers.
--
You received this message because you are subscribed to the Google Groups "YouTube APIs Developer Forum" group.
To post to this group, send email to youtube-api-gdata@googlegroups.com.
To unsubscribe from this group, send email to youtube-api-gdata+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/youtube-api-gdata?hl=en.
No comments:
Post a Comment