Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Android™ Development

Reply
Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2
Accepted Solution

in app Youtube video's

Hello,

I'm back again with another problem. Okay so this time my issue is with youtube video's. My app supports both android and fire os(kindle devices) from api level 9 to api level 19. I have Youtube video's that are displayed in my app primarily using the android youtube api. If and when the Youtube api cannot be used in circumstances where the Youtube app is not installed on a device (ex. Kindle) I replace the Youtube player view with a webview and then load the Youtube video in an iframe using the below code(edited)

 

private static String loadIframe(String videoId){
iframe = "<html><body bgcolor=\"#000\">";
iframe += "<iframe align=\"center\" width=\"100%\" height=\"315\" src=\"http://www.youtube.com/embed/" + videoId + "?rel=0&fs=0\" frameborder=\"0\" ></iframe>";
iframe += "</body></html>";
return iframe;
}

 

This approach works perfectly on all android devices with or without the Youtube app installed and it works on kindle devices as well, but fails with my playbook. When the app is run the device is correctly detected as a blackberry device and the Youtube player view is then replaced with the webview. But the video does not load and I'm greeted with the webview error message that the page could not be loaded. If I can't use this approach to show video's within my app what can I do? I really do not want to use an intent to open the Youtube video in a web browser because I want to keep my users within my app and to be honest I'm not even sure if I can use intents like this with Blackberry devices. How do you guys handle Youtube video's. Any advice?

Thanks

Highlighted
Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2

Re: in app Youtube video's

I should have known! Once again this is not a BlackBerry issue this is a gingerbread issue. Once I realised this I solved the problem by changing the line of code that I use to load the iframe into the webview from loadData() to loadDataWithBaseUrl(). Simple, now my video's play on android devices, kindle devices, and blackberry devices. Great!!
Contributor
Posts: 19
Registered: ‎04-16-2013
My Device: PlayBook
My Carrier: N/A

Re: in app Youtube video's

Is it still working for you? If yes, can you please post your code?

I'm trying to play a Youtube video in a WebView but it does not start.

Thanks!

 

Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2

Re: in app Youtube video's

I dropped the app that I used this piece of code in so I can't be sure if it is still working, but I don't see why it shouldn't as it was a solution that fixed a problem on older android based devices. I think what I posted is pretty straight forward and should give you all the information you need. Does the video load in the webview? And if so is the only problem that you are experiencing to do with actually starting the video? And how are you loading the data in the webview

Contributor
Posts: 19
Registered: ‎04-16-2013
My Device: PlayBook
My Carrier: N/A

Re: in app Youtube video's

Thanks for your reply.
The video loads, but when I tap the "play" button, it does not start. There is only a black screen with media control buttons at the bottom.

Here is my code to load the data in the WebView:

    webView = new WebView (this);
    setContentView (webView);
    webView.getSettings ().setJavaScriptEnabled (true);
    webView.setWebChromeClient (new WebChromeClient ());
    webView.setWebViewClient (new WebViewClient () {
      public boolean shouldOverrideUrlLoading (WebView view, String url) {
        view.loadUrl (url);
        return true;
      }
    });
    webView.getSettings ().setPluginState (PluginState.ON);
    webView.loadDataWithBaseURL (null, loadIframe ("NAg4qXsk99c"), "text/html", "UTF-8", null);

Any advice is welcome.

Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2

Re: in app Youtube video's

Sorry for not responding sooner, but I have been preoccupied throughout the day. So I found a version of my app with the webview used to display youtube video's and yes it still works(if you want to call it that) yes the video's are loaded and when the play button is pressed the video's play and all works as expected. The code that I use to display the webview is pretty much the same as yours except that I do not set a webview client and my webview is inflated from an xml layout, but nothing really significant.

 

youtubeIframe is the webview.

 

 

settings = youtubeIframe.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setPluginState(PluginState.ON);


youtubeIframe.setWebChromeClient(new WebChromeClient());


if (youtubeIframe != null){
youtubeIframe.loadDataWithBaseURL("", loadIframe(gdataList.get(0).getVidId()), "text/html", "utf-8", null);
}

 

So I do not think that your code is the issue but it is possible that you may not have set hardware acceleration to true in the manifest and from what I recall that was required.

Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2

Re: in app Youtube video's

Also if you or anybody else reading this post is using the method that I previously posted then you may want to edit it to make it more useful as what I posted was just to illustrate my problem, but really the method should be rewritten.

 

private static String loadIframe(String videoId){
iframe = "<html><body bgcolor=\"#000\">";
iframe += "<iframe align=\"center\" width=\"100%\" height=\"315\" src=\"http://www.youtube.com/embed/" + videoId + "?rel=0&fs=0\" frameborder=\"0\" ></iframe>";
iframe += "</body></html>";
return iframe;
}

 

to at the least something more like 

 

private static String loadIframe(String videoId, String height, String width, String alignment, String bgColor){
iframe = "<html><body bgcolor=\"#"+bgColor+"\">";
iframe += "<iframe align=\""+alignment+"\" width=\""+width+"\" height=\""+height"+\" src=\"http://www.youtube.com/embed/" + videoId + "?rel=0&fs=0\" frameborder=\"0\" ></iframe>";
iframe += "</body></html>";
return iframe;
}

 

and so on...........

Contributor
Posts: 19
Registered: ‎04-16-2013
My Device: PlayBook
My Carrier: N/A

Re: in app Youtube video's

Thanks for the details. I had already set android:hardwareAccelerated="true". I changed my code to be the same as yours, but unfortunately the video still doesn't start. I only get this screen:
http://cjoint.com/14au/DHCmzZ0AjST_capture.png

Contributor
Posts: 13
Registered: ‎01-03-2014
My Device: z10
My Carrier: O2

Re: in app Youtube video's

[ Edited ]

I put a quick project together just to test this piece of code. I tested on two devices a nexus7v2 (os version 4.4.2) and an lg l3 (os version 2.3.6). My playbook is packed away somewhere so I didnt test on a blackberry device but this is an android specific issue so the result should be the same dependant on the installed android runtime being used.

 

Anyway so the result was that on the nexus 7 running kit kat the code worked as expected, but on the lg l3 the thumbnail would load along with the video controls but upon clicking play icon the video would not load and the end time for the video would be displayed as a negative time value (ex. -140.00). 

 

The app that I used this code in was tested and known working on a nexus 7, nexus 7v2, nexus 5, playbook (with outdated os version), kindle fire 7" hd 3rd gen, and a few other devices. I was pretty sure that it was also working on the lg l3 but after the results that I previously mentioned I wasn't sure so I fixed the project and tested it. It works but thats because it uses the android youtube player api and not the webview on that device. I guess I never noticed before but I think this issue is related to a specific version of gingerbread(2.3.6 for certain) .

Contributor
Posts: 19
Registered: ‎04-16-2013
My Device: PlayBook
My Carrier: N/A

Re: in app Youtube video's

You have the same problem as I have: it works on Android version 4 but not version 2. I tested on 2.1 and 2.3.3 and it fails on both.
The Youtube Player API will not work on Blackberry, I guess.
So I'm now working on another solution, with a VideoView and the Android Youtube Player library, with some hacks to make it work.