1

I'm developing an app with Android Studio for which I need the Youtube API. The problem is that I want the video to be played every time the user clicks on "Reproducir"("play"). So I have researched how to load a video and everybody says to use the function initializates that will call onInitializationSuccess and playes there. But if I want to load new videos, parsing the url and playing the new videos I can't. I tried calling the initialization everytime the user clicks on Reproducir but in that case It ig.

public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_video);
    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
    urlTxtView = (EditText) findViewById(R.id.urlYoutube);
    playButton = (Button) findViewById(R.id.buttonPlay);
    playButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
    }
});

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    String url = urlTxtView.getText().toString();
    Log.d("1",url );
    youTubePlayer.loadVideo("a4NT5iBFuZs");

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
    Toast.makeText(this, "ERROR:"+result.toString(), Toast.LENGTH_LONG).show();

}

XML Layout

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


    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/action_add_video"
        android:textStyle="normal|bold"
        android:textSize="30sp"
        android:gravity="center_horizontal"
        android:layout_marginBottom="20dp" />

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

    <EditText
        android:id="@+id/tituloVideo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Titulo"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/urlYoutube"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="URL o buscar con @youtube"
        android:inputType="textPersonName" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonCancelar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancelar" />
        <Button
            android:id="@+id/buttonPlay"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Reproducir" />
        <Button
            android:id="@+id/buttonGuardar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Guardar" />
    </LinearLayout>

</LinearLayout>

Log: W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50 #102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge. .

Here there is an image of the idea:

Picture of Android emulator, the user should introduce an url, clicks on Reproducir and plays the video from the url

user1140237
  • 5,015
  • 1
  • 28
  • 56
  • any error or crash log ? api key is properly given and enable from api console ? – user1140237 Feb 20 '17 at 11:38
  • No crash log, and the key is fine because if I initializate once It works, It's like It can't be initializated more than once. So maybe the solution would be something like getting the player from another way and load video without initializating again – Javier Uribe Feb 20 '17 at 11:39
  • Sorry, there is a log: W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50 #102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge. . – Javier Uribe Feb 20 '17 at 11:46
  • post your whole code.. screen xml too ? – user1140237 Feb 20 '17 at 11:57

1 Answers1

1

If you are reinitilize the youtube video video view if already its been initialize thn first you need to release exising youtube view.

Please check below code Specifically i've changed initialize part on Play button click

playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (youTubePlayer != null) {
                    youTubePlayer.release();
                }
                youTubeView.initialize("AIzaSyDo9p_Qjy8XRBdFnVOaIx586MzHHLOUopw", (YouTubePlayer.OnInitializedListener) view.getContext());
            }
        });

Entire Activity code

 public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
        private YouTubePlayerView youTubeView;
        private EditText urlTxtView;
        private Button playButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_video);
            youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
    
            urlTxtView = (EditText) findViewById(R.id.urlYoutube);
            playButton = (Button) findViewById(R.id.buttonPlay);
            playButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (youTubePlayer != null) {
                        youTubePlayer.release();
                    }
                    youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
                }
            });
    
        }
    
        private YouTubePlayer youTubePlayer;
    
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
            this.youTubePlayer = youTubePlayer;
            String url = urlTxtView.getText().toString();
            Log.d("1", url);
            if (!wasRestored) {/// here videoid you need to pass not entire video URL
                youTubePlayer.loadVideo(url);
            }
    
    
        }
    
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
            Toast.makeText(this, "ERROR:" + result.toString(), Toast.LENGTH_LONG).show();
    
        }
    }

to load video you need to pass video id not video url.So with assumption tht in edit text video id will be pass or if url thn you will get the video id from url and thn video will be loaded

this.youTubePlayer = youTubePlayer;
                String url = urlTxtView.getText().toString();
                Log.d("1", url);
                if (!wasRestored) {/// here videoid you need to pass not entire video URL
                    youTubePlayer.loadVideo(url);
                }
        

And for below log

Log: W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50

102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge.

you can refer answer https://stackoverflow.com/a/29676512/1140237

in your case for this log... issue is with Application or Activity theme. Give No ActionBarTheme to resolve that error

Community
  • 1
  • 1
user1140237
  • 5,015
  • 1
  • 28
  • 56