Question

I want to embed a YouTube video into my Android activity by video URL. This is what i have done so far:

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

} }

And this is my .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>

There is no error except saying on device when app runs Sorry, this video cannot be played

How can i solve this issue? Thank you.

Note: If i can relate video with mobile player, that might also be acceptable. But, I don`t want to open a new browser or tab in WebView.

Was it helpful?

Solution 2

I lately found out the solution. Video is streaming on Activity without any 3rd party app or Webpage.

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>

OTHER TIPS

use youtube api for playing youtube video in android app....

if you want to play a video from any url replace this line for Ex...

String videoUrl= "http://www.pocketjourney.com/downloads/pj/video/famous.3gp"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top