2014-05-09 57 views
0

我想通過視頻網址將YouTube視頻嵌入到我的Android活動中。這是我迄今所做的:活動中的視頻流

import android.net.Uri; 
import android.os.Bundle; 
import android.widget.MediaController; 
import android.widget.VideoView; 
import android.app.Activity; 

public class MainActivity extends Activity { 

String videoUrl= "http://www.youtube.com/watch?v=oSD0YigRW3o"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

VideoView videoView = (VideoView) findViewById(R.id.videoview); 
//Use a media controller so that you can scroll the video contents 
//and also to pause, start the video. 
MediaController mediaController = new MediaController(this); 
mediaController.setAnchorView(videoView); 
videoView.setMediaController(mediaController); 
videoView.setVideoURI(Uri.parse(videoUrl)); 
videoView.start(); 

} } 

,這是我的.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:text="Video Test.." 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <VideoView 
    android:id="@+id/videoview" 
    android:layout_width="fill_parent" 
    android:layout_height="161dp" 
    android:layout_weight="0.34" /> 

    <TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:text="Video Test.." 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

有除了在設備上說,當應用程序運行Sorry, this video cannot be played

我該如何解決沒有錯誤這個問題?謝謝。

注:如果我可以將視頻與移動播放器關聯起來,那也可以接受。但是,我不想在WebView中打開新的瀏覽器或選項卡。

回答

0

我最近找到了解決方案。視頻在沒有任何第三方應用或網頁的情況下正在流傳播。

public class VideoActivity extends Activity { 

private WebView video; 

public static final int USER_MOBILE = 0; 
public static final int USER_DESKTOP = 1; 
protected static final String WebSettings = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_video); 

video = (WebView) findViewById(R.id.videoView); 
video.getSettings().setJavaScriptEnabled(true); 
//video.getSettings().setPluginState(WebSettings.PluginState.ON); 
//video.getSettings().setUserAgent(USER_MOBILE); 
video.setWebChromeClient(new WebChromeClient() { 
}); 

final String mimeType = "text/html"; 
final String encoding = "UTF-8"; 
String html = getHTML("0sFTC7l0okg"); //Only change this video id to change video! 
video.loadDataWithBaseURL("", html, mimeType, encoding, ""); 

} 

public String getHTML(String videoId) { 


String html = 
"<iframe class=\"youtube-player\" " 
+ "style=\"border: 0; width: 100%; height: 95%;" 
+ "padding:0px; margin:0px\" " 
+ "id=\"ytplayer\" type=\"text/html\" " 
+ "src=\"https://www.youtube.com/embed/" + videoId 
+ "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer " 
+ "controls onclick=\"this.play()\">\n" + "</iframe>\n"; 

return html; 
}} 

activity_video.xml:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/scroll" > 

<LinearLayout 

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".VideoActivity" > 

<WebView 
android:id="@+id/videoView" 
android:layout_width="fill_parent" 
android:layout_height="319dp" /> 

</LinearLayout> 

</ScrollView> 
0

使用的YouTube API用於播放Android應用YouTube視頻....

如果你想玩的任何URL的視頻更換防爆此行

...

String videoUrl= "http://www.pocketjourney.com/downloads/pj/video/famous.3gp" 
+0

這是我已經。並不成功。 – Umitk