Can anyone give me an idea how can we show or embed a YouTube video if we just have the URL or the Embed code?
Answers
As I can't find any way to load the rtsp URL in video view (for all devices & android versions), I solved my problem with another work around. I used a webview to embed the youtube player within it, and this method working nicely in all tested devices.
Here is my code:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("http://www.youtube.com/embed/" + videoID + "?autoplay=1&vq=small");
mWebView.setWebChromeClient(new WebChromeClient());
Thank you very much for all your help guys.
you can easily do this with the Youtube Player API
If you have a read through that document, you'll see it's pretty easy to have your own controls and extend the player.
Example
// Get element holding the video (embed or object)
var player = document.getElementById("myYouTubePlayer");
//Create a simple function and check if player exists
function play() {
if(player) {
player.playVideo();
}
}
Then in your HTML simply
<a href="#" onclick="play()">Play Youtube Video</a>
Using the Youtube IFrame API, you can do this easily.
The only part you need to configure here is the array of youtube IDs. You can retrieve those from the part after the /v/ in the URL (If need be, you can modify the javascript to load URLs instead of IDs. I just like this way better.
<div id="player"></div>
<script src="//www.youtube.com/iframe_api"></script>
<script>
/**
* Put your video IDs in this array
*/
var videoIDs = [
'OdT9z-JjtJk',
'NlXTv5Ondgs'
];
var player, currentVideoId = 0;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '350',
width: '425',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.loadVideoById(videoIDs[currentVideoId]);
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
currentVideoId++;
if (currentVideoId < videoIDs.length) {
player.loadVideoById(videoIDs[currentVideoId]);
}
}
}
</script>
It is possible to play the audio in the background. However , you are not allowed to do so.
This violation of the Youtube API is cause for immidiate removal from the play store.
Official answer:
During review, we found that your app violates the Prohibited Actions provision of the Content Policy.
We have determined that your app enables background playing of YouTube videos, which is a violation of the YouTube API Terms of Service: "Your API Client will not, and You will not encourage or create functionality for Your users or other third parties to: (8) separate, isolate, or modify the audio or video components of any YouTube audiovisual content made available through the YouTube API;"
You have to ask users to store the 11 character code from the youtube video.
For e.g. http://www.youtube.com/watch?v=Ahg6qcgoay4
The eleven character code is : Ahg6qcgoay4
You then take this code and place it in your database. Then wherever you want to place the youtube video in your page, load the character from the database and put the following code:-
e.g. for Ahg6qcgoay4 it will be :