Jude Fernandes
Jude Fernandes

Reputation: 7517

Integrate Youtube videos in app without having the youtube app

I have followed the instructions available on this page YouTube Android Player API and it has worked successfully but inspite of having downloaded the YouTubeAndroidPlayerApi jar file, if i try running the app on a device which does not have the youtube app, it throws an error requesting to update to the latest youtube version. Is there a way i can embed youtube videos in the app independent of the app user having the youtube app or not on their device?

I have checked out the other questions with most of them suggesting loading the webview in an iframe or a webview. I was wondering if there is any solution available as of 2018 or is this still not possible?

P.S. I am looking to natively embed the video and not load it in a webview.

Upvotes: 1

Views: 2830

Answers (1)

Pierfrancesco Soffritti
Pierfrancesco Soffritti

Reputation: 1738

As far as i know it's not possible to do it any other way. Maybe you can do some hacks and stream videos to a normal player, but I'm not sure that would be 100% ok with YouTube terms of service. WebView is probably the best way to go.

In case you don't want to build everything from scratch, I've built this library to have a ready to use player based on WebView. Android-YouTube-Player.

In order to use it, you only need to import the lib, drop the YouTubePlayerView in your layout and initialize it.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

-

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);

youTubePlayerView.initialize(new YouTubePlayerInitListener() {
    @Override
    public void onInitSuccess(final YouTubePlayer initializedYouTubePlayer) {    
        initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady() {
                String videoId = "6JYIGclVQdw";
                initializedYouTubePlayer.loadVideo(videoId, 0);
            }
        });        
    }
}, true);

Upvotes: 8

Related Questions