How to play Youtube videos in Android Video View?

后端 未结 8 1393
[愿得一人]
[愿得一人] 2020-12-02 14:24

I am developing an android application which requires a youtube video player embedded within it. I successfully got the RTSP video URL from the API, but while trying to load

相关标签:
8条回答
  • 2020-12-02 14:58

    Checkout this link . It explains how to implement youtube video in VideoView.

    0 讨论(0)
  • 2020-12-02 14:59

    As I can't find any way to load the rtsp URL in video view (for all devices & android versions), I solved my problem with another work around. I used a webview to embed the youtube player within it, and this method working nicely in all tested devices.

    Here is my code:

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginState(PluginState.ON);
    mWebView.loadUrl("http://www.youtube.com/embed/" + videoID + "?autoplay=1&vq=small");
    mWebView.setWebChromeClient(new WebChromeClient());
    

    Thank you very much for all your help guys.

    0 讨论(0)
  • 2020-12-02 15:01

    Use YouTube Android Player API. It works perfectly. Check my example from here:

    activity_main.xml:

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    tools:context="com.example.andreaskonstantakos.vfy.MainActivity">
    
    <com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_centerHorizontal="true"
    android:id="@+id/youtube_player"
    android:layout_alignParentTop="true" />
    
    <Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="195dp"
    android:visibility="visible"
    android:id="@+id/button" />
    
    
    </RelativeLayout>
    

    MainActivity.java:

    package com.example.andreaskonstantakos.vfy;
    
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import com.google.android.youtube.player.YouTubeBaseActivity;
    import com.google.android.youtube.player.YouTubeInitializationResult;
    import com.google.android.youtube.player.YouTubePlayer;
    import com.google.android.youtube.player.YouTubePlayerView;
    
    
    
    public class MainActivity extends YouTubeBaseActivity {
    
    YouTubePlayerView youTubePlayerView;
    Button button;
    YouTubePlayer.OnInitializedListener onInitializedListener;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
         youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
         button = (Button) findViewById(R.id.button);
    
    
         onInitializedListener = new YouTubePlayer.OnInitializedListener(){
    
             @Override
             public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    
                youTubePlayer.loadVideo("Hce74cEAAaE");
    
                 youTubePlayer.play();
         }
    
             @Override
             public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    
             }
         };
    
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    youTubePlayerView.initialize(PlayerConfig.API_KEY,onInitializedListener);
            }
        });
    }
    }
    

    and the PlayerConfig.java class:

      package com.example.andreaskonstantakos.vfy;
    
    /**
     * Created by Andreas Konstantakos on 13/4/2017.
     */
    
    public class PlayerConfig {
    
    PlayerConfig(){}
    
    public static final String API_KEY = 
    "xxxxx";
    }
    

    Replace the "Hce74cEAAaE" with your video ID from https://www.youtube.com/watch?v=Hce74cEAAaE. Get your API_KEY from Console.developers.google.com and also replace it on the PlayerConfig.API_KEY. For any further information you can follow the following tutorial step by step: https://www.youtube.com/watch?v=3LiubyYpEUk

    0 讨论(0)
  • 2020-12-02 15:10

    After a long search, I found this way of implementation.

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.about_fragment, container, false);
    
        String frameVideo = "<html><body><br><iframe width=\"320\" height=\"200\" src=\"https://www.youtube.com/embed/XDYbEuY8nIc\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
    
        WebView displayYoutubeVideo = (WebView) rootView.findViewById(R.id.videoView);
        displayYoutubeVideo.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        WebSettings webSettings = displayYoutubeVideo.getSettings();
        webSettings.setJavaScriptEnabled(true);
        displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
      return rootView;
        }
    

    inside the layout.xml:

     <WebView android:id="@+id/videoView"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_marginTop="-45dp"
                android:layout_marginLeft="-5dp"/>
    

    This will work well.

    0 讨论(0)
  • 2020-12-02 15:11

    Due to current version of YouTube you are likely to get a “Can’t play this video” error if you will use VideoView to show your video.

    Take a look at this approcach with YouTubePlayerView: http://xinyustudio.wordpress.com/2014/03/17/android-development-play-youtube-video-in-your-app-cant-play-this-video-and-troubleshooting/

    0 讨论(0)
  • 2020-12-02 15:12
    private class YourAsyncTask extends AsyncTask<Void, Void, Void>
        {
            ProgressDialog progressDialog;
    
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                progressDialog = ProgressDialog.show(AlertDetail.this, "", "Loading Video wait...", true);
            }
    
            @Override
            protected Void doInBackground(Void... params)
            {
                try
                {
                    String url = "http://www.youtube.com/watch?v=1FJHYqE0RDg";
                    videoUrl = getUrlVideoRTSP(url);
                    Log.e("Video url for playing=========>>>>>", videoUrl);
                }
                catch (Exception e)
                {
                    Log.e("Login Soap Calling in Exception", e.toString());
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result)
            {
                super.onPostExecute(result);
                progressDialog.dismiss();
    /*
                videoView.setVideoURI(Uri.parse("rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQk4RDShYkdS1BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
                videoView.setMediaController(new MediaController(AlertDetail.this));
                videoView.requestFocus();
                videoView.start();*/            
                videoView.setVideoURI(Uri.parse(videoUrl));
                MediaController mc = new MediaController(AlertDetail.this);
                videoView.setMediaController(mc);
                videoView.requestFocus();
                videoView.start();          
                mc.show();
            }
    
        }
    
    public static String getUrlVideoRTSP(String urlYoutube)
        {
            try
            {
                String gdy = "http://gdata.youtube.com/feeds/api/videos/";
                DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                String id = extractYoutubeId(urlYoutube);
                URL url = new URL(gdy + id);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                Document doc = documentBuilder.parse(connection.getInputStream());
                Element el = doc.getDocumentElement();
                NodeList list = el.getElementsByTagName("media:content");///media:content
                String cursor = urlYoutube;
                for (int i = 0; i < list.getLength(); i++)
                {
                    Node node = list.item(i);
                    if (node != null)
                    {
                        NamedNodeMap nodeMap = node.getAttributes();
                        HashMap<String, String> maps = new HashMap<String, String>();
                        for (int j = 0; j < nodeMap.getLength(); j++)
                        {
                            Attr att = (Attr) nodeMap.item(j);
                            maps.put(att.getName(), att.getValue());
                        }
                        if (maps.containsKey("yt:format"))
                        {
                            String f = maps.get("yt:format");
                            if (maps.containsKey("url"))
                            {
                                cursor = maps.get("url");
                            }
                            if (f.equals("1"))
                                return cursor;
                        }
                    }
                }
                return cursor;
            }
            catch (Exception ex)
            {
                Log.e("Get Url Video RTSP Exception======>>", ex.toString());
            }
            return urlYoutube;
    
        }
    
    protected static String extractYoutubeId(String url) throws MalformedURLException
        {
            String id = null;
            try
            {
                String query = new URL(url).getQuery();
                if (query != null)
                {
                    String[] param = query.split("&");
                    for (String row : param)
                    {
                        String[] param1 = row.split("=");
                        if (param1[0].equals("v"))
                        {
                            id = param1[1];
                        }
                    }
                }
                else
                {
                    if (url.contains("embed"))
                    {
                        id = url.substring(url.lastIndexOf("/") + 1);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.e("Exception", ex.toString());
            }
            return id;
        }
    
    0 讨论(0)
提交回复
热议问题