我正在尝试使用WebView将Youtube播放器嵌入到我的Android应用中。我已经关注了几个例子(包括http://fancifulandroid.blogspot.com.es/2013/01/play-youtube-video-in-webview-without.html和YouTube Video not playing in WebView - Android),但无法让它发挥作用。当我加载我的活动时,youtube视频播放器显示正确的预览图像,但当我尝试播放时,加载图标会不断循环,我的日志中出现以下错误:
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
这是我的代码:
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);
}
我还在AndroidManifest.xml中设置了 android:hardwareAccelerated =“true”,并确认嵌入的url链接在我的浏览器中有效。 如何解决此问题?
答案 0 :(得分:1)
正如朱莉娅推荐的那样,我使用了YouTube播放器API。这就是我的最终代码:
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);
}