1

I'm trying to embed a Youtube player into my Android app using a WebView. I've followed several examples (including http://fancifulandroid.blogspot.com.es/2013/01/play-youtube-video-in-webview-without.html and YouTube Video not playing in WebView - Android) but cannot get it to work. When I load my activity, the youtube video player shows up with the correct preview image, but when I attempt to play it, the loading icon cycles continuously, and I get the following error in my logs:

12-07 19:23:18.484: D/HTML5VideoInline(26166): HTML5VideoInline created
12-07 19:23:18.484: V/MediaPlayer-JNI(26166): native_setup
12-07 19:23:18.484: V/MediaPlayer(26166): constructor
12-07 19:23:18.484: V/MediaPlayer(26166): setListener
12-07 19:23:18.484: V/MediaPlayer(26166): setVideoSurfaceTexture
12-07 19:23:18.484: V/MediaPlayer-JNI(26166): reset
12-07 19:23:18.484: V/MediaPlayer(26166): reset
12-07 19:23:18.484: I/MediaPlayer(26166): path is null
12-07 19:23:18.484: D/MediaPlayer(26166): Couldn't open file on client side, trying server side
12-07 19:23:18.494: V/MediaPlayer(26166): setVideoSurfaceTexture
12-07 19:23:18.494: V/MediaPlayer(26166): prepareAsync
12-07 19:23:18.754: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:18.789: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:18.849: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:18.879: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:18.929: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:18.994: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:19.029: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:19.114: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566
12-07 19:23:19.134: E/Web Console(26166): Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1:566

Here is my code:

  if(mediaURL.toLowerCase().contains("youtube")){
                     Log.d(TAG, "adding youtube video");
                     final String mimeType = "text/html";
                     final String encoding = "UTF-8";
                     String youtube_url = "<iframe class=\"youtube-player\" "
                             + "style=\"border: 0; width: 100%; height: 95%;"
                             + "padding:0px; margin:0px\" "
                             + "id=\"ytplayer\" type=\"text/html\" "
                             + "src=\"" + mediaURL
                             + "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
                             + "controls onclick=\"this.play()\">\n" + "</iframe>\n";
                     Log.d(TAG, "youtube_url: " + youtube_url);
                     final WebView video = new WebView(MediaPreview.this);
                     video.setWebChromeClient(new WebChromeClient() {
                     });
                     video.loadDataWithBaseURL("", youtube_url, mimeType, encoding, "");
                     video.getSettings().setJavaScriptEnabled(true);
                     video.getSettings().setPluginState(PluginState.ON);
                     video.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                     mediaHolder.addView(video);

                 }

I've also set android:hardwareAccelerated="true" in my AndroidManifest.xml and confirmed that the embed url link works in my browser. How can I solve this problem?

Community
  • 1
  • 1
scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • Why don't you use the YouTube Player API for android? Then you can use a native player. Download the package and pick a class of the sample code you want to have in your app: https://developers.google.com/youtube/android/player – Julia Hexen Dec 08 '13 at 01:05
  • I'm trying to embed this player in an activity that already extends a different class...I don't want to create a separate activity just for the youtube player (which requires that the activity extend the YouTubeBaseActivity) – scientiffic Dec 08 '13 at 01:57
  • 1
    You can also use a Fragment and extend from another Activity like in the VideoWallDemoActivity. – Julia Hexen Dec 08 '13 at 02:19

1 Answers1

1

As Julia recommended, I went with the YouTube Player API. This is what my final code looked like:

public class MediaPreview extends Main implements YouTubePlayer.OnInitializedListener {

....

if(mediaURL.toLowerCase().contains("youtube")){
     playerFragment = YouTubePlayerFragment.newInstance();
     playerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
     playerView = new FrameLayout(this);
     playerView.setId(R.id.player_view);
     mediaHolder.addView(playerView);
     getFragmentManager().beginTransaction().add(R.id.player_view, playerFragment).commit();                
}

....

    @Override
    public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
            boolean arg2) {
        Log.d(TAG, "adding youtube video");
        // get ID of youtube video from mediaURL
        String videoID = mediaURL.substring(mediaURL.lastIndexOf("/")+1, mediaURL.length());
        player.cueVideo(videoID);
    }
scientiffic
  • 9,045
  • 18
  • 76
  • 149