Skip to content Skip to sidebar Skip to footer

Android Code: I Have Coded For Youtube Url But I Can't Play Videos Inside My App

I am new to Android and I have linked the URL to my YouTube video. I have created a webview in XML and I have linked the URL of YouTube from Java file. The problem is I am not able

Solution 1:

Try with this simple code.

publicclassMainextendsActivity {

    private WebView mWebview ;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mWebview  = newWebView(this);
        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascriptfinalActivityactivity=this;

        mWebview.setWebViewClient(newWebViewClient() {
            publicvoidonReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("https://www.youtube.com/results?search_query=apples+are+yummy");
        setContentView(mWebview);

    }
    }

and also don't forget to pass

<uses-permissionandroid:name="android.permission.INTERNET" />

in android manifest file

EDIT

publicclassWebViewActivityextendsActivity {

    private WebView webView;

    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://www.youtube.com/results?search_query=apples+are+yummy");
    }
}

XML

<?xml version="1.0" encoding="utf-8"?><WebViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/webView1"android:layout_width="fill_parent"android:layout_height="fill_parent"
/>

VideoView

Uriuri= Uri.parse(URL); //Declare your url here.VideoViewmVideoView= (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(newMediaController(this));       
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

XML for videoview

<LinearLayoutandroid:id="@+id/LinearLayout01"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:orientation="vertical"><VideoViewandroid:layout_height="fill_parent"android:layout_width="fill_parent"android:id="@+id/VideoView"></VideoView></LinearLayout>

Post a Comment for "Android Code: I Have Coded For Youtube Url But I Can't Play Videos Inside My App"