How to embed and play a YouTube video in android

前端 未结 6 630
粉色の甜心
粉色の甜心 2021-02-05 18:11

Can we watch youtube video in android app? I mean if we have link to video on youtube, can we play it in VideoView or another widget? Any ideas about it?

相关标签:
6条回答
  • 2021-02-05 18:28

    First you should try to download the Youtube player library for Android from the link below:

    Youtube Android Player

    You should first install it like this: Project -> menu: File > Structure > Dependencies Tab > Add -> library dependency

    if it doesn't work, please try one of these two:

    Add dependency of the library inside dependency inside build.gradle file of the library u r using, and paste ur library in External Libraries.

    OR

    Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there Now the trick here is that now go inside settings.gradle file now add this line include ':app:libs' after include ':app' It will definitely work.

    Then, you should have a layout like this:

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    And you can have a player activity like this:

    import android.os.Bundle;
    import android.util.Log;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    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;
    import com.google.api.client.http.HttpRequest;
    import com.google.api.client.http.HttpRequestInitializer;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.services.youtube.YouTube;
    import java.io.IOException;
    
    public class YoutubeActivity extends YouTubeBaseActivity{
    
        private YouTubePlayerView playerView;
        private YouTube youtube;
    
        @Override
        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);
    
            setContentView(R.layout.activity_youtube);
    
            youtube = new YouTube.Builder(new NetHttpTransport(),
                    new JacksonFactory(), new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest hr) throws IOException {}
            }).setApplicationName(this.getString(R.string.app_name)).build();
    
    
            playerView = (YouTubePlayerView)findViewById(R.id.player_view);
            playerView.initialize("Your API Key", new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    if(!b){
                        String videoId = getIntent().getExtras().getString("videoID");
                        youTubePlayer.cueVideo(videoId);
                    }
                }
    
                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                    Toast.makeText(getApplicationContext(), getString(R.string.failed), Toast.LENGTH_LONG).show();
                }
            });
        }
    
    }
    
    0 讨论(0)
  • 2021-02-05 18:35

    to implement Youtube video refer this post

    http://www.feelzdroid.com/2017/01/embed-youtube-video-player-android-app-example.html

    Here detailed steps with pictorial representations are added, if you face any issues let me know.

    Thanks

    0 讨论(0)
  • 2021-02-05 18:41

    The Answer is Simple "YES".

    Please look at to following link,

    How to play YouTube video in my Android application?

    Streaming Youtube Videos

    0 讨论(0)
  • 2021-02-05 18:43

    You can Use webview to do so. Here is the screen shot of of what it look like with webview :-

    if this is what you want to achieve you can get sample app from here :- https://github.com/hiteshsahu/Android-Universal-Web-Content-Loader

    It support both portrait & landscape mode.

    0 讨论(0)
  • 2021-02-05 18:47

    Can integrate youtube videos using youtube sdk

    with youtube sdk you can have many options while integrating a video

    for more http://www.androidcoding.in/2017/12/17/android-play-youtube-video-play-youtube-video-app/

    private final class StateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
    
    @Override
     public void onLoading() {
     }
    
     @Override
     public void onLoaded(String s) {
      }
    
      @Override
      public void onAdStarted() {
      }
    
      @Override
       public void onVideoStarted() {
       }
    
       @Override
       public void onVideoEnded() {
       }
    
        @Override
       public void onError(YouTubePlayer.ErrorReason errorReason) {
            }
           }
    
    0 讨论(0)
  • 2021-02-05 18:52

    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)
提交回复
热议问题