Youtube Player API - onYouTubePlayerReady() is not executed - youtube-api

I am customising a video player using the youtube player javascript API:
<html>
<head>
</head>
<body>
<iframe src="http://www.youtube.com/v/M7lc1UVf-VE?version=3&enablejsapi=1" frameborder="0"></iframe>
<script type="text/javascript" charset="utf-8">
function onYouTubePlayerReady(){
console.log('done')
}
</script>
</body>
</html>
done is not printed in console, what am I missing?

You have to include the iframe javascript library; once that's injected, it will automatically call a function named onYouTubeIframeAPIReady ... within that function you will create a player object that you bind to your iframe, and it can have event listeners (such as notifying you when the player is ready). Soemthing like this:
<html>
<head>
</head>
<body>
<iframe id="player" src="http://www.youtube.com/v/M7lc1UVf-VE?version=3&enablejsapi=1" frameborder="0"></iframe>
<script type="text/javascript" charset="utf-8">
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onYouTubePlayerReady
}
});
}
function onYouTubePlayerReady(){
console.log('done');
}
</script>
</body>
</html>

Related

ui-router not working in cordova angular ios build

I have a problem while loading views using ui-router in cordova ios build. I'm using cordova angular in my application. The ui-router working fine in android build but while i'm running the app using cordova emulate ios the views not getting loaded.
here is my code looks like,
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Awesome material design app</title>
<link rel="stylesheet" href="node_modules/angular-material/angular-material.css">
<link rel="stylesheet" href="lib/ionic/css/ionicons.min.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/ngPercentDisplay.css">
<script src="node_modules/angular/angular.js"></script>
<script src="lib/angular-ui-router.min.js"></script>
<script src="node_modules/angular-aria/angular-aria.js"></script>
<script src="node_modules/angular-animate/angular-animate.js"> </script>
<script src="node_modules/angular-material/angular-material.js"></script>
<script src="node_modules/angular-local-storage/dist/angular-local-storage.js"></script>
<script src="lib/angular-touch.min.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script src="js/index.js"></script>
<script src="js/controller.js"></script>
<script src="js/router.js"></script>
</head>
<body ng-app="YourApp">
<div layout="column">
<ng-include src="'templates/partials/sidebar.html'" style="top:0px" ng-if ="lang =='ENGLISH'"></ng-include>
<ng-include src="'templates/partials/sidebar-right.html'" style="top:0px" ng-if ="lang =='ARABIC'"></ng-include>
<div ui-view></div>
</div>
</body>
</html>
The router.js file looks like below
app.config(['$urlRouterProvider', '$stateProvider','$compileProvider', function($urlRouterProvider, $stateProvider,$compileProvider) {
$urlRouterProvider.otherwise('/signup');
$stateProvider.state('signup', {
url:'/signup',
templateUrl: 'templates/sign-up.html',
controller: 'signupCtrl'
});
}]);
The index.js file looks like
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
controller.js looks like below,
'use strict';
var app = angular.module( 'YourApp', [ 'ngMaterial','ui.router']);
app.controller('myCtrl',['$scope',function ($scope) {
$scope.detail={};
$scope.detail.name="MyApp";
$scope.detail.desc="welcome to my app!";
}]);
Please help me if any one knows about this issue. Thanks in advance.

YouTube Live Stream RTMP URL

I have live event on YouTube and I'd like to broadcast it on my website. I want to make my event private, grab it's RTMP broadcasting URL and paste it on my website, in JWPlayer.
Is that possible?
Providing YouTube live URL to JWPlayer is not working. Already tried.
This should work for YouTube live with JW Player 6.12. Keep in mind the YouTube URL needs to be actually broadcasting for this to work:
<!DOCTYPE html>
<html>
<head>
<title>Live YouTube</title>
</head>
<body>
<script src="http://p.jwpcdn.com/6/12/jwplayer.js" type="text/javascript"></script>
<div id="player"></div>
<script type="text/javascript" language="javascript">
jwplayer("player").setup({
file: "//www.youtube.com/watch?v=U3dnUonOGOs",
primary: 'flash'
});
jwplayer().onPlay(function () {
VideoLength = jwplayer().getDuration() * 1000;
jwplayer().seek(jwplayer().getDuration());
jwplayer().getPosition() * 1000;
});
jwplayer().onPause(function () {
jwplayer().getPosition() * 1000;
});
jwplayer().onBuffer(function () {
jwplayer().getPosition() * 1000;
});
jwplayer().onComplete(function () {
jwplayer().getPosition() * 1000;
});
</script>
</body>
</html>

Autoplay Youtube video in full screen and then redirect

I am having trouble redirecting youtube video after it finished playing using Youtube api.I have css code for styling but that doesn't really matter in this case. Any hep would be appreciated. EDIT: I would like this to be responsive. Here is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<LINK href="style.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<div class="video-container">
<iframe src="http://www.youtube.com/embed/sOS9aOIXPEk?autoplay=1" frameborder="0" width="560" height="315"></iframe>
</div>
</HTML>
<div id="player"></div>
<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height=''
width=''
videoId: '0Bmhjf0rKe8',
playerVars:{'rel':0,'showinfo':1,'autoplay':1}
events: {
'onStateChange': onPlayerStateChange
}
});
}
// when video ends
function onPlayerStateChange(event) {
if(!event.data)
GoHome();
}
function GoHome(){
window.location = 'http://www.google.com';
}
</script>
When the video ends the event.data == 2. You also missed some comma's as separator of the object entries.
Here is a working example (Note: that the first iframe is commented-out):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<LINK href="style.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<!--
<div class="video-container">
<iframe src="http://www.youtube.com/embed/sOS9aOIXPEk?autoplay=1" frameborder="0" width="560" height="315"></iframe>
</div>
-->
<div id="player"></div>
<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '768',
width: '1024',
videoId: '0Bmhjf0rKe8',
playerVars:{'rel':0,'showinfo':1,'autoplay':1},
events: {
'onStateChange': onPlayerStateChange
}
});
}
// when video ends
function onPlayerStateChange(event) {
//alert (event.data);
if (event.data == 0)
GoHome();
}
function GoHome(){
window.location = 'http://www.google.com';
}
</script>
</BODY>
</HTML>

Youtube API - getAvailableQualityLevels return an empty array

i get an empty array when i call the youtube getAvailableQualityLevels() function, but why?
The video has many quality-levels like 360p 480p ... 720p
DEMO:
http://jsbin.com/eyexah/1/edit
YT API Doc https://developers.google.com/youtube/js_api_reference#GettingReference
CODE:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<script type="text/javascript" language="javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "youtube_player" };
swfobject.embedSWF('http://www.youtube.com/v/ZGx2WUbhnyM?enablejsapi=1&playerapiid=ytplayer&version=3',
'youtube',
'425',
'356',
'8',
null,
null,
params,
atts);
</script>
<script type="text/javascript" language="javascript">
function onYouTubePlayerReady()
{
console.log('# onYouTubePlayerReady');
var ytp = document.getElementById("youtube_player");
var getQuali = ytp.getAvailableQualityLevels();
console.log(getQuali);
};
</script>
</head>
<body>
<div id="youtube"></div>
</body>
</html>
This would work if the state would be playing (or 1).
add a listener for onStateChange events in your onYouTubePlayerReady function:
ytp.addEventListener('onStateChange', 'onPlayerStateChange');
and add the onPlayerStateChange function, for example:
function onPlayerStateChange(event)
{
var getQuali = ytp.getAvailableQualityLevels();
}
See a working example based on your code: http://jsbin.com/eyexah/6/edit
You can retrieve when the state is 'playing.'
Check onPlayerStateChange(event)

cordova 1.7 iOS audiorecord plugin

I've implemented the audio record plugin on IOS using phonegap/cordova 1.7. IOS5.1
I'm using it over the standard media.startrecord() function as I want to change the bitrate to reduce the size of the file.
It works fine in the simulator.
On the real iphone it records successfully once but subsequently refuses.. it seems to work but on playback I'm getting an error 4 MediaError.MEDIA_ERR_NONE_SUPPORTED
I've created a new project with just a bare bones record and playback and I'm still getting the same issue.
Here's the test code - It's very basic - just a couple of links to record two separate files and play them back. The setTimeout is there to record just a couple of seconds of audio each time.
I've googled til I've worn out my fingerprints but haven't found a resolution.
Any insight you can give would be very gratefully received.
I'm wondering if I'm failing to close out the recording/playback properly? Or are files referenced differently on the sim v the iPhone?
Many thanks!
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript">
var fs,mediaRec=null, recInterval,recordSettings = {"FormatID": "kAudioFormatULaw","SampleRate": 16000.0,"NumberOfChannels": 1,"LinearPCMBitDepth": 16};
function recfile1() {recordAudio("test.wav");}
function recfile2() {recordAudio("test2.wav");}
function success(){console.log("ok");}
function recordAudio(fname) {
fs.root.getFile(fname, {create: true, exclusive: false}, function(entry){
mediaRec = new Media(entry.fullPath, success, function(){alert("failed");});
mediaRec.startRecordWithSettings(recordSettings);
recInterval = setTimeout(function() {
mediaRec.stopRecordWithSettings();
}, 2000);
}, function(){console.log("error");});
}
function playfile(fname) {
var my_media;
fs.root.getFile(fname, {create: false, exclusive: false},
function success(entry) {
my_media = new Media(entry.fullPath,function(){console.log("ok");},function(err){alert(err.code+" "+err.message);});
my_media.play();
},
function() {
console.log("not found file");
}
);
}
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){fs=fileSystem;}, function(){console.log("failed");});
Media.prototype.startRecordWithSettings = function(options) {
Cordova.exec(null, null, "AudioRecord","startAudioRecord", [this.id, this.src, options]);
};
Media.prototype.stopRecordWithSettings = function() {
Cordova.exec(null, null, "AudioRecord","stopAudioRecord", [this.id, this.src]);
};
}
</script>
</head>
<body onload="onBodyLoad()">
<a onclick="playfile('test.wav');">play</a>
<a style='margin-top:100px;' onclick="recfile1();">record 1</a>
<a style='margin-top:100px;' onclick="recfile2();">record 2</a>
<a onclick="playfile('test2.wav');">play2</a>
</body>
</html>

Resources