How to play youtube videos.

You must Login before you can answer or comment on any questions.

Hi, I am using Titanium to develop an application. Here in this application I want play videos from youtube. I was able to play videos of type .mp4 which are in the local path of the project. But when I try to play videos from remote I am failing to play. I want to play youtube videos also. I tried using MediaPlayer and using webview also. But I can`t play the videos. Below is my code using webview...

app.js:

var win = Ti.UI.createWindow({

backgroundColor : 'white',

exitOnClose : true,

layout : 'vertical',

orientationModes : [Ti.UI.PORTRAIT],

height : Ti.UI.FILL

});

var scrollView = Titanium.UI.createScrollView({

top : '10%',

width : 500,

height : 500,

scrollType : 'horizontal'

});

var baseHeight = 500;

var baseWidth = 500;

var img = Ti.UI.createImageView({ image : 'flower.jpg', height : baseHeight, width : baseWidth });

scrollView.add(img);

var button = Ti.UI.createButton({

top : '10%',

title : 'Click for Video',

width : Ti.UI.SIZE,

height : Ti.UI.SIZE

});

scrollView.addEventListener('pinch', function(e) {

if (e.scale >= 1) {

    var t = Ti.UI.create2DMatrix().scale(e.scale);

    img.transform = t;

    scrollView.showHorizontalScrollIndicator = true;

    scrollView.contentWidth = 500 * e.scale;

    scrollView.left = scrollView.left;

}

});

button.addEventListener('click', function() {

var win1 = Ti.UI.createWindow({

    backgroundColor : 'white',

    orientationModes : [Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT],

    fullscreen : true

});

var webVideo = Ti.UI.createWebView({

    width : Ti.UI.FILL,

    height : Ti.UI.FILL,

    url : 'video.html'

});

win1.add(webVideo);

win1.open();

});

win.add(scrollView);

win.add(button);

win.open();

video.html:

<html>

&lt;head&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;

        function tiPlay() {

            Titanium.App.fireEvent('htmlVideoStarted');

        }


        function tiPause() {

            Titanium.App.fireEvent('htmlVideoPaused');

        }

    &lt;/script&gt;

&lt;/head&gt;

&lt;body style=&quot;margin:0; padding: 0&quot;&gt;

    &lt;video id=&quot;viddlerVideo-87cd75f3&quot; src=&quot;http://www.youtube.com/watch?v=r1gojBA4ZUk&quot; type=

"video/mp4" width="320" height="180" poster="http://www.viddler.com/thumbnail/87cd75f3/" controls="con

trols" onPlay="tiPlay();" onPause="tiPause();"></video>

&lt;/body&gt;

</html>

But still I am unable to play videos. There is a youtube video url in video tag. Even I tried using MediaPlayer by specifying the same url to the url property of MediaPlayer. But no use. I want to play this video automatically when I click on the button and should close the video and should return to previous window once video gets completed. Can anyone share a possible solution.

Thanks in Advance

M.R.C.Prasad

1 Answer

— answered 2 years ago by Jigar Maheshwari
answer permalink
2 Comments
  • hi jigar,

    I tried the code in the link you given. But still it is not playing any video. Can you suggest the modifications in the code which I posted.

    — commented 2 years ago by Prasad M.R.C

  • hi jigar,

    Can you share any sample code that works fine in both android and ios platforms for accessing remote videos using media player of titanium.

    Thanks in Advance

    — commented 2 years ago by Prasad M.R.C

Your Answer

Think you can help? Login to answer this question!