2014-05-09 3 views
0

Je souhaite intégrer une vidéo YouTube dans mon activité Android par URL vidéo. C'est ce que je l'ai fait jusqu'à présent:Diffusion vidéo en continu dans l'activité

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(); 

} } 

Et voici ma .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> 

Il n'y a pas d'erreur, sauf dire sur l'appareil lorsque l'application fonctionne Sorry, this video cannot be played

Comment puis-je résoudre ce problème? Je vous remercie.

Note: Si je peux relier la vidéo avec un lecteur mobile, cela pourrait aussi être acceptable. Mais, je ne veux pas ouvrir un nouveau navigateur ou onglet dans WebView.

Répondre

0

Je trouve ces derniers temps la solution. La vidéo est diffusée sur l'activité sans aucune application ou page Web tierce.

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

utiliser YouTube api pour la lecture vidéo sur youtube en application android ....

si vous voulez lire une vidéo de toute URL remplacer cette ligne Ex ...

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

ce que je l'ai déjà. et échoué. – Umitk

Questions connexes