Embed Youtube video inside an Android app

后端 未结 9 1252
野性不改
野性不改 2021-01-29 23:01

I\'m using a WebView for displaying embedded Youtube video and that works on Galaxcy S2 (OS 2.3.5) and doesn\'t on Nexus S (OS 2.3.4), all I get is white screen without any vide

相关标签:
9条回答
  • 2021-01-29 23:27

    It works like this:

    String item = "http://www.youtube.com/embed/";
    
    String ss = "your url";
    ss = ss.substring(ss.indexOf("v=") + 2);
    item += ss;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int w1 = (int) (metrics.widthPixels / metrics.density), h1 = w1 * 3 / 5;
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebChromeClient(chromeClient);
    wv.getSettings().setPluginsEnabled(true);
    
    try {
        wv.loadData(
        "<html><body><iframe class=\"youtube-player\" type=\"text/html5\" width=\""
        + (w1 - 20)
        + "\" height=\""
        + h1
        + "\" src=\""
        + item
        + "\" frameborder=\"0\"\"allowfullscreen\"></iframe></body></html>",
                                "text/html5", "utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    private WebChromeClient chromeClient = new WebChromeClient() {
    
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    VideoView video = (VideoView) frame.getFocusedChild();
                    frame.removeView(video);
                    video.start();
                }
            }
    
        }
    };
    
    0 讨论(0)
  • 2021-01-29 23:34

    Refer to this answer: How can we play YouTube embeded code in an Android application using webview?

    It uses WebViews and loads an iframe in it... and yes it works.

    0 讨论(0)
  • 2021-01-29 23:37

    although I suggest to use youtube api or call new intent and make the system handle it (i.e. youtube app), here some code that can help you, it has a call to an hidden method because you can't pause and resume webview

    import java.lang.reflect.Method;
    
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    
    import android.app.Activity;
    
    @SuppressLint("SetJavaScriptEnabled")
    public class MultimediaPlayer extends Activity
    {
        private WebView mWebView;
        private boolean mIsPaused = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);
    
            String media_url = VIDEO_URL;
    
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setWebChromeClient(new WebChromeClient());
    
            WebSettings ws = mWebView.getSettings();
            ws.setBuiltInZoomControls(true);
            ws.setJavaScriptEnabled(true);
    
            mIsPaused = true;
            resumeBrowser();
            mWebView.loadUrl(media_url);
        }
    
        @Override
        protected void onPause()
        {
            pauseBrowser();
            super.onPause();
        }
    
        @Override
        protected void onResume()
        {
            resumeBrowser();
            super.onResume();
        }
    
        private void pauseBrowser()
        {
            if (!mIsPaused)
            {
                // pause flash and javascript etc
                callHiddenWebViewMethod(mWebView, "onPause");
                mWebView.pauseTimers();
                mIsPaused = true;
            }
        }
    
        private void resumeBrowser()
        {
            if (mIsPaused)
            {
                // resume flash and javascript etc
                callHiddenWebViewMethod(mWebView, "onResume");
                mWebView.resumeTimers();
                mIsPaused = false;
            }
        }
    
        private void callHiddenWebViewMethod(final WebView wv, final String name)
        {
            try
            {
                final Method method = WebView.class.getMethod(name);
                method.invoke(mWebView);
            } catch (final Exception e)
            {}
        }
    }
    
    0 讨论(0)
  • 2021-01-29 23:38

    Embedding the YouTube player in Android is very simple & it hardly takes you 10 minutes,

    1) Enable YouTube API from Google API console
    2) Download YouTube player Jar file
    3) Start using it in Your app
    

    Here are the detailed steps http://www.feelzdroid.com/2017/01/embed-youtube-video-player-android-app-example.html.

    Just refer it & if you face any problem, let me know, ill help you

    0 讨论(0)
  • 2021-01-29 23:43

    there is an official YouTube Android Player API wich you can use. This is a bit more complicated but it is working better than other solutions using webclients.

    First you must register your app in Googles API Console. This is completely free until your app gets over 25k request a month (or something like that). There are complete anf great tutorials under the link. I hope you can understand them. If not, ask! :)

    0 讨论(0)
  • 2021-01-29 23:45

    How it looks:

    Best solution to my case. I need video fit web view size. Use embed youtube link with your video id. Example:

    WebView youtubeWebView; //todo find or bind web view
    String myVideoYoutubeId = "-bvXmLR3Ozc";
    
    outubeWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
            });
    
    WebSettings webSettings = youtubeWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    
    youtubeWebView.loadUrl("https://www.youtube.com/embed/" + myVideoYoutubeId);
    

    Web view xml code

    <WebView
            android:id="@+id/youtube_web_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
    
    0 讨论(0)
提交回复
热议问题